~evanj/cms

ref: 8a453a73244ce94c291204147a7ee2cbf61f7ab2 cms/internal/c/space/space_test.go -rw-r--r-- 8.9 KiB
8a453a73Evan M Jones Fix(testing): Refactor'd tests per RBAC feat breakage. All tests 5 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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
package space_test

import (
	"bytes"
	"io/ioutil"
	"log"
	"net/http"
	"net/http/httptest"
	"net/url"
	"os"
	"strings"
	"testing"

	"git.sr.ht/~evanj/cms/internal/c"
	"git.sr.ht/~evanj/cms/internal/c/space"
	"git.sr.ht/~evanj/cms/internal/m/contenttype"
	"git.sr.ht/~evanj/cms/internal/m/hook"
	spaceT "git.sr.ht/~evanj/cms/internal/m/space"
	userT "git.sr.ht/~evanj/cms/internal/m/user"
	"github.com/bmizerany/assert"
	"github.com/golang/mock/gomock"
	"github.com/google/uuid"
)

var client = &http.Client{
	CheckRedirect: func(req *http.Request, via []*http.Request) error {
		return http.ErrUseLastResponse
	},
}

func emptyreq(ts *httptest.Server, method string) (*http.Response, error) {
	req, err := newrequest(method, ts.URL, nil)
	if err != nil {
		return nil, err
	}
	return client.Do(req)
}

type FakeContentTypeList struct{}

func fakecontenttypelist() FakeContentTypeList                    { return FakeContentTypeList{} }
func (_ FakeContentTypeList) List() (r []contenttype.ContentType) { return r }
func (_ FakeContentTypeList) More() bool                          { return false }
func (_ FakeContentTypeList) Before() int                         { return 0 }

type FakeHookList struct{}

func fakehooklist() FakeHookList             { return FakeHookList{} }
func (_ FakeHookList) List() (r []hook.Hook) { return r }
func (_ FakeHookList) More() bool            { return false }
func (_ FakeHookList) Before() int           { return 0 }

func newrequest(method, url string, bod url.Values) (*http.Request, error) {
	// Fake the method. This is used for two reasons.
	// 1. On the frontend we use a hidden method field in form to mock/use REST.
	// 		It ends up making cURL use much more ergonomic.
	// 2. When testing, net/http doesn't read full body of DELETE/PUT. Incredibly
	// 		annoying.
	if bod != nil {
		bod.Add("method", method)
	}
	return http.NewRequest("POST", url, strings.NewReader(bod.Encode()))
}

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

	var (
		ctrl    = gomock.NewController(t)
		db      = space.NewMockdber(ctrl)
		l       *log.Logger
		s       = space.New(c.New(l, db, true, "test"), l, db)
		ts      = httptest.NewServer(s)
		methods = []string{"GET", "POST", "PUT", "PATCH", "DELETE"}
	)

	for _, method := range methods {
		res, err := emptyreq(ts, method)
		assert.Equal(t, err, nil)

		bytes, err := ioutil.ReadAll(res.Body)
		assert.Equal(t, err, nil)
		assert.Equal(t, true, strings.Contains(string(bytes), c.ErrNoLogin.Error()))
	}
}

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

	var (
		uname = "test"
		upass = "test"
		uItem = userT.NewMock(uname, upass)

		sname = uuid.New().String()
		sdesc = uuid.New().String()
		sItem = spaceT.NewMock(sname, sdesc, "")

		snameUpdate = uuid.New().String()
		sdescUpdate = uuid.New().String()
		sItemUpdate = spaceT.NewMock(snameUpdate, sdescUpdate, "")

		snameCopy = uuid.New().String()
		sdescCopy = uuid.New().String()
		sItemCopy = spaceT.NewMock(snameCopy, sdescCopy, "")

		contentTypeList = fakecontenttypelist()
		hookList        = fakehooklist()
	)

	type SpaceTest struct {
		createSC int
		create   func(*space.Mockdber)
		getSC    int
		get      func(*space.Mockdber)
		updateSC int
		update   func(*space.Mockdber)
		copySC   int
		copy     func(*space.Mockdber)
		deleteSC int
		delete   func(*space.Mockdber)
	}

	tests := []SpaceTest{

		SpaceTest{
			createSC: http.StatusFound,
			create: func(db *space.Mockdber) {
				db.EXPECT().UserGet(uname, upass).Return(uItem, nil).AnyTimes()
				db.EXPECT().SpaceNew(uItem, sname, sdesc).Return(sItem, nil).AnyTimes()
			},
			getSC: http.StatusOK,
			get: func(db *space.Mockdber) {
				db.EXPECT().UserGet(uname, upass).Return(uItem, nil).AnyTimes()
				db.EXPECT().SpaceGet(uItem, sItem.ID()).Return(sItem, nil).AnyTimes()
				db.EXPECT().ContentTypesPerSpace(uItem, sItem, 0).Return(contentTypeList, nil).AnyTimes()
				db.EXPECT().HooksPerSpace(uItem, sItem, 0).Return(hookList, nil).AnyTimes()
			},
			updateSC: http.StatusFound,
			update: func(db *space.Mockdber) {
				db.EXPECT().UserGet(uname, upass).Return(uItem, nil).AnyTimes()
				db.EXPECT().SpaceGet(uItem, sItem.ID()).Return(sItem, nil).AnyTimes()
				db.EXPECT().SpaceUpdate(uItem, sItem, snameUpdate, sdescUpdate).Return(sItemUpdate, nil).AnyTimes()
			},
			copySC: http.StatusFound,
			copy: func(db *space.Mockdber) {
				db.EXPECT().UserGet(uname, upass).Return(uItem, nil).AnyTimes()
				db.EXPECT().SpaceGet(uItem, sItemUpdate.ID()).Return(sItemUpdate, nil).AnyTimes()
				db.EXPECT().SpaceCopy(uItem, sItemUpdate, snameCopy, sdescCopy).Return(sItemCopy, nil).AnyTimes()
			},
			deleteSC: http.StatusFound,
			delete: func(db *space.Mockdber) {
				db.EXPECT().UserGet(uname, upass).Return(uItem, nil).AnyTimes()
				db.EXPECT().SpaceGet(uItem, sItem.ID()).Return(sItem, nil).AnyTimes()
				db.EXPECT().SpaceDelete(uItem, sItem).Return(nil).AnyTimes()
			},
		},

		SpaceTest{
			createSC: http.StatusBadRequest,
			create: func(db *space.Mockdber) {
				db.EXPECT().UserGet(uname, upass).Return(nil, c.ErrNoLogin).AnyTimes()
			},
			getSC: http.StatusBadRequest,
			get: func(db *space.Mockdber) {
				db.EXPECT().UserGet(uname, upass).Return(uItem, nil).AnyTimes()
			},
			updateSC: http.StatusBadRequest,
			update: func(db *space.Mockdber) {
				db.EXPECT().UserGet(uname, upass).Return(uItem, nil).AnyTimes()
			},
			copySC: http.StatusBadRequest,
			copy: func(db *space.Mockdber) {
				db.EXPECT().UserGet(uname, upass).Return(uItem, nil).AnyTimes()
			},
			deleteSC: http.StatusBadRequest,
			delete: func(db *space.Mockdber) {
				db.EXPECT().UserGet(uname, upass).Return(uItem, nil).AnyTimes()
			},
		},

		SpaceTest{
			createSC: http.StatusBadRequest,
			create: func(db *space.Mockdber) {
				db.EXPECT().UserGet(uname, upass).Return(uItem, nil).AnyTimes()
				db.EXPECT().SpaceNew(uItem, sname, sdesc).Return(nil, space.ErrNoSpace).AnyTimes()
			},
			getSC: http.StatusNotFound,
			get: func(db *space.Mockdber) {
				db.EXPECT().UserGet(uname, upass).Return(uItem, nil).AnyTimes()
				db.EXPECT().SpaceGet(uItem, sItem.ID()).Return(nil, space.ErrNoSpace).AnyTimes()
			},
			updateSC: http.StatusNotFound,
			update: func(db *space.Mockdber) {
				db.EXPECT().UserGet(uname, upass).Return(uItem, nil).AnyTimes()
				db.EXPECT().SpaceGet(uItem, sItem.ID()).Return(nil, space.ErrNoSpace).AnyTimes()
			},
			copySC: http.StatusNotFound,
			copy: func(db *space.Mockdber) {
				db.EXPECT().UserGet(uname, upass).Return(uItem, nil).AnyTimes()
				db.EXPECT().SpaceGet(uItem, sItemUpdate.ID()).Return(nil, space.ErrNoSpace).AnyTimes()
			},
			deleteSC: http.StatusNotFound,
			delete: func(db *space.Mockdber) {
				db.EXPECT().UserGet(uname, upass).Return(uItem, nil).AnyTimes()
				db.EXPECT().SpaceGet(uItem, sItem.ID()).Return(nil, space.ErrNoSpace).AnyTimes()
			},
		},
	}

	for _, test := range tests {
		// NOTE: If we run each iteration in their own t.Run it kills test
		// coverage...

		var (
			ctrl = gomock.NewController(t)
			db   = space.NewMockdber(ctrl)
			l    = log.New(bytes.NewBufferString(os.DevNull), "", 0)
			s    = space.New(c.New(l, db, true, "test"), l, db)
			ts   = httptest.NewServer(s)
		)

		// Create
		{
			test.create(db)

			req, _ := newrequest("POST", ts.URL, url.Values{"name": []string{sname}, "desc": []string{sdesc}})
			req.SetBasicAuth(uname, upass)
			req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
			req.Header.Set("Accept", "text/html")

			res, _ := client.Do(req)
			assert.Equal(t, res.StatusCode, test.createSC)
		}

		// Get
		{
			test.get(db)

			req, _ := newrequest("GET", ts.URL, url.Values{"space": []string{sItem.ID()}})
			req.SetBasicAuth(uname, upass)
			req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
			req.Header.Set("Accept", "text/html")

			res, _ := client.Do(req)
			assert.Equal(t, res.StatusCode, test.getSC)
		}

		// Update
		{
			test.update(db)

			req, _ := newrequest("PATCH", ts.URL,
				url.Values{
					"space": []string{sItem.ID()},
					"name":  []string{snameUpdate},
					"desc":  []string{sdescUpdate}})

			req.SetBasicAuth(uname, upass)
			req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
			req.Header.Set("Accept", "text/html")

			res, _ := client.Do(req)
			assert.Equal(t, res.StatusCode, test.updateSC)
		}

		// Copy
		{
			test.copy(db)

			req, _ := newrequest("PUT", ts.URL,
				url.Values{
					"space": []string{sItemUpdate.ID()},
					"name":  []string{snameCopy},
					"desc":  []string{sdescCopy}})
			req.SetBasicAuth(uname, upass)
			req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
			req.Header.Set("Accept", "text/html")

			res, _ := client.Do(req)
			assert.Equal(t, res.StatusCode, test.copySC)
		}

		// Delete
		{
			test.delete(db)

			req, _ := newrequest("DELETE", ts.URL,
				url.Values{
					"space":  []string{sItem.ID()},
					"method": []string{"DELETE"},
				})

			req.SetBasicAuth(uname, upass)
			req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
			req.Header.Set("Accept", "text/html")

			res, _ := client.Do(req)
			assert.Equal(t, res.StatusCode, test.deleteSC)
		}
	}
}