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

     1  package server
     2  
     3  import (
     4  	"io/ioutil"
     5  	"net/http"
     6  	"net/url"
     7  	"os"
     8  
     9  	"github.com/hoffie/larasync/api/common"
    10  
    11  	. "gopkg.in/check.v1"
    12  )
    13  
    14  type AuthorizationGetTests struct {
    15  	AuthorizationTests
    16  }
    17  
    18  var _ = Suite(&AuthorizationGetTests{getAuthorizationTest()})
    19  
    20  func (t *AuthorizationGetTests) TestRepositoryNotExists(c *C) {
    21  	resp := t.getResponse(t.req)
    22  	c.Assert(resp.Code, Equals, http.StatusUnauthorized)
    23  }
    24  
    25  func (t *AuthorizationGetTests) TestNotSigned(c *C) {
    26  	t.createRepository(c)
    27  	resp := t.getResponse(t.req)
    28  	c.Assert(resp.Code, Equals, http.StatusUnauthorized)
    29  }
    30  
    31  func (t *AuthorizationGetTests) TestSignedWithRepositoryKey(c *C) {
    32  	t.createRepository(c)
    33  	t.signRequest()
    34  	resp := t.getResponse(t.req)
    35  
    36  	c.Assert(resp.Code, Equals, http.StatusUnauthorized)
    37  }
    38  
    39  func (t *AuthorizationGetTests) TestNotFound(c *C) {
    40  	t.createRepository(c)
    41  	t.signRequestWithAuthKey()
    42  	resp := t.getResponse(t.req)
    43  
    44  	c.Assert(resp.Code, Equals, http.StatusUnauthorized)
    45  }
    46  
    47  func (t *AuthorizationGetTests) setUpWithExist(c *C) {
    48  	t.createRepository(c)
    49  	auth := t.testAuthorization(c)
    50  	t.addAuthorization(c, auth)
    51  	t.signRequestWithAuthKey()
    52  }
    53  
    54  func (t *AuthorizationGetTests) TestGet(c *C) {
    55  	t.setUpWithExist(c)
    56  
    57  	resp := t.getResponse(t.req)
    58  	c.Assert(resp.Code, Equals, http.StatusOK)
    59  }
    60  
    61  func (t *AuthorizationGetTests) TestGetMimeType(c *C) {
    62  	t.setUpWithExist(c)
    63  
    64  	resp := t.getResponse(t.req)
    65  	c.Assert(resp.Header().Get("Content-Type"), Equals, "application/octet-stream")
    66  }
    67  
    68  func (t *AuthorizationGetTests) TestGetBody(c *C) {
    69  	t.setUpWithExist(c)
    70  
    71  	resp := t.getResponse(t.req)
    72  	data, err := ioutil.ReadAll(resp.Body)
    73  	c.Assert(err, IsNil)
    74  
    75  	c.Assert(len(data) > 0, Equals, true)
    76  }
    77  
    78  func (t *AuthorizationGetTests) TestRemove(c *C) {
    79  	t.setUpWithExist(c)
    80  
    81  	t.getResponse(t.req)
    82  
    83  	repo := t.getRepository(c)
    84  	_, err := repo.GetAuthorizationReader(t.authPublicKey)
    85  	c.Assert(os.IsNotExist(err), Equals, true)
    86  }
    87  
    88  func (t *AuthorizationGetTests) TestPublicKeyExtractionFailure(c *C) {
    89  	t.setUpWithExist(c)
    90  	urlString := t.req.URL.String()
    91  	urlString = urlString[:len(urlString)-2]
    92  	var err error
    93  	t.req.URL, err = url.Parse(urlString)
    94  	c.Assert(err, IsNil)
    95  
    96  	resp := t.getResponse(t.req)
    97  	c.Assert(resp.Code, Equals, http.StatusUnauthorized)
    98  }
    99  
   100  func (t *AuthorizationGetTests) signRequestWithAuthKey() {
   101  	common.SignWithKey(t.req, t.authPrivateKey)
   102  }