-
Notifications
You must be signed in to change notification settings - Fork 0
/
authorization_test.go
83 lines (67 loc) · 1.83 KB
/
authorization_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
package client
import (
"bytes"
"encoding/hex"
"io"
"io/ioutil"
"path"
"github.com/hoffie/larasync/repository"
. "gopkg.in/check.v1"
)
type AuthorizationClientTest struct {
BaseTest
data []byte
authorization *repository.Authorization
}
var _ = Suite(&AuthorizationClientTest{
BaseTest: newBaseTest(),
})
func (t *AuthorizationClientTest) getAuthorizationURL(c *C) string {
return t.serverURL(c) + "/" + path.Join("authorizations", t.pubKeyToString())
}
func (t *AuthorizationClientTest) SetUpTest(c *C) {
t.BaseTest.SetUpTest(c)
t.data = []byte("This is test authorization data.")
t.authorization = &repository.Authorization{
SigningKey: t.privateKey,
EncryptionKey: t.encryptionKey,
HashingKey: t.hashingKey,
}
t.createRepository(c)
}
func (t *AuthorizationClientTest) pubKeyToString() string {
return hex.EncodeToString(t.pubKey[:])
}
func (t *AuthorizationClientTest) doAuthorization(c *C) (io.Reader, error) {
return t.client.GetAuthorization(t.getAuthorizationURL(c), t.privateKey)
}
func (t *AuthorizationClientTest) TestGet(c *C) {
repository := t.getClientRepository(c)
repository.SetAuthorization(
t.pubKey,
t.encryptionKey,
t.authorization,
)
reader, err := t.doAuthorization(c)
c.Assert(err, IsNil)
d, err := ioutil.ReadAll(reader)
c.Assert(err, IsNil)
c.Assert(len(d) > 0, Equals, true)
}
func (t *AuthorizationClientTest) TestConnError(c *C) {
t.server.Close()
_, err := t.doAuthorization(c)
c.Assert(err, NotNil)
}
func (t *AuthorizationClientTest) putAuthorization(c *C) error {
return t.client.PutAuthorization(&t.pubKey, bytes.NewReader(t.data))
}
func (t *AuthorizationClientTest) TestAdd(c *C) {
err := t.putAuthorization(c)
c.Assert(err, IsNil)
}
func (t *AuthorizationClientTest) TestAddConnError(c *C) {
t.server.Close()
err := t.putAuthorization(c)
c.Assert(err, NotNil)
}