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
|
package auth
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/MichaelMure/git-bug/entity"
"github.com/MichaelMure/git-bug/identity"
"github.com/MichaelMure/git-bug/repository"
)
func TestCredential(t *testing.T) {
repo := repository.NewMockRepoForTest()
user1 := identity.NewIdentity("user1", "email")
err := user1.Commit(repo)
assert.NoError(t, err)
user2 := identity.NewIdentity("user2", "email")
err = user2.Commit(repo)
assert.NoError(t, err)
storeToken := func(user identity.Interface, val string, target string) *Token {
token := NewToken(user.Id(), val, target)
err = Store(repo, token)
require.NoError(t, err)
return token
}
token := storeToken(user1, "foobar", "github")
// Store + Load
err = Store(repo, token)
assert.NoError(t, err)
token2, err := LoadWithId(repo, token.ID())
assert.NoError(t, err)
assert.Equal(t, token.createTime.Unix(), token2.CreateTime().Unix())
token.createTime = token2.CreateTime()
assert.Equal(t, token, token2)
prefix := string(token.ID())[:10]
// LoadWithPrefix
token3, err := LoadWithPrefix(repo, prefix)
assert.NoError(t, err)
assert.Equal(t, token.createTime.Unix(), token3.CreateTime().Unix())
token.createTime = token3.CreateTime()
assert.Equal(t, token, token3)
token4 := storeToken(user1, "foo", "gitlab")
token5 := storeToken(user2, "bar", "github")
// List + options
creds, err := List(repo, WithTarget("github"))
assert.NoError(t, err)
sameIds(t, creds, []Credential{token, token5})
creds, err = List(repo, WithTarget("gitlab"))
assert.NoError(t, err)
sameIds(t, creds, []Credential{token4})
creds, err = List(repo, WithUser(user1))
assert.NoError(t, err)
sameIds(t, creds, []Credential{token, token4})
creds, err = List(repo, WithUserId(user1.Id()))
assert.NoError(t, err)
sameIds(t, creds, []Credential{token, token4})
creds, err = List(repo, WithKind(KindToken))
assert.NoError(t, err)
sameIds(t, creds, []Credential{token, token4, token5})
creds, err = List(repo, WithKind(KindLoginPassword))
assert.NoError(t, err)
sameIds(t, creds, []Credential{})
// Exist
exist := IdExist(repo, token.ID())
assert.True(t, exist)
exist = PrefixExist(repo, prefix)
assert.True(t, exist)
// Remove
err = Remove(repo, token.ID())
assert.NoError(t, err)
creds, err = List(repo)
assert.NoError(t, err)
sameIds(t, creds, []Credential{token4, token5})
}
func sameIds(t *testing.T, a []Credential, b []Credential) {
t.Helper()
ids := func(creds []Credential) []entity.Id {
result := make([]entity.Id, len(creds))
for i, cred := range creds {
result[i] = cred.ID()
}
return result
}
assert.ElementsMatch(t, ids(a), ids(b))
}
|