~evanj/cms

ref: 27de4d9c907e2258a828fbfcda8f22b910df0f63 cms/internal/c/content/content_test.go -rw-r--r-- 1.7 KiB
27de4d9cEvan M Jones Feat(spaces): Added the ability to copy an entire space. 1 year, 3 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
package content_test

import (
	"bytes"
	"io/ioutil"
	"log"
	"mime/multipart"
	"net/http"
	"net/http/httptest"
	"os"
	"testing"

	"git.sr.ht/~evanj/cms/internal/c/content"
	"github.com/bmizerany/assert"
	gomock "github.com/golang/mock/gomock"
)

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

	routes := []string{
		"/content/new",
		"/content/delete",
		"/content/search",
		"/content/update",
	}

	ctrl := gomock.NewController(t)
	defer ctrl.Finish()

	c := content.New(
		log.New(bytes.NewBufferString(os.DevNull), "", 0),
		content.NewMockDBer(ctrl),
		content.NewMockE3er(ctrl),
		content.NewMockHooker(ctrl),
	)

	for _, route := range routes {
		// Create body of post request.
		requestBody := bytes.Buffer{}
		formwriter := multipart.NewWriter(&requestBody)

		// Add a field.
		fieldwriter, _ := formwriter.CreateFormField("access")
		fieldwriter.Write([]byte("public"))
		defer formwriter.Close()

		// Send req.
		req, _ := http.NewRequest(http.MethodPost, route, &requestBody)

		// Read req.
		w := httptest.NewRecorder()
		c.ServeHTTP(w, req)
		resp := w.Result()
		bod, _ := ioutil.ReadAll(resp.Body)

		assert.Equal(t, content.ErrNoLogin.Error(), string(bod))
		assert.NotEqual(t, http.StatusOK, resp.StatusCode)
	}
}

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

	ctrl := gomock.NewController(t)
	defer ctrl.Finish()

	c := content.New(
		log.New(bytes.NewBufferString(os.DevNull), "", 0),
		content.NewMockDBer(ctrl),
		content.NewMockE3er(ctrl),
		content.NewMockHooker(ctrl),
	)

	// Send req.
	req, _ := http.NewRequest(http.MethodGet, "/content/whatever", nil)

	// Read req.
	w := httptest.NewRecorder()
	c.ServeHTTP(w, req)
	resp := w.Result()
	assert.Equal(t, http.StatusNotFound, resp.StatusCode)
}