1
2
3
4
5
6
7
8
9 package integration
10
11 import (
12 "context"
13 "math/rand"
14 "os"
15 "strconv"
16 "strings"
17 "testing"
18 "time"
19
20 "github.com/google/go-github/v47/github"
21 )
22
23 const msgEnvMissing = "Skipping test because the required environment variable (%v) is not present."
24 const envKeyGitHubUsername = "GITHUB_USERNAME"
25 const envKeyGitHubPassword = "GITHUB_PASSWORD"
26 const envKeyClientID = "GITHUB_CLIENT_ID"
27 const envKeyClientSecret = "GITHUB_CLIENT_SECRET"
28 const envKeyAccessToken = "GITHUB_ACCESS_TOKEN"
29 const InvalidTokenValue = "iamnotacroken"
30
31
32
33 func TestAuthorizationsAppOperations(t *testing.T) {
34
35 appAuthenticatedClient := getOAuthAppClient(t)
36
37
38
39 clientID := os.Getenv(envKeyClientID)
40 accessToken := os.Getenv(envKeyAccessToken)
41
42
43 appAuth, resp, err := appAuthenticatedClient.Authorizations.Check(context.Background(), clientID, accessToken)
44 failOnError(t, err)
45 failIfNotStatusCode(t, resp, 200)
46
47
48 if *appAuth.Token != accessToken {
49 t.Fatal("The returned auth/token does not match.")
50 }
51
52
53 _, resp, err = appAuthenticatedClient.Authorizations.Check(context.Background(), clientID, InvalidTokenValue)
54 if err == nil {
55 t.Fatal("An error should have been returned because of the invalid token.")
56 }
57 failIfNotStatusCode(t, resp, 404)
58
59
60 resetAuth, resp, err := appAuthenticatedClient.Authorizations.Reset(context.Background(), clientID, accessToken)
61 failOnError(t, err)
62 failIfNotStatusCode(t, resp, 200)
63
64
65 _, resp, err = appAuthenticatedClient.Authorizations.Reset(context.Background(), clientID, InvalidTokenValue)
66 if err == nil {
67 t.Fatal("An error should have been returned because of the invalid token.")
68 }
69 failIfNotStatusCode(t, resp, 404)
70
71
72 if *resetAuth.Token == accessToken {
73 t.Fatal("The reset token should be different from the original.")
74 }
75
76
77 if *resetAuth.Token == "" {
78 t.Fatal("A token value should have been returned.")
79 }
80
81
82 _, resp, err = appAuthenticatedClient.Authorizations.Check(context.Background(), clientID, accessToken)
83 if err == nil {
84 t.Fatal("The original token should be invalid.")
85 }
86 failIfNotStatusCode(t, resp, 404)
87
88
89 _, resp, err = appAuthenticatedClient.Authorizations.Check(context.Background(), clientID, *resetAuth.Token)
90 failOnError(t, err)
91 failIfNotStatusCode(t, resp, 200)
92
93
94 resp, err = appAuthenticatedClient.Authorizations.Revoke(context.Background(), clientID, *resetAuth.Token)
95 failOnError(t, err)
96 failIfNotStatusCode(t, resp, 204)
97
98
99
100 time.Sleep(time.Second * 2)
101
102
103 _, resp, err = appAuthenticatedClient.Authorizations.Check(context.Background(), clientID, *resetAuth.Token)
104 if err == nil {
105 t.Fatal("The reset token should be invalid.")
106 }
107 failIfNotStatusCode(t, resp, 404)
108 }
109
110
111
112 func generatePersonalAuthTokenRequest() *github.AuthorizationRequest {
113
114 rand := randString()
115 auth := github.AuthorizationRequest{
116 Note: github.String("Personal token: Note generated by test: " + rand),
117 Scopes: []github.Scope{github.ScopePublicRepo},
118 Fingerprint: github.String("Personal token: Fingerprint generated by test: " + rand),
119 }
120
121 return &auth
122 }
123
124
125
126 func generateAppAuthTokenRequest(clientID string, clientSecret string) *github.AuthorizationRequest {
127
128 rand := randString()
129 auth := github.AuthorizationRequest{
130 Note: github.String("App token: Note generated by test: " + rand),
131 Scopes: []github.Scope{github.ScopePublicRepo},
132 Fingerprint: github.String("App token: Fingerprint generated by test: " + rand),
133 ClientID: github.String(clientID),
134 ClientSecret: github.String(clientSecret),
135 }
136
137 return &auth
138 }
139
140
141 func randString() string {
142 return strconv.FormatInt(rand.NewSource(time.Now().UnixNano()).Int63(), 10)
143 }
144
145
146 func failOnError(t *testing.T, err error) {
147
148 if err != nil {
149 t.Fatal(err)
150 }
151 }
152
153
154 func failIfNotStatusCode(t *testing.T, resp *github.Response, expectedCode int) {
155
156 if resp.StatusCode != expectedCode {
157 t.Fatalf("Expected HTTP status code [%v] but received [%v]", expectedCode, resp.StatusCode)
158 }
159
160 }
161
162
163
164
165 func getUserPassClient(t *testing.T) *github.Client {
166 username, ok := os.LookupEnv(envKeyGitHubUsername)
167 if !ok {
168 t.Skipf(msgEnvMissing, envKeyGitHubUsername)
169 }
170
171 password, ok := os.LookupEnv(envKeyGitHubPassword)
172 if !ok {
173 t.Skipf(msgEnvMissing, envKeyGitHubPassword)
174 }
175
176 tp := github.BasicAuthTransport{
177 Username: strings.TrimSpace(username),
178 Password: strings.TrimSpace(password),
179 }
180
181 return github.NewClient(tp.Client())
182 }
183
184
185
186
187
188
189
190
191
192 func getOAuthAppClient(t *testing.T) *github.Client {
193
194 username, ok := os.LookupEnv(envKeyClientID)
195 if !ok {
196 t.Skipf(msgEnvMissing, envKeyClientID)
197 }
198
199 password, ok := os.LookupEnv(envKeyClientSecret)
200 if !ok {
201 t.Skipf(msgEnvMissing, envKeyClientSecret)
202 }
203
204 tp := github.BasicAuthTransport{
205 Username: strings.TrimSpace(username),
206 Password: strings.TrimSpace(password),
207 }
208
209 return github.NewClient(tp.Client())
210 }
211
View as plain text