-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth_corp_test.go
114 lines (96 loc) · 2.44 KB
/
auth_corp_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
package monobank
import (
"errors"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
)
var (
secKey = []byte("-----BEGIN EC PARAMETERS-----\n" +
"BgUrgQQACg==\n" +
"-----END EC PARAMETERS-----\n" +
"-----BEGIN EC PRIVATE KEY-----\n" +
"MHQCAQEEIP5DyqGW1yUD5YZRSzsvjT5I9M1utN9aYi3uWJgKhsvPoAcGBSuBBAAK\n" +
"oUQDQgAEOX+BUepYysBoGR3l9ZsnIXNBm4FYD6m76rGPvbJnUD11xm/SQrOALZYC\n" +
"s0VrWcLTP60Z1xeLw+NP+D+rUK5IsA==\n" +
"-----END EC PRIVATE KEY-----\n")
keyID = "b38daf14d0e6f487949cefbccce99d8add909685"
)
func TestNewCorpAuthMaker(t *testing.T) {
tests := map[string]struct {
secKey []byte
wantKeyID string
wantErr error
}{
"positive": {
secKey: secKey,
wantKeyID: keyID,
},
"negative": {
secKey: []byte("invalid"),
//nolint:goerr113
wantErr: errors.New("failed to decode private key"),
},
}
for name, tt := range tests {
tt := tt
t.Run(name, func(t *testing.T) {
got, err := NewCorpAuthMaker(tt.secKey)
if tt.wantErr != nil {
assert.EqualError(t, err, tt.wantErr.Error())
} else {
require.NoError(t, err)
require.NotNil(t, got)
assert.NotNil(t, got.privateKey)
assert.Equal(t, tt.wantKeyID, got.KeyID)
}
})
}
}
// ################################## Suite ###################################
type CorpAuthSuite struct {
suite.Suite
authMaker *CorpAuthMaker
}
func TestCorpAuthSuite(t *testing.T) {
suite.Run(t, new(CorpAuthSuite))
}
func (s *CorpAuthSuite) SetupTest() {
authMaker, err := NewCorpAuthMaker(secKey)
s.Require().NoError(err)
s.authMaker = authMaker
}
func (s *CorpAuthSuite) TearDownTest() {
s.authMaker = nil
}
func (s *CorpAuthSuite) Test_sign() {
tests := map[string]struct {
timestamp string
actor string
urlPath string
wantErr error
}{
"positive": {
timestamp: "1136239445",
actor: "p",
urlPath: "/personal/auth/request",
},
}
authIface := s.authMaker.NewPermissions(PermPI)
auth, ok := authIface.(CorpAuth)
s.Require().True(ok)
for name, tt := range tests {
tt := tt
s.Run(name, func() {
got, err := auth.sign(tt.timestamp, tt.actor, tt.urlPath)
if tt.wantErr != nil {
s.Assert().EqualError(err, tt.wantErr.Error())
} else {
s.Require().NoError(err)
// result is random, e.g. "MEUCIQCWqCFyU4dYU8RnAhZST7BXW+2giBEu125X/h9lcRnvPwIgU89Dox807jv07XknxRTsgZFlRUsiKUV9KwdiLQ7gWCI="
s.Assert().Len(got, 96)
}
})
}
}