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

     1  package server
     2  
     3  import (
     4  	"bytes"
     5  	"encoding/json"
     6  	"fmt"
     7  	"io"
     8  	"net/http"
     9  	"net/http/httptest"
    10  	"os"
    11  	"strings"
    12  
    13  	"github.com/hoffie/larasync/api"
    14  
    15  	"github.com/hoffie/larasync/api/common"
    16  
    17  	. "gopkg.in/check.v1"
    18  )
    19  
    20  type RepoListCreateTests struct {
    21  	BaseTests
    22  	req    *http.Request
    23  	pubKey []byte
    24  }
    25  
    26  var _ = Suite(&RepoListCreateTests{
    27  	BaseTests: newBaseTest(),
    28  })
    29  
    30  func (t *RepoListCreateTests) requestWithBytes(c *C, body []byte) *http.Request {
    31  	var httpBody io.Reader
    32  	if body == nil {
    33  		httpBody = nil
    34  	} else {
    35  		httpBody = bytes.NewReader(body)
    36  	}
    37  	return t.requestWithReader(c, httpBody)
    38  }
    39  
    40  func (t *RepoListCreateTests) requestWithReader(c *C, httpBody io.Reader) *http.Request {
    41  	req, err := http.NewRequest(
    42  		"PUT",
    43  		fmt.Sprintf(
    44  			"http://example.org/repositories/%s",
    45  			t.repositoryName,
    46  		),
    47  		httpBody)
    48  	c.Assert(err, IsNil)
    49  	if httpBody != nil {
    50  		req.Header.Set("Content-Type", "application/json")
    51  	}
    52  	return req
    53  }
    54  
    55  func (t *RepoListCreateTests) SetUpTest(c *C) {
    56  	t.repositoryName = "test"
    57  	t.createRepoManager(c)
    58  	t.createServer(c)
    59  	t.req = t.requestWithBytes(c, nil)
    60  }
    61  
    62  func (t *RepoListCreateTests) SetUpSuite(c *C) {
    63  	t.pubKey = make([]byte, PublicKeySize)
    64  	t.createServerCert(c)
    65  }
    66  
    67  func (t *RepoListCreateTests) TearDownTest(c *C) {
    68  	os.RemoveAll(t.repos)
    69  }
    70  
    71  func (t *RepoListCreateTests) getResponse(req *http.Request) *httptest.ResponseRecorder {
    72  	rw := httptest.NewRecorder()
    73  	t.server.router.ServeHTTP(rw, req)
    74  	return rw
    75  }
    76  
    77  func (t *RepoListCreateTests) addPubKey(c *C) {
    78  	repository, err := json.Marshal(api.JSONRepository{
    79  		PubKey: t.pubKey,
    80  	})
    81  	c.Assert(err, IsNil)
    82  	t.req = t.requestWithBytes(c, repository)
    83  }
    84  
    85  func (t *RepoListCreateTests) TestRepoCreateUnauthorized(c *C) {
    86  	resp := t.getResponse(t.req)
    87  	c.Assert(resp.Code, Equals, http.StatusUnauthorized)
    88  }
    89  
    90  func (t *RepoListCreateTests) TestRepoCreateAdmin(c *C) {
    91  	t.addPubKey(c)
    92  	common.SignWithPassphrase(t.req, adminSecret)
    93  	resp := t.getResponse(t.req)
    94  	c.Assert(resp.Code, Equals, http.StatusCreated)
    95  }
    96  
    97  func (t *RepoListCreateTests) TestRepoCreateContentType(c *C) {
    98  	t.addPubKey(c)
    99  	common.SignWithPassphrase(t.req, adminSecret)
   100  	resp := t.getResponse(t.req)
   101  
   102  	contentType := resp.Header().Get("Content-Type")
   103  	c.Assert(
   104  		strings.HasPrefix(
   105  			contentType,
   106  			"application/json"),
   107  		Equals,
   108  		true)
   109  }
   110  
   111  func (t *RepoListCreateTests) TestRepoCreateMangled(c *C) {
   112  	common.SignWithPassphrase(t.req, adminSecret)
   113  	t.req.Header.Set("Mangled", "Yes")
   114  	resp := t.getResponse(t.req)
   115  	c.Assert(resp.Code, Equals, http.StatusUnauthorized)
   116  }
   117  
   118  func (t *RepoListCreateTests) TestRepositoryCreate(c *C) {
   119  	t.addPubKey(c)
   120  	common.SignWithPassphrase(t.req, adminSecret)
   121  	t.getResponse(t.req)
   122  	c.Assert(
   123  		t.rm.Exists(t.repositoryName),
   124  		Equals,
   125  		true,
   126  	)
   127  }
   128  
   129  func (t *RepoListCreateTests) TestWrongPubKeySize(c *C) {
   130  	t.pubKey = make([]byte, 5)
   131  	t.addPubKey(c)
   132  	common.SignWithPassphrase(t.req, adminSecret)
   133  	c.Assert(
   134  		t.getResponse(t.req).Code,
   135  		Equals,
   136  		http.StatusBadRequest,
   137  	)
   138  }
   139  
   140  func (t *RepoListCreateTests) TestRepoAlreadyExists(c *C) {
   141  	t.rm.Create(t.repositoryName, t.pubKey)
   142  	t.addPubKey(c)
   143  	common.SignWithPassphrase(t.req, adminSecret)
   144  	c.Assert(
   145  		t.getResponse(t.req).Code,
   146  		Equals,
   147  		http.StatusConflict,
   148  	)
   149  }
   150  
   151  func (t *RepoListCreateTests) TestMangledJson(c *C) {
   152  	jsonBytes := bytes.NewBufferString("{'hello':'world'}").Bytes()
   153  	t.req = t.requestWithBytes(c, jsonBytes)
   154  	common.SignWithPassphrase(t.req, adminSecret)
   155  	c.Assert(
   156  		t.getResponse(t.req).Code,
   157  		Equals,
   158  		http.StatusBadRequest,
   159  	)
   160  }
   161  
   162  func (t *RepoListCreateTests) TestRepositoryError(c *C) {
   163  	os.RemoveAll(t.repos)
   164  	t.addPubKey(c)
   165  	common.SignWithPassphrase(t.req, adminSecret)
   166  	c.Assert(
   167  		t.getResponse(t.req).Code,
   168  		Equals,
   169  		http.StatusInternalServerError,
   170  	)
   171  }