<commit_before>package api

import (
	"net/http"

	. "gopkg.in/check.v1"

	"github.com/hoffie/larasync/repository"
)

type NIBPutTest struct {
	NIBItemTest
}

var _ = Suite(&NIBPutTest{getNIBItemTest()})

func (t *NIBPutTest) SetUpTest(c *C) {
	t.NIBItemTest.SetUpTest(c)
	t.httpMethod = "PUT"
	t.req = t.requestWithBytes(c, t.signNIBBytes(c, t.getTestNIBBytes()))
	t.createRepository(c)
}

func (t *NIBPutTest) TestUnauthorized(c *C) {
	resp := t.getResponse(t.req)
	c.Assert(resp.Code, Equals, http.StatusUnauthorized)
}

func (t *NIBPutTest) TestRepositoryNotExisting(c *C) {
	t.repositoryName = "does-not-exist"
	t.req = t.requestEmptyBody(c)
	t.signRequest()
	resp := t.getResponse(t.req)
	c.Assert(resp.Code, Equals, http.StatusUnauthorized)
}

func (t *NIBPutTest) TestPutMalformedSignature(c *C) {
	data := t.getTestNIBBytes()
	data = t.signNIBBytes(c, data)
	data[len(data)-1] = 0
	t.req = t.requestWithBytes(c, data)
	t.signRequest()

	resp := t.getResponse(t.req)

	c.Assert(resp.Code, Equals, http.StatusUnauthorized)
	repo := t.getRepository(c)
	c.Assert(repo.HasNIB(t.nibID), Equals, false)
}

func (t *NIBPutTest) TestPutMalformedData(c *C) {
	data := t.getTestNIBBytes()
	data[0] = 0
	data = t.signNIBBytes(c, data)
	t.req = t.requestWithBytes(c, data)
	t.signRequest()

	resp := t.getResponse(t.req)

	c.Assert(resp.Code, Equals, http.StatusBadRequest)
	repo := t.getRepository(c)
	c.Assert(repo.HasNIB(t.nibID), Equals, false)
}

func (t *NIBPutTest) TestPutNew(c *C) {
	t.signRequest()
	resp := t.getResponse(t.req)
	c.Assert(resp.Code, Equals, http.StatusCreated)
}

func (t *NIBPutTest) TestPutNewStore(c *C) {
	t.signRequest()
	t.getResponse(t.req)
	r := t.getRepository(c)

	nib, err := r.GetNIB(t.nibID)
	c.Assert(err, IsNil)
	c.Assert(nib.ID, Equals, t.nibID)
}
<commit_msg>api: Added tests for the precondition checks.<commit_after>package api

import (
	"net/http"

	. "gopkg.in/check.v1"

	"github.com/hoffie/larasync/repository"
)

type NIBPutTest struct {
	NIBItemTest
}

var _ = Suite(&NIBPutTest{getNIBItemTest()})

func (t *NIBPutTest) SetUpTest(c *C) {
	t.NIBItemTest.SetUpTest(c)
	t.httpMethod = "PUT"
	t.req = t.requestWithBytes(c, t.signNIBBytes(c, t.getTestNIBBytes()))
	t.createRepository(c)
}

func (t *NIBPutTest) TestUnauthorized(c *C) {
	resp := t.getResponse(t.req)
	c.Assert(resp.Code, Equals, http.StatusUnauthorized)
}

func (t *NIBPutTest) TestRepositoryNotExisting(c *C) {
	t.repositoryName = "does-not-exist"
	t.req = t.requestEmptyBody(c)
	t.signRequest()
	resp := t.getResponse(t.req)
	c.Assert(resp.Code, Equals, http.StatusUnauthorized)
}

func (t *NIBPutTest) TestPutMalformedSignature(c *C) {
	data := t.getTestNIBBytes()
	data = t.signNIBBytes(c, data)
	data[len(data)-1] = 0
	t.req = t.requestWithBytes(c, data)
	t.signRequest()

	resp := t.getResponse(t.req)

	c.Assert(resp.Code, Equals, http.StatusUnauthorized)
	repo := t.getRepository(c)
	c.Assert(repo.HasNIB(t.nibID), Equals, false)
}

func (t *NIBPutTest) TestPutMalformedData(c *C) {
	data := t.getTestNIBBytes()
	data[0] = 0
	data = t.signNIBBytes(c, data)
	t.req = t.requestWithBytes(c, data)
	t.signRequest()

	resp := t.getResponse(t.req)

	c.Assert(resp.Code, Equals, http.StatusBadRequest)
	repo := t.getRepository(c)
	c.Assert(repo.HasNIB(t.nibID), Equals, false)
}

func (t *NIBPutTest) TestPutNew(c *C) {
	t.signRequest()
	resp := t.getResponse(t.req)
	c.Assert(resp.Code, Equals, http.StatusCreated)
}

func (t *NIBPutTest) TestPutNewStore(c *C) {
	t.signRequest()
	t.getResponse(t.req)
	r := t.getRepository(c)

	nib, err := r.GetNIB(t.nibID)
	c.Assert(err, IsNil)
	c.Assert(nib.ID, Equals, t.nibID)
}

func (t *NIBPutTest) TestPutNewPrecondition(c *C) {
	t.addTestNIB(c)
	header := t.req.Header
	rep := t.getRepository(c)
	transaction, err := rep.CurrentTransaction()
	c.Assert(err, IsNil)
	header.Set("If-Match", transaction.IDString())
	t.signRequest()

	resp := t.getResponse(t.req)
	c.Assert(resp.Code, Equals, http.StatusOK)
}

func (t *NIBPutTest) TestPutNewPreconditionFailed(c *C) {
	t.addTestNIB(c)
	header := t.req.Header
	rep := t.getRepository(c)
	transaction, err := rep.CurrentTransaction()
	c.Assert(err, IsNil)
	header.Set("If-Match", transaction.PreviousIDString())
	t.signRequest()

	resp := t.getResponse(t.req)
	c.Assert(resp.Code, Equals, http.StatusPreconditionFailed)
}

{'fraction_non_alphanumeric': 0.16381304951411382, 'fraction_numerical': 0.0023137436372049976, 'mean_word_length': 4.873641304347826, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '874589', 'n_tokens_mistral': 1864, 'n_tokens_neox': 1840, 'n_words': 312}