-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathuser_test.go
162 lines (140 loc) · 3.02 KB
/
user_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
package gitdir
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/belak/go-gitdir/models"
)
func TestLookupUserFromUsername(t *testing.T) {
t.Parallel()
c := newTestConfig()
var tests = []struct { //nolint:gofumpt
Username string
Error error
}{
{
"missing-user",
ErrUserNotFound,
},
{
"disabled",
ErrUserNotFound,
},
}
for _, test := range tests {
user, err := c.LookupUserFromUsername(test.Username)
if test.Error != nil {
require.Equal(t, test.Error, err)
} else {
assert.Nil(t, err)
assert.Equal(t, test.Username, user.Username)
}
}
}
func TestLookupUserFromKey(t *testing.T) { //nolint:funlen
t.Parallel()
c := newTestConfig()
var tests = []struct { //nolint:gofumpt
UserHint string
Username string
PublicKey string
Error error
ErrorGitUser error
}{
{
"missing-user",
"missing-user",
"ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIJ7+BNW+C5HHQ8C3QcJCYfUvxz+biXbxB0JtufT+P2AD user-not-found",
ErrUserNotFound,
ErrUserNotFound,
},
{
"disabled",
"disabled",
"ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIBx4DYr9m+EnG0tgFsUIZqrDP7pa+vpVXJJ6/PE9J7Ll disabled",
ErrUserNotFound,
ErrUserNotFound,
},
{
"invalid-user",
"invalid-user",
"ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIKQJzT5mM5eDYhoe3pVodWPCDzoj0/+pCVNoVsuUR4ao invalid-user",
ErrUserNotFound,
ErrUserNotFound,
},
{
"an-admin",
"an-admin",
"ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAILQGpcX2owFW6hdTWHa/CzbTwhUJlmI8gKAgnp/c0NK2 an-admin",
nil,
nil,
},
{
// Mismatched username will error
"non-admin",
"an-admin",
"ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAILQGpcX2owFW6hdTWHa/CzbTwhUJlmI8gKAgnp/c0NK2 an-admin",
ErrUserNotFound,
nil,
},
}
for _, test := range tests {
pk, err := models.ParsePublicKey([]byte(test.PublicKey))
require.Nil(t, err)
// Try with the username hint
user, err := c.LookupUserFromKey(*pk, test.UserHint)
if test.Error != nil {
require.Equal(t, test.Error, err)
} else {
assert.Nil(t, err)
assert.Equal(t, test.Username, user.Username)
}
// Try without the username hint
user, err = c.LookupUserFromKey(*pk, c.Options.GitUser)
if test.ErrorGitUser != nil {
require.Equal(t, test.ErrorGitUser, err)
} else {
assert.Nil(t, err)
assert.Equal(t, test.Username, user.Username)
}
}
}
func TestLookupUserFromInvite(t *testing.T) {
t.Parallel()
c := newTestConfig()
var tests = []struct { //nolint:gofumpt
Username string
Invite string
Error error
}{
{
"an-admin",
"valid-invite",
nil,
},
{
"an-admin",
"invalid-invite",
ErrUserNotFound,
},
{
"disabled",
"user-disabled",
ErrUserNotFound,
},
{
"invalid-user",
"user-missing",
ErrUserNotFound,
},
}
for _, test := range tests {
user, err := c.LookupUserFromInvite(test.Invite)
if test.Error != nil {
require.Equal(t, test.Error, err)
} else {
assert.Nil(t, err)
assert.Equal(t, test.Username, user.Username)
}
}
}