github.com/hoffie/larasync@v0.0.0-20151025221940-0384d2bddcef/api/client/authorization_test.go (about) 1 package client 2 3 import ( 4 "bytes" 5 "encoding/hex" 6 "io" 7 "io/ioutil" 8 "path" 9 10 "github.com/hoffie/larasync/repository" 11 12 . "gopkg.in/check.v1" 13 ) 14 15 type AuthorizationClientTest struct { 16 BaseTest 17 data []byte 18 authorization *repository.Authorization 19 } 20 21 var _ = Suite(&AuthorizationClientTest{ 22 BaseTest: newBaseTest(), 23 }) 24 25 func (t *AuthorizationClientTest) getAuthorizationURL(c *C) string { 26 return t.serverURL(c) + "/" + path.Join("authorizations", t.pubKeyToString()) 27 } 28 29 func (t *AuthorizationClientTest) SetUpTest(c *C) { 30 t.BaseTest.SetUpTest(c) 31 t.data = []byte("This is test authorization data.") 32 t.authorization = &repository.Authorization{ 33 SigningKey: t.privateKey, 34 EncryptionKey: t.encryptionKey, 35 HashingKey: t.hashingKey, 36 } 37 38 t.createRepository(c) 39 } 40 41 func (t *AuthorizationClientTest) pubKeyToString() string { 42 return hex.EncodeToString(t.pubKey[:]) 43 } 44 45 func (t *AuthorizationClientTest) doAuthorization(c *C) (io.Reader, error) { 46 return t.client.getAuthorization(t.getAuthorizationURL(c), t.privateKey) 47 } 48 49 func (t *AuthorizationClientTest) TestGet(c *C) { 50 repository := t.getClientRepository(c) 51 repository.SetAuthorization( 52 t.pubKey, 53 t.encryptionKey, 54 t.authorization, 55 ) 56 57 reader, err := t.doAuthorization(c) 58 c.Assert(err, IsNil) 59 d, err := ioutil.ReadAll(reader) 60 c.Assert(err, IsNil) 61 c.Assert(len(d) > 0, Equals, true) 62 } 63 64 func (t *AuthorizationClientTest) TestConnError(c *C) { 65 t.server.Close() 66 _, err := t.doAuthorization(c) 67 c.Assert(err, NotNil) 68 } 69 70 func (t *AuthorizationClientTest) putAuthorization(c *C) error { 71 return t.client.PutAuthorization(&t.pubKey, bytes.NewReader(t.data)) 72 } 73 74 func (t *AuthorizationClientTest) TestAdd(c *C) { 75 err := t.putAuthorization(c) 76 c.Assert(err, IsNil) 77 } 78 79 func (t *AuthorizationClientTest) TestAddConnError(c *C) { 80 t.server.Close() 81 err := t.putAuthorization(c) 82 c.Assert(err, NotNil) 83 }