github.com/hoffie/larasync@v0.0.0-20151025221940-0384d2bddcef/api/client/nib_test.go (about) 1 package client 2 3 import ( 4 "bytes" 5 "io/ioutil" 6 "path/filepath" 7 8 "github.com/hoffie/larasync/helpers" 9 "github.com/hoffie/larasync/helpers/crypto" 10 repositoryModule "github.com/hoffie/larasync/repository" 11 "github.com/hoffie/larasync/repository/nib" 12 13 . "gopkg.in/check.v1" 14 ) 15 16 type NIBClientTest struct { 17 BaseTest 18 data []byte 19 } 20 21 var _ = Suite(&NIBClientTest{ 22 BaseTest: newBaseTest(), 23 }) 24 25 func (t *NIBClientTest) SetUpTest(c *C) { 26 t.BaseTest.SetUpTest(c) 27 28 t.createRepository(c) 29 t.data = []byte("This is testdata") 30 } 31 32 func (t *NIBClientTest) AddTestData(c *C) { 33 t.AddDataWith(c, t.data) 34 } 35 36 func (t *NIBClientTest) AddDataWith(c *C, content []byte) { 37 repository := t.getClientRepository(c) 38 path := filepath.Join(t.repositoryPath(c), "test.txt") 39 err := ioutil.WriteFile(path, content, 0600) 40 c.Assert(err, IsNil) 41 42 err = repository.AddItem(path) 43 c.Assert(err, IsNil) 44 } 45 46 func (t *NIBClientTest) getGETResponse(c *C) *NIBGetResponse { 47 t.AddTestData(c) 48 49 response, err := t.client.GetNIBs() 50 c.Assert(err, IsNil) 51 return response 52 } 53 54 func (t *NIBClientTest) TestGet(c *C) { 55 response := t.getGETResponse(c) 56 i := 0 57 for _ = range response.NIBData { 58 i++ 59 } 60 c.Assert(i, Equals, 1) 61 } 62 63 func (t *NIBClientTest) TestGetTransactionIDResponse(c *C) { 64 t.AddTestData(c) 65 repository := t.getClientRepository(c) 66 response := t.getGETResponse(c) 67 transaction, err := repository.CurrentTransaction() 68 c.Assert(err, IsNil) 69 c.Assert(response.ServerTransactionID, Equals, transaction.ID) 70 } 71 72 func (t *NIBClientTest) getFromTransactionIDResponse(c *C) *NIBGetResponse { 73 t.AddTestData(c) 74 t.AddDataWith(c, []byte("Hello world")) 75 repository := t.getClientRepository(c) 76 transaction, err := repository.CurrentTransaction() 77 c.Assert(err, IsNil) 78 response, err := t.client.GetNIBsFromTransactionID(transaction.ID - 1) 79 c.Assert(err, IsNil) 80 return response 81 } 82 83 func (t *NIBClientTest) TestGetFromTransactionID(c *C) { 84 response := t.getFromTransactionIDResponse(c) 85 i := 0 86 for _ = range response.NIBData { 87 i++ 88 } 89 c.Assert(i, Equals, 1) 90 } 91 92 func (t *NIBClientTest) TestGetFromTransactionIDResponse(c *C) { 93 response := t.getFromTransactionIDResponse(c) 94 repository := t.getRepository(c) 95 transaction, err := repository.CurrentTransaction() 96 c.Assert(err, IsNil) 97 c.Assert(response.ServerTransactionID, Equals, transaction.ID) 98 } 99 100 func (t *NIBClientTest) TestConnError(c *C) { 101 t.server.Close() 102 _, err := t.client.GetNIBs() 103 c.Assert(err, NotNil) 104 } 105 106 func (t *NIBClientTest) prepareForNIBAddition(c *C) (string, []byte) { 107 repository := t.getClientRepository(c) 108 t.AddTestData(c) 109 channel, err := repository.GetAllNibs() 110 c.Assert(err, IsNil) 111 nib := <-channel 112 113 reader, err := repository.GetNIBReader(nib.ID) 114 c.Assert(err, IsNil) 115 defer reader.Close() 116 117 data, err := ioutil.ReadAll(reader) 118 c.Assert(err, IsNil) 119 return nib.ID, data 120 121 } 122 123 func (t *NIBClientTest) TestAdd(c *C) { 124 ID, data := t.prepareForNIBAddition(c) 125 126 err := t.client.PutNIB(ID, bytes.NewReader(data)) 127 c.Assert(err, IsNil) 128 } 129 130 func (t *NIBClientTest) TestAddNIBContentMissing(c *C) { 131 privateKey := t.privateKey 132 ID := "1" 133 n := &nib.NIB{ 134 ID: ID, 135 Revisions: []*nib.Revision{ 136 { 137 MetadataID: "meta1", 138 ContentIDs: []string{"content1", "content2"}, 139 UTCTimestamp: 100, 140 DeviceID: "", 141 }, 142 }, 143 } 144 buffer := &bytes.Buffer{} 145 writer := crypto.NewSigningWriter(privateKey, buffer) 146 _, err := n.WriteTo(writer) 147 c.Assert(err, IsNil) 148 err = writer.Finalize() 149 c.Assert(err, IsNil) 150 151 err = t.client.PutNIB(ID, buffer) 152 c.Assert(repositoryModule.IsNIBContentMissing(err), Equals, true) 153 154 nibContentMissing := err.(*repositoryModule.ErrNIBContentMissing) 155 for _, str := range n.AllObjectIDs() { 156 c.Assert(helpers.SliceContainsString(nibContentMissing.MissingContentIDs(), str), Equals, true) 157 } 158 } 159 160 func (t *NIBClientTest) TestAddConnError(c *C) { 161 ID, data := t.prepareForNIBAddition(c) 162 t.server.Close() 163 164 err := t.client.PutNIB(ID, bytes.NewReader(data)) 165 c.Assert(err, NotNil) 166 }