-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathauth_test.go
182 lines (169 loc) · 4.21 KB
/
auth_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
package githubauth
import (
"context"
"crypto/rand"
"crypto/rsa"
"crypto/x509"
"encoding/pem"
"net/http"
"reflect"
"testing"
"time"
"github.com/google/go-github/v64/github"
"github.com/migueleliasweb/go-github-mock/src/mock"
"golang.org/x/oauth2"
)
func TestNewApplicationTokenSource(t *testing.T) {
privateKey, err := generatePrivateKey()
if err != nil {
t.Fatal(err)
}
type args struct {
appID int64
privateKey []byte
opts []ApplicationTokenOpt
}
tests := []struct {
name string
args args
want oauth2.TokenSource
wantErr bool
}{
{
name: "application id is not provided",
args: args{},
wantErr: true,
},
{
name: "private key is not provided",
args: args{appID: 132},
wantErr: true,
},
{
name: "valid application token source",
args: args{
appID: 132,
privateKey: privateKey,
opts: []ApplicationTokenOpt{
WithApplicationTokenExpiration(15 * time.Minute),
},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
_, err := NewApplicationTokenSource(tt.args.appID, tt.args.privateKey, tt.args.opts...)
if (err != nil) != tt.wantErr {
t.Errorf("NewApplicationTokenSource() error = %v, wantErr %v", err, tt.wantErr)
return
}
})
}
}
func Test_installationTokenSource_Token(t *testing.T) {
now := time.Now().UTC()
expiration := now.Add(10 * time.Minute)
mockedHTTPClient := mock.NewMockedHTTPClient(
mock.WithRequestMatch(
mock.PostAppInstallationsAccessTokensByInstallationId,
github.InstallationToken{
Token: github.String("mocked-installation-token"),
ExpiresAt: &github.Timestamp{
Time: expiration,
},
Permissions: &github.InstallationPermissions{
PullRequests: github.String("read"),
},
Repositories: []*github.Repository{
{
Name: github.String("mocked-repo-1"),
ID: github.Int64(1),
},
},
},
),
)
errMockedHTTPClient := mock.NewMockedHTTPClient(
mock.WithRequestMatchHandler(
mock.PostAppInstallationsAccessTokensByInstallationId,
http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusInternalServerError)
_, _ = w.Write([]byte(`{"message":"Internal Server Error"}`))
}),
))
privateKey, err := generatePrivateKey()
if err != nil {
t.Fatal(err)
}
appSrc, err := NewApplicationTokenSource(34434, privateKey, WithApplicationTokenExpiration(5*time.Minute))
if err != nil {
t.Fatal(err)
}
type fields struct {
id int64
src oauth2.TokenSource
opts []InstallationTokenSourceOpt
}
tests := []struct {
name string
fields fields
want *oauth2.Token
wantErr bool
}{
{
name: "error getting installation token",
fields: fields{
id: 1,
src: appSrc,
opts: []InstallationTokenSourceOpt{
WithInstallationTokenOptions(&github.InstallationTokenOptions{}),
WithHTTPClient(errMockedHTTPClient),
},
},
wantErr: true,
},
{
name: "generate a new installation token",
fields: fields{
id: 1,
src: appSrc,
opts: []InstallationTokenSourceOpt{
WithInstallationTokenOptions(&github.InstallationTokenOptions{}),
WithContext(context.Background()),
WithEnterpriseURLs("https://github.example.com", "https://github.example.com"),
WithHTTPClient(mockedHTTPClient),
},
},
want: &oauth2.Token{
AccessToken: "mocked-installation-token",
TokenType: "Bearer",
Expiry: expiration,
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tr := NewInstallationTokenSource(tt.fields.id, tt.fields.src, tt.fields.opts...)
got, err := tr.Token()
if (err != nil) != tt.wantErr {
t.Errorf("installationTokenSource.Token() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("installationTokenSource.Token() = %v, want %v", got, tt.want)
}
})
}
}
func generatePrivateKey() ([]byte, error) {
privateKey, err := rsa.GenerateKey(rand.Reader, 2048)
if err != nil {
return nil, err
}
// Encode the private key to the PEM format
privateKeyPEM := &pem.Block{
Type: "RSA PRIVATE KEY",
Bytes: x509.MarshalPKCS1PrivateKey(privateKey),
}
return pem.EncodeToMemory(privateKeyPEM), nil
}