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
|
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/repository"
)
func TestCredential(t *testing.T) {
repo := repository.NewMockRepoForTest()
storeToken := func(val string, target string) *Token {
token := NewToken(val, target)
err := Store(repo, token)
require.NoError(t, err)
return token
}
token := storeToken("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("foo", "gitlab")
token5 := storeToken("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, 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{})
// Metadata
token4.SetMetadata("key", "value")
err = Store(repo, token4)
assert.NoError(t, err)
creds, err = List(repo, WithMeta("key", "value"))
assert.NoError(t, err)
sameIds(t, creds, []Credential{token4})
// 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))
}
|