AtlatestRepositorysigil-youtube
sigil-youtube / tree / testoauth-test.sgl
1
;;; Tests for (youtube oauth) — OAuth integration module2
;;;3
;;; Tests config creation, client construction from tokens,4
;;; and the ensure-client auto-refresh flow.6
(import (sigil core)7
(sigil dict)8
(sigil struct)9
(sigil test)10
(youtube)11
(youtube oauth))13
;; ---------------------------------------------------------------14
;; Config tests15
;; ---------------------------------------------------------------17
(test-group "youtube oauth config"19
(test "creates google oauth config with defaults"20
(let ((config (youtube-oauth-config "my-client-id" "my-secret")))21
(assert-true (oauth-config? config))22
(assert-equal "my-client-id" (oauth-config-client-id config))23
(assert-equal "my-secret" (oauth-config-client-secret config))24
(assert-equal "https://accounts.google.com/o/oauth2/v2/auth"25
(oauth-config-auth-url config))26
(assert-equal "https://oauth2.googleapis.com/token"27
(oauth-config-token-url config))))29
(test "default scopes include youtube and analytics"30
(let* ((config (youtube-oauth-config "id" "secret"))31
(scopes (oauth-config-scopes config)))32
(assert-true (member "https://www.googleapis.com/auth/youtube" scopes))33
(assert-true (member "https://www.googleapis.com/auth/yt-analytics.readonly"34
scopes))))36
(test "custom scopes override defaults"37
(let* ((config (youtube-oauth-config "id" "secret"38
#{ scopes: (list "https://www.googleapis.com/auth/youtube.readonly") }))39
(scopes (oauth-config-scopes config)))40
(assert-equal 1 (length scopes))41
(assert-equal "https://www.googleapis.com/auth/youtube.readonly"42
(car scopes))))44
(test "custom redirect uri"45
(let ((config (youtube-oauth-config "id" "secret"46
#{ redirect-uri: "http://localhost:9999/cb" })))47
(assert-equal "http://localhost:9999/cb"48
(oauth-config-redirect-uri config))))50
(test "google extra params include access_type offline"51
(let ((config (youtube-oauth-config "id" "secret")))52
(assert-equal "offline"53
(dict-ref (oauth-config-extra-params config) access_type:)))))55
;; ---------------------------------------------------------------56
;; Client construction tests57
;; ---------------------------------------------------------------59
(test-group "youtube oauth client"61
(test "build client from tokens"62
(let* ((tokens (oauth-tokens access-token: "ya29.test-token"63
refresh-token: "1//refresh"64
expires-at: 9999999999))65
(client (youtube-oauth-client tokens)))66
(assert-true (youtube-client? client))67
(assert-equal "ya29.test-token" (youtube-client-access-token client))68
(assert-equal #f (youtube-client-api-key client))))70
(test "build client from tokens with api-key"71
(let* ((tokens (oauth-tokens access-token: "ya29.tok"))72
(client (youtube-oauth-client tokens #{ api-key: "AIza.key" })))73
(assert-equal "ya29.tok" (youtube-client-access-token client))74
(assert-equal "AIza.key" (youtube-client-api-key client)))))76
;; ---------------------------------------------------------------77
;; Ensure-client tests78
;; ---------------------------------------------------------------80
(test-group "youtube ensure client"82
(test "returns client when token is not expired"83
(let* ((config (youtube-oauth-config "id" "secret"))84
(tokens (oauth-tokens access-token: "ya29.valid"85
refresh-token: "1//refresh"86
expires-at: 9999999999))87
(current-time 1000))88
(let-values (((client new-tokens)89
(youtube-ensure-client config tokens current-time90
#{ save?: #f })))91
(assert-true (youtube-client? client))92
(assert-equal "ya29.valid" (youtube-client-access-token client))93
;; Tokens unchanged when not expired94
(assert-equal "ya29.valid"95
(oauth-tokens-access-token new-tokens)))))97
(test "returns client when no expiry info"98
(let* ((config (youtube-oauth-config "id" "secret"))99
(tokens (oauth-tokens access-token: "ya29.noexpiry"))100
(current-time 1000))101
(let-values (((client new-tokens)102
(youtube-ensure-client config tokens current-time103
#{ save?: #f })))104
(assert-true (youtube-client? client))105
(assert-equal "ya29.noexpiry"106
(youtube-client-access-token client))))))108
(run-tests)