...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package github
16
17 import (
18 "os"
19 "testing"
20
21 "github.com/google/go-containerregistry/pkg/authn"
22 )
23
24
25
26 func TestKeychain(t *testing.T) {
27 username, tok := "octocat", "my-token"
28 os.Setenv("GITHUB_ACTOR", username)
29 os.Setenv("GITHUB_TOKEN", tok)
30 got, err := Keychain.Resolve(resource("ghcr.io/my/repo"))
31 if err != nil {
32 t.Fatalf("Resolve: %v", err)
33 }
34 if got == authn.Anonymous {
35 t.Fatalf("Got anonymous, wanted authenticator")
36 }
37
38 auth, err := got.Authorization()
39 if err != nil {
40 t.Fatalf("Authorization: %v", err)
41 }
42 if auth.Username != username {
43 t.Errorf("Got username %q, want %q", auth.Username, username)
44 }
45 if auth.Password != tok {
46 t.Errorf("Got password %q, want %q", auth.Password, tok)
47 }
48 }
49
50
51
52 func TestKeychainUsernameUnset(t *testing.T) {
53 tok := "my-token"
54 os.Unsetenv("GITHUB_ACTOR")
55 os.Setenv("GITHUB_TOKEN", tok)
56 got, err := Keychain.Resolve(resource("ghcr.io/my/repo"))
57 if err != nil {
58 t.Fatalf("Resolve: %v", err)
59 }
60 if got == authn.Anonymous {
61 t.Fatalf("Got anonymous, wanted authenticator")
62 }
63
64 auth, err := got.Authorization()
65 if err != nil {
66 t.Fatalf("Authorization: %v", err)
67 }
68 if auth.Username != "unset" {
69 t.Errorf("Got username %q, want unset", auth.Username)
70 }
71 if auth.Password != tok {
72 t.Errorf("Got password %q, want %q", auth.Password, tok)
73 }
74 }
75
76
77
78 func TestKeychainUnset(t *testing.T) {
79 os.Unsetenv("GITHUB_TOKEN")
80
81 got, err := Keychain.Resolve(resource("ghcr.io/my/repo"))
82 if err != nil {
83 t.Fatalf("Resolve: %v", err)
84 }
85 if got != authn.Anonymous {
86 t.Errorf("Resolve(ghcr.io) got %v, want Anonymous", got)
87 }
88 }
89
90
91 func TestNoMatch(t *testing.T) {
92 os.Setenv("GITHUB_TOKEN", "my-token")
93 for _, s := range []string{
94 "gcr.io",
95 "example.com",
96 "ghcr.io.example.com",
97 "invalid-domain-name -- %U)(@*)(%*)@(*#%@",
98 } {
99 got, err := Keychain.Resolve(resource(s))
100 if err != nil {
101 t.Fatalf("Resolve: %v", err)
102 }
103 if got != authn.Anonymous {
104 t.Errorf("Resolve(%q) got %v, want Anonymous", s, got)
105 }
106 }
107 }
108
109 type resource string
110
111 func (r resource) String() string { return string(r) }
112 func (r resource) RegistryStr() string { return string(r) }
113
View as plain text