~evanj/cms

ref: 7c487cb4cca6290eae5f854300d6e9c78f81ea25 cms/internal/c/doc/doc_test.go -rw-r--r-- 2.0 KiB
7c487cb4Evan J Feat(dynamic): Dynamic content pages have been added (rather than 9 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package doc_test

import (
	"errors"
	"net/http"
	"net/http/httptest"
	"testing"

	"git.sr.ht/~evanj/cms/internal/c"
	"git.sr.ht/~evanj/cms/internal/c/doc"
	userT "git.sr.ht/~evanj/cms/internal/m/user"
	"github.com/bmizerany/assert"
	gomock "github.com/golang/mock/gomock"
)

var ctx = gomock.Any()

func TestWithoutUser(t *testing.T) {
	t.Parallel()

	var (
		ctrl = gomock.NewController(t)
		db   = doc.NewMockdber(ctrl)
		s    = doc.New(c.New(nil, db, true, "test"), nil)
		ts   = httptest.NewServer(s)
	)

	res, err := http.Get(ts.URL + "/page/doc")
	assert.Equal(t, nil, err)
	assert.Equal(t, res.StatusCode, http.StatusOK)
}

func TestWithUser(t *testing.T) {
	t.Parallel()

	var (
		ctrl = gomock.NewController(t)
		db   = doc.NewMockdber(ctrl)
		s    = doc.New(c.New(nil, db, true, "test"), nil)
		ts   = httptest.NewServer(s)
	)

	db.EXPECT().UserGet(ctx, "user", "pass").Return(userT.NewMock("", ""), nil).AnyTimes()

	req, _ := http.NewRequest("GET", ts.URL+"/page/doc", nil)
	req.SetBasicAuth("user", "pass")
	res, _ := http.DefaultClient.Do(req)
	assert.Equal(t, res.StatusCode, http.StatusOK)
}

func TestWithInvalidUserToken(t *testing.T) {
	t.Parallel()

	var (
		ctrl = gomock.NewController(t)
		db   = doc.NewMockdber(ctrl)
		s    = doc.New(c.New(nil, db, true, "test"), nil)
		ts   = httptest.NewServer(s)
		u    = userT.NewMock("", "")
		tok  = u.Token()
		err  = errors.New("placeholder")
	)

	db.EXPECT().UserGetFromToken(ctx, tok).Return(nil, err).AnyTimes()

	// Shouldn't error.
	req, _ := http.NewRequest("GET", ts.URL+"/page/doc", nil)
	req.AddCookie(&http.Cookie{
		Name:  c.KeyUserLogin,
		Value: tok,
	})
	res, _ := http.DefaultClient.Do(req)
	assert.Equal(t, res.StatusCode, http.StatusOK)
}

func TestNotFound(t *testing.T) {
	t.Parallel()

	var (
		ctrl = gomock.NewController(t)
		db   = doc.NewMockdber(ctrl)
		s    = doc.New(c.New(nil, db, true, "test"), nil)
		ts   = httptest.NewServer(s)
	)

	res, _ := http.Get(ts.URL + "/page/aye,aye,captain")
	assert.Equal(t, res.StatusCode, http.StatusNotFound)
}