github.com/hoffie/larasync@v0.0.0-20151025221940-0384d2bddcef/api/server/authorization_test.go (about)

     1  package server
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"crypto/rand"
     7  	"encoding/hex"
     8  
     9  	. "gopkg.in/check.v1"
    10  
    11  	"github.com/hoffie/larasync/api/common"
    12  	edhelpers "github.com/hoffie/larasync/helpers/ed25519"
    13  	"github.com/hoffie/larasync/repository"
    14  )
    15  
    16  type AuthorizationTests struct {
    17  	BaseTests
    18  	encryptionKey  [repository.EncryptionKeySize]byte
    19  	authPrivateKey [PrivateKeySize]byte
    20  	authPublicKey  [PublicKeySize]byte
    21  }
    22  
    23  func getAuthorizationTest() AuthorizationTests {
    24  	return AuthorizationTests{
    25  		BaseTests: newBaseTest(),
    26  	}
    27  }
    28  
    29  func (t *AuthorizationTests) SetUpTest(c *C) {
    30  	t.BaseTests.SetUpTest(c)
    31  	t.encryptionKey = [repository.EncryptionKeySize]byte{}
    32  	t.authPrivateKey = [repository.PrivateKeySize]byte{}
    33  
    34  	rand.Read(t.encryptionKey[:])
    35  	byteArray := make([]byte, 200)
    36  	rand.Read(byteArray)
    37  	var err error
    38  	t.authPrivateKey, err = common.PassphraseToKey(byteArray)
    39  	c.Assert(err, IsNil)
    40  	t.authPublicKey = edhelpers.GetPublicKeyFromPrivate(t.authPrivateKey)
    41  
    42  	getURL := t.getURL
    43  	t.getURL = func() string {
    44  		return fmt.Sprintf(
    45  			"%s/authorizations/%s",
    46  			getURL(),
    47  			hex.EncodeToString(t.authPublicKey[:]),
    48  		)
    49  	}
    50  	t.req = t.requestEmptyBody(c)
    51  }
    52  
    53  func (t *AuthorizationTests) testAuthorization(c *C) *repository.Authorization {
    54  	return &repository.Authorization{}
    55  }
    56  
    57  func (t *AuthorizationTests) addAuthorization(c *C, auth *repository.Authorization) {
    58  	repository := t.getClientRepository(c)
    59  
    60  	err := repository.SetAuthorization(t.authPublicKey, t.encryptionKey, auth)
    61  	c.Assert(err, IsNil)
    62  }