github.com/hoffie/larasync@v0.0.0-20151025221940-0384d2bddcef/repository/clientRepository_test.go (about)

     1  package repository
     2  
     3  import (
     4  	"io/ioutil"
     5  	"path/filepath"
     6  
     7  	. "gopkg.in/check.v1"
     8  )
     9  
    10  type ClientRepositoryTests struct {
    11  	RepositoryTests
    12  }
    13  
    14  var _ = Suite(&ClientRepositoryTests{})
    15  
    16  func (t *ClientRepositoryTests) TestStateConfig(c *C) {
    17  	exp := "example.org:14124"
    18  
    19  	r := NewClient(t.dir)
    20  	err := r.CreateManagementDir()
    21  	c.Assert(err, IsNil)
    22  
    23  	sc, err := r.StateConfig()
    24  	c.Assert(err, IsNil)
    25  	sc.DefaultServer.URL = exp
    26  	sc.Save()
    27  
    28  	r2 := NewClient(t.dir)
    29  	sc2, err := r2.StateConfig()
    30  	c.Assert(err, IsNil)
    31  	c.Assert(sc2.DefaultServer.URL, Equals, exp)
    32  }
    33  
    34  func (t *RepositoryTests) TestPathToNIBID(c *C) {
    35  	r := NewClient(t.dir)
    36  	err := r.CreateManagementDir()
    37  	c.Assert(err, IsNil)
    38  
    39  	err = r.keys.CreateHashingKey()
    40  	c.Assert(err, IsNil)
    41  
    42  	path := "foo/bar.txt"
    43  	id, err := r.pathToNIBID(path)
    44  	c.Assert(err, IsNil)
    45  	c.Assert(id, Not(Equals), "")
    46  
    47  	id2, err := r.pathToNIBID(path)
    48  	c.Assert(err, IsNil)
    49  	c.Assert(id2, Equals, id)
    50  }
    51  
    52  func (t *RepositoryTests) TestGetFileChunkIDs(c *C) {
    53  	r := NewClient(t.dir)
    54  	err := r.CreateManagementDir()
    55  	c.Assert(err, IsNil)
    56  
    57  	err = r.keys.CreateHashingKey()
    58  	c.Assert(err, IsNil)
    59  
    60  	path := filepath.Join(t.dir, "foo.txt")
    61  	err = ioutil.WriteFile(path, []byte("test"), 0600)
    62  	c.Assert(err, IsNil)
    63  
    64  	ids, err := r.getFileChunkIDs(path)
    65  	c.Assert(err, IsNil)
    66  	c.Assert(len(ids), Equals, 1)
    67  	c.Assert(len(ids[0]), Not(Equals), 0)
    68  
    69  	ids2, err := r.getFileChunkIDs(path)
    70  	c.Assert(err, IsNil)
    71  	c.Assert(ids2, DeepEquals, ids)
    72  }
    73  
    74  func (t *RepositoryTests) TestCurrentAuthorization(c *C) {
    75  	r := NewClient(t.dir)
    76  	err := r.CreateManagementDir()
    77  	c.Assert(err, IsNil)
    78  
    79  	err = r.CreateKeys()
    80  	c.Assert(err, IsNil)
    81  
    82  	keyStore := r.keys
    83  	auth, err := r.NewAuthorization()
    84  	c.Assert(err, IsNil)
    85  
    86  	encrpytionKey, err := keyStore.EncryptionKey()
    87  	c.Assert(err, IsNil)
    88  
    89  	hashingKey, err := keyStore.HashingKey()
    90  	c.Assert(err, IsNil)
    91  
    92  	signingKey, err := keyStore.SigningPrivateKey()
    93  	c.Assert(err, IsNil)
    94  
    95  	c.Assert(auth.EncryptionKey, DeepEquals, encrpytionKey)
    96  	c.Assert(auth.HashingKey, DeepEquals, hashingKey)
    97  	c.Assert(auth.SigningKey, DeepEquals, signingKey)
    98  
    99  }
   100  
   101  func (t *RepositoryTests) TestGetSigningKey(c *C) {
   102  	r := NewClient(t.dir)
   103  	err := r.CreateManagementDir()
   104  	c.Assert(err, IsNil)
   105  
   106  	err = r.keys.CreateSigningKey()
   107  	c.Assert(err, IsNil)
   108  
   109  	data, err := r.GetSigningPrivateKey()
   110  	c.Assert(err, IsNil)
   111  
   112  	keyData, err := r.keys.SigningPrivateKey()
   113  	c.Assert(err, IsNil)
   114  
   115  	c.Assert(data, DeepEquals, keyData)
   116  }
   117  
   118  func (t *RepositoryTests) TestTransactionsFrom(c *C) {
   119  	r := NewClient(t.dir)
   120  
   121  	err := r.CreateManagementDir()
   122  	c.Assert(err, IsNil)
   123  
   124  	count := 2
   125  	nibIDs := []string{"a", "b"}
   126  	transactions := make([]*Transaction, count)
   127  	for i := 1; i <= count; i++ {
   128  		transaction := &Transaction{
   129  			ID:     int64(i),
   130  			NIBIDs: nibIDs}
   131  		transactions[i-1] = transaction
   132  		r.transactionManager.Add(transaction)
   133  	}
   134  
   135  	transactions, err = r.TransactionsFrom(1)
   136  	c.Assert(len(transactions), Equals, 1)
   137  }