This repository has been archived by the owner on Apr 2, 2024. It is now read-only.
generated from mrz1836/go-template
-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
model_access_keys_test.go
173 lines (151 loc) · 5.66 KB
/
model_access_keys_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
package bux
import (
"encoding/hex"
"testing"
"time"
"github.com/BuxOrg/bux/utils"
"github.com/bitcoinschema/go-bitcoin/v2"
"github.com/libsv/go-bk/bec"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func Test_newAccessKey(t *testing.T) {
t.Run("valid access key", func(t *testing.T) {
key := newAccessKey(testXPubID)
require.NotNil(t, key)
assert.Equal(t, testXPubID, key.XpubID)
assert.Equal(t, ModelAccessKey.String(), key.GetModelName())
assert.Equal(t, 64, len(key.GetID()))
assert.Equal(t, 64, len(key.Key))
privateKey, _ := bitcoin.PrivateKeyFromString(key.Key)
assert.IsType(t, bec.PrivateKey{}, *privateKey)
publicKey := privateKey.PubKey()
assert.IsType(t, bec.PublicKey{}, *publicKey)
id := utils.Hash(hex.EncodeToString(publicKey.SerialiseCompressed()))
assert.Equal(t, id, key.ID)
})
t.Run("save", func(t *testing.T) {
ctx, client, deferMe := CreateTestSQLiteClient(t, false, false, withTaskManagerMockup())
defer deferMe()
key := newAccessKey(testXPubID, append(
client.DefaultModelOptions(),
New(),
WithMetadatas(Metadata{
"test-key": "test-value",
}),
)...)
assert.Equal(t, 64, len(key.Key))
err := key.Save(ctx)
id := key.ID
require.NoError(t, err)
var accessKey *AccessKey
accessKey, err = getAccessKey(ctx, id, client.DefaultModelOptions()...)
require.NoError(t, err)
assert.Equal(t, id, accessKey.ID)
assert.Equal(t, testXPubID, accessKey.XpubID)
assert.Equal(t, "", accessKey.Key) // private key is lost after Save
assert.Len(t, accessKey.Metadata, 1)
assert.Equal(t, "test-value", accessKey.Metadata["test-key"])
})
t.Run("revoke", func(t *testing.T) {
ctx, client, deferMe := CreateTestSQLiteClient(t, false, false, withTaskManagerMockup())
defer deferMe()
key := newAccessKey(testXPubID, append(client.DefaultModelOptions(), New())...)
assert.Equal(t, 64, len(key.Key))
err := key.Save(ctx)
id := key.ID
require.NoError(t, err)
var accessKey *AccessKey
accessKey, err = getAccessKey(ctx, id, client.DefaultModelOptions()...)
require.NoError(t, err)
assert.Equal(t, id, accessKey.ID)
assert.False(t, accessKey.RevokedAt.Valid)
// revoke the key
accessKey.RevokedAt.Valid = true
accessKey.RevokedAt.Time = time.Now()
err = accessKey.Save(ctx)
require.NoError(t, err)
var revokedKey *AccessKey
revokedKey, err = getAccessKey(ctx, id, client.DefaultModelOptions()...)
require.NoError(t, err)
assert.Equal(t, id, revokedKey.ID)
assert.True(t, revokedKey.RevokedAt.Valid)
})
}
// TestAccessKey_GetAccessKey will test the method getAccessKey()
func TestAccessKey_GetAccessKey(t *testing.T) {
t.Run("not found", func(t *testing.T) {
ctx, client, deferMe := CreateTestSQLiteClient(t, false, false, withTaskManagerMockup())
defer deferMe()
accessKey, err := getAccessKey(ctx, testXPubID, client.DefaultModelOptions()...)
require.NoError(t, err)
assert.Nil(t, accessKey)
})
t.Run("found tx", func(t *testing.T) {
ctx, client, deferMe := CreateTestSQLiteClient(t, false, true)
defer deferMe()
opts := client.DefaultModelOptions()
ak := newAccessKey(testXPubID, append(opts, New())...)
txErr := ak.Save(ctx)
require.NoError(t, txErr)
assert.NotEqual(t, "", ak.Key)
accessKey, err := getAccessKey(ctx, ak.ID, client.DefaultModelOptions()...)
require.NoError(t, err)
assert.NotNil(t, accessKey)
assert.Equal(t, ak.ID, accessKey.ID)
assert.Equal(t, testXPubID, accessKey.XpubID)
assert.Equal(t, "", accessKey.Key)
})
}
// TestAccessKey_GetAccessKeys will test the method getAccessKeysByXPubID()
func TestAccessKey_GetAccessKeys(t *testing.T) {
t.Run("not found", func(t *testing.T) {
ctx, client, deferMe := CreateTestSQLiteClient(t, false, false, withTaskManagerMockup())
defer deferMe()
accessKey, err := getAccessKeysByXPubID(ctx, testXPubID, nil, nil, nil, client.DefaultModelOptions()...)
require.NoError(t, err)
assert.Nil(t, accessKey)
})
t.Run("found txs", func(t *testing.T) {
ctx, client, deferMe := CreateTestSQLiteClient(t, false, true)
defer deferMe()
opts := client.DefaultModelOptions()
ak := newAccessKey(testXPubID, append(opts, New())...)
txErr := ak.Save(ctx)
require.NoError(t, txErr)
assert.NotEqual(t, "", ak.Key)
ak2 := newAccessKey(testXPubID, append(opts, New())...)
txErr = ak2.Save(ctx)
require.NoError(t, txErr)
assert.NotEqual(t, "", ak2.Key)
accessKeys, err := getAccessKeysByXPubID(ctx, testXPubID, nil, nil, nil, client.DefaultModelOptions()...)
require.NoError(t, err)
assert.Len(t, accessKeys, 2)
assert.Equal(t, ak.ID, accessKeys[0].ID)
assert.Equal(t, testXPubID, accessKeys[0].XpubID)
assert.Equal(t, "", accessKeys[0].Key)
assert.Equal(t, ak2.ID, accessKeys[1].ID)
assert.Equal(t, testXPubID, accessKeys[1].XpubID)
assert.Equal(t, "", accessKeys[1].Key)
})
t.Run("found txs with metadata", func(t *testing.T) {
ctx, client, deferMe := CreateTestSQLiteClient(t, false, true)
defer deferMe()
opts := client.DefaultModelOptions()
ak := newAccessKey(testXPubID, append(opts, New(), WithMetadata("test-key", "test-value-1"))...)
txErr := ak.Save(ctx)
require.NoError(t, txErr)
assert.NotEqual(t, "", ak.Key)
ak2 := newAccessKey(testXPubID, append(opts, New(), WithMetadata("test-key", "test-value-2"))...)
txErr = ak2.Save(ctx)
require.NoError(t, txErr)
assert.NotEqual(t, "", ak2.Key)
metadata := &Metadata{"test-key": "test-value-2"}
accessKeys, err := getAccessKeysByXPubID(ctx, testXPubID, metadata, nil, nil, client.DefaultModelOptions()...)
require.NoError(t, err)
assert.Len(t, accessKeys, 1)
assert.Equal(t, ak2.ID, accessKeys[0].ID)
assert.Equal(t, testXPubID, accessKeys[0].XpubID)
assert.Equal(t, "", accessKeys[0].Key)
})
}