api_test.go 8.88 KB
Newer Older
Lukas Böhm's avatar
Lukas Böhm committed
1
2
3
package main

import (
Lukas Böhm's avatar
Lukas Böhm committed
4
	"bytes"
Lukas Böhm's avatar
Lukas Böhm committed
5
	"encoding/json"
Lukas Böhm's avatar
Lukas Böhm committed
6
	"fmt"
Lukas Böhm's avatar
Lukas Böhm committed
7
	"github.com/google/uuid"
Lukas Böhm's avatar
Lukas Böhm committed
8
	"github.com/gorilla/mux"
Lukas Böhm's avatar
Lukas Böhm committed
9
	"github.com/stretchr/testify/assert"
Lukas Böhm's avatar
Lukas Böhm committed
10
	"io"
Lukas Böhm's avatar
Lukas Böhm committed
11
	"io/ioutil"
Lukas Böhm's avatar
Lukas Böhm committed
12
	"log"
Lukas Böhm's avatar
Lukas Böhm committed
13
	"mime/multipart"
Lukas Böhm's avatar
Lukas Böhm committed
14
15
	"net/http"
	"net/http/httptest"
Lukas Böhm's avatar
Lukas Böhm committed
16
17
	"os"
	"path/filepath"
Lukas Böhm's avatar
Lukas Böhm committed
18
	"strings"
Lukas Böhm's avatar
Lukas Böhm committed
19
	"testing"
Lukas Böhm's avatar
Lukas Böhm committed
20
21
)

Lukas Böhm's avatar
Lukas Böhm committed
22
// DATA
Lukas Böhm's avatar
Lukas Böhm committed
23
var shares = []Share{
Lukas Böhm's avatar
Lukas Böhm committed
24
	{
Lukas Böhm's avatar
Lukas Böhm committed
25
26
		ID:            uuid.MustParse("5713d228-a042-446d-a5e4-183b19fa832a"),
		Name:          "TestFinalPrivate",
Lukas Böhm's avatar
Lukas Böhm committed
27
		DownloadLimit: 100,
Lukas Böhm's avatar
Lukas Böhm committed
28
29
30
31
		IsPublic:      false,
		IsTemporary:   false,
		Password:      "test123",
		Emails:        []string{""},
Lukas Böhm's avatar
Lukas Böhm committed
32

Lukas Böhm's avatar
Lukas Böhm committed
33
		Attachments: []Attachment{
Lukas Böhm's avatar
Lukas Böhm committed
34
			{
Lukas Böhm's avatar
Lukas Böhm committed
35
36
37
				ID:          uuid.MustParse("913134c0-894f-4c4d-b545-92ec373168b1"),
				Filename:    "kekw.txt",
				Filesize:    123456,
Lukas Böhm's avatar
Lukas Böhm committed
38
				IsEncrypted: false,
Lukas Böhm's avatar
Lukas Böhm committed
39
				ShareID:     uuid.MustParse("5713d228-a042-446d-a5e4-183b19fa832a"),
Lukas Böhm's avatar
Lukas Böhm committed
40
41
			},
		},
Lukas Böhm's avatar
Lukas Böhm committed
42
43
	},
	{
Lukas Böhm's avatar
Lukas Böhm committed
44
45
46
		ID:          uuid.MustParse("f43b0e48-13cc-4c6c-8a23-3a18a670effd"),
		Name:        "TestFinalPublic",
		IsPublic:    true,
Lukas Böhm's avatar
Lukas Böhm committed
47
		IsTemporary: false,
Lukas Böhm's avatar
Lukas Böhm committed
48
		Emails:      []string{""},
Lukas Böhm's avatar
Lukas Böhm committed
49
50
	},
	{
Lukas Böhm's avatar
Lukas Böhm committed
51
52
		ID:            uuid.MustParse("a558aca3-fb40-400b-8dc6-ae49c705c791"),
		Name:          "TestTemporary",
Lukas Böhm's avatar
Lukas Böhm committed
53
		DownloadLimit: 300,
Lukas Böhm's avatar
Lukas Böhm committed
54
55
56
		IsPublic:      true,
		IsTemporary:   true,
		Emails:        []string{""},
Lukas Böhm's avatar
Lukas Böhm committed
57
58
59
60
61
62
63
64
65
	},
}

func Reset() {
	db, err := GetDatabase()
	if err != nil {
		log.Fatal("database brok")
	}
	db.AutoMigrate(&Share{})
Lukas Böhm's avatar
Lukas Böhm committed
66
	db.AutoMigrate(&Attachment{})
Lukas Böhm's avatar
Lukas Böhm committed
67
	// delete everything
Lukas Böhm's avatar
Lukas Böhm committed
68
69
	db.Where("1 = 1").Delete(&Share{})
	db.Where("1 = 1").Delete(&Attachment{})
Lukas Böhm's avatar
Lukas Böhm committed
70
71
	os.RemoveAll(filepath.Join(config.mediaDir, "data"))
	os.RemoveAll(filepath.Join(config.mediaDir, "temp"))
Lukas Böhm's avatar
Lukas Böhm committed
72
	// create everything
Lukas Böhm's avatar
Lukas Böhm committed
73
74
75
	for _, sh := range shares {
		db.Create(&sh)
	}
Lukas Böhm's avatar
Lukas Böhm committed
76
77
	os.MkdirAll(filepath.Join(config.mediaDir, "data"), os.ModePerm)
	os.MkdirAll(filepath.Join(config.mediaDir, "temp"), os.ModePerm)
Lukas Böhm's avatar
Lukas Böhm committed
78
79
	// testfiles
	ioutil.WriteFile(filepath.Join(config.mediaDir, "data", shares[0].ID.String(), shares[0].Attachments[0].ID.String()), []byte("KEKW KEKW KEKW"), os.ModePerm)
Lukas Böhm's avatar
Lukas Böhm committed
80
81
}

Lukas Böhm's avatar
Lukas Böhm committed
82
83
84
/////////////////////////////////////
/////////////// TEST ////////////////
/////////////////////////////////////
Lukas Böhm's avatar
Lukas Böhm committed
85
86
func TestAllShares(t *testing.T) {
	Reset()
Lukas Böhm's avatar
Lukas Böhm committed
87
88
89
90
	r := mux.NewRouter()
	r.Handle("/shares", endpointREST(AllShares)).Methods("GET")
	ts := httptest.NewServer(r)
	defer ts.Close()
Lukas Böhm's avatar
Lukas Böhm committed
91

Lukas Böhm's avatar
Lukas Böhm committed
92
	t.Run("happy path", func(t *testing.T) {
Lukas Böhm's avatar
Lukas Böhm committed
93
		res, _ := http.Get(ts.URL + "/shares")
Lukas Böhm's avatar
Lukas Böhm committed
94
		body, _ := ioutil.ReadAll(res.Body)
Lukas Böhm's avatar
Lukas Böhm committed
95
		var erg []Share
Lukas Böhm's avatar
Lukas Böhm committed
96
		json.Unmarshal(body, &erg)
Lukas Böhm's avatar
Lukas Böhm committed
97
98
		assert.EqualValues(t, http.StatusOK, res.StatusCode)
		assert.EqualValues(t, shares[1], erg[0])
Lukas Böhm's avatar
Lukas Böhm committed
99
100
101
102
103
	})
}

func TestGetShare(t *testing.T) {
	Reset()
Lukas Böhm's avatar
Lukas Böhm committed
104
105
106
107
	router := mux.NewRouter()
	ts := httptest.NewServer(router)
	router.Handle("/share/{id}", endpointREST(GetShare)).Methods("GET")
	defer ts.Close()
Lukas Böhm's avatar
Lukas Böhm committed
108

Lukas Böhm's avatar
Lukas Böhm committed
109
110
	t.Run("happy path", func(t *testing.T) {
		header := map[string][]string{
Lukas Böhm's avatar
Lukas Böhm committed
111
			"Authorization": {"Basic NTcxM2QyMjgtYTA0Mi00NDZkLWE1ZTQtMTgzYjE5ZmE4MzJhOnRlc3QxMjM="}, // pw: test123
Lukas Böhm's avatar
Lukas Böhm committed
112
		}
Lukas Böhm's avatar
Lukas Böhm committed
113
		req, _ := http.NewRequest("GET", fmt.Sprintf("%s/share/%s", ts.URL, shares[0].ID.String()), nil)
Lukas Böhm's avatar
Lukas Böhm committed
114
115
116
117
118
119
120
121
122
123
124
125
126
		req.Header = header
		res, _ := http.DefaultClient.Do(req)

		body, _ := ioutil.ReadAll(res.Body)
		var actual Share
		var expected Share

		json.Unmarshal(body, &actual)
		ex, _ := json.Marshal(shares[0])
		json.Unmarshal(ex, &expected)

		assert.Equal(t, http.StatusOK, res.StatusCode)
		assert.Equal(t, expected, actual)
Lukas Böhm's avatar
Lukas Böhm committed
127
	})
Lukas Böhm's avatar
Lukas Böhm committed
128

Lukas Böhm's avatar
Lukas Böhm committed
129
	t.Run("unauthorized", func(t *testing.T) {
Lukas Böhm's avatar
Lukas Böhm committed
130
		req, _ := http.NewRequest("GET", fmt.Sprintf("%s/share/%s", ts.URL, shares[0].ID.String()), nil)
Lukas Böhm's avatar
Lukas Böhm committed
131
132
		res, _ := http.DefaultClient.Do(req)
		assert.Equal(t, http.StatusUnauthorized, res.StatusCode)
Lukas Böhm's avatar
Lukas Böhm committed
133
	})
Lukas Böhm's avatar
Lukas Böhm committed
134

Lukas Böhm's avatar
Lukas Böhm committed
135
	t.Run("not found", func(t *testing.T) {
Lukas Böhm's avatar
Lukas Böhm committed
136
		req, _ := http.NewRequest("GET", fmt.Sprintf("%s/share/4015a76b-09d0-402b-814f-bd9fa48ce8e1", ts.URL), nil)
Lukas Böhm's avatar
Lukas Böhm committed
137
138
		res, _ := http.DefaultClient.Do(req)
		assert.Equal(t, http.StatusNotFound, res.StatusCode)
Lukas Böhm's avatar
Lukas Böhm committed
139
	})
Lukas Böhm's avatar
Lukas Böhm committed
140

Lukas Böhm's avatar
Lukas Böhm committed
141
	t.Run("not ready", func(t *testing.T) {
Lukas Böhm's avatar
Lukas Böhm committed
142
		req, _ := http.NewRequest("GET", fmt.Sprintf("%s/share/%s", ts.URL, shares[2].ID.String()), nil)
Lukas Böhm's avatar
Lukas Böhm committed
143
144
		res, _ := http.DefaultClient.Do(req)
		assert.Equal(t, http.StatusForbidden, res.StatusCode)
Lukas Böhm's avatar
Lukas Böhm committed
145
	})
Lukas Böhm's avatar
Lukas Böhm committed
146
147
}

Lukas Böhm's avatar
Lukas Böhm committed
148
func TestDownloadFile(t *testing.T) {
Lukas Böhm's avatar
Lukas Böhm committed
149
150
151
	Reset()
	router := mux.NewRouter()
	ts := httptest.NewServer(router)
Lukas Böhm's avatar
Lukas Böhm committed
152
	router.Handle("/share/{id}/attachment/{att}", endpointREST(DownloadFile)).Methods("GET")
Lukas Böhm's avatar
Lukas Böhm committed
153
154
	defer ts.Close()

Lukas Böhm's avatar
Lukas Böhm committed
155
	t.Run("happy path", func(t *testing.T) {
Lukas Böhm's avatar
Lukas Böhm committed
156
157
		req, _ := http.NewRequest("GET", fmt.Sprintf("%s/share/%s/attachment/%s", ts.URL, shares[0].ID.String(), shares[0].Attachments[0].ID.String()), nil)
		req.Header = map[string][]string{
Lukas Böhm's avatar
Lukas Böhm committed
158
			"Authorization": {"Basic NTcxM2QyMjgtYTA0Mi00NDZkLWE1ZTQtMTgzYjE5ZmE4MzJhOnRlc3QxMjM="}, // pw: test123
Lukas Böhm's avatar
Lukas Böhm committed
159
160
161
		}
		res, _ := http.DefaultClient.Do(req)
		body, _ := ioutil.ReadAll(res.Body)
Lukas Böhm's avatar
Lukas Böhm committed
162
163
		assert.FileExists(t, filepath.Join(config.mediaDir, "data", shares[0].ID.String(), shares[0].Attachments[0].ID.String()))
		expected, _ := ioutil.ReadFile(filepath.Join(config.mediaDir, "data", shares[0].ID.String(), shares[0].Attachments[0].ID.String()))
Lukas Böhm's avatar
Lukas Böhm committed
164
		assert.EqualValues(t, expected, body)
Lukas Böhm's avatar
Lukas Böhm committed
165
166
		assert.EqualValues(t, http.StatusOK, res.StatusCode)
	})
Lukas Böhm's avatar
Lukas Böhm committed
167

Lukas Böhm's avatar
Lukas Böhm committed
168
169
170
171
172
	t.Run("unauthorized", func(t *testing.T) {
		req, _ := http.NewRequest("GET", fmt.Sprintf("%s/share/%s/attachment/%s", ts.URL, shares[0].ID.String(), shares[0].Attachments[0].ID.String()), nil)
		res, _ := http.DefaultClient.Do(req)
		assert.EqualValues(t, http.StatusUnauthorized, res.StatusCode)
	})
Lukas Böhm's avatar
Lukas Böhm committed
173

Lukas Böhm's avatar
Lukas Böhm committed
174
	t.Run("not found", func(t *testing.T) {
Lukas Böhm's avatar
Lukas Böhm committed
175
		req, _ := http.NewRequest("GET", fmt.Sprintf("%s/share/%s/attachment/%s", ts.URL, shares[0].ID.String(), "0dd9a011-612b-4f33-99c0-bfd687021014"), nil)
Lukas Böhm's avatar
Lukas Böhm committed
176
177
		res, _ := http.DefaultClient.Do(req)
		assert.EqualValues(t, http.StatusNotFound, res.StatusCode)
Lukas Böhm's avatar
Lukas Böhm committed
178
179
180
	})
}

Lukas Böhm's avatar
Lukas Böhm committed
181
182
183
184
185
186
187
188
func TestOpenShare(t *testing.T) {
	Reset()
	router := mux.NewRouter()
	ts := httptest.NewServer(router)
	router.Handle("/shares", endpointREST(OpenShare)).Methods("POST")
	defer ts.Close()

	t.Run("happy path", func(t *testing.T) {
Lukas Böhm's avatar
Lukas Böhm committed
189
190
191
192
193
194
		var newShare = Share{
			ID:            uuid.MustParse("e5134044-2704-4864-85be-318fb158009f"),
			Name:          "TestOpenShare",
			Expires:       nil,
			DownloadLimit: 69,
			IsPublic:      false,
Lukas Böhm's avatar
Lukas Böhm committed
195
			Attachments: []Attachment{
Lukas Böhm's avatar
Lukas Böhm committed
196
				{
Lukas Böhm's avatar
Lukas Böhm committed
197
198
199
					ID:          uuid.MustParse("2b524827-9c3c-47e0-9277-8b51fd45b4bd"),
					Filename:    "hallo.txt",
					Filesize:    123456,
Lukas Böhm's avatar
Lukas Böhm committed
200
					IsEncrypted: false,
Lukas Böhm's avatar
Lukas Böhm committed
201
					ShareID:     uuid.MustParse("e5134044-2704-4864-85be-318fb158009f"),
Lukas Böhm's avatar
Lukas Böhm committed
202
203
204
205
				},
			},
		}
		b, _ := json.Marshal(newShare)
Lukas Böhm's avatar
Lukas Böhm committed
206
		req, _ := http.NewRequest("POST", ts.URL+"/shares", strings.NewReader(string(b)))
Lukas Böhm's avatar
Lukas Böhm committed
207
		res, _ := http.DefaultClient.Do(req)
Lukas Böhm's avatar
Lukas Böhm committed
208
209
210
211
		body, _ := ioutil.ReadAll(res.Body)
		var actual Share
		json.Unmarshal(body, &actual)
		// checks
Lukas Böhm's avatar
Lukas Böhm committed
212
		assert.Equal(t, http.StatusOK, res.StatusCode)
Lukas Böhm's avatar
Lukas Böhm committed
213
214
215
216
217
		assert.DirExists(t, filepath.Join(config.mediaDir, "temp", newShare.ID.String()))
		assert.NoDirExists(t, filepath.Join(config.mediaDir, "data", newShare.ID.String()))

		assert.Equal(t, newShare.ID, actual.ID)
		//assert.(t, newShare, actual)
Lukas Böhm's avatar
Lukas Böhm committed
218
219
220
	})

	t.Run("bad request", func(t *testing.T) {
Lukas Böhm's avatar
Lukas Böhm committed
221
		req, _ := http.NewRequest("POST", ts.URL+"/shares", nil)
Lukas Böhm's avatar
Lukas Böhm committed
222
		res, _ := http.DefaultClient.Do(req)
Lukas Böhm's avatar
Lukas Böhm committed
223
224
225
		//body, _ := ioutil.ReadAll(res.Body)
		//fmt.Println(string(body))
		// checks
Lukas Böhm's avatar
Lukas Böhm committed
226
227
228
		assert.Equal(t, http.StatusBadRequest, res.StatusCode)
	})
}
Lukas Böhm's avatar
Lukas Böhm committed
229
230

func TestCloseShare(t *testing.T) {
Lukas Böhm's avatar
Lukas Böhm committed
231
	Reset()
Lukas Böhm's avatar
Lukas Böhm committed
232
233
234
235
	router := mux.NewRouter()
	ts := httptest.NewServer(router)
	router.Handle("/share/{id}", endpointREST(CloseShare)).Methods("POST")
	defer ts.Close()
Lukas Böhm's avatar
Lukas Böhm committed
236

Lukas Böhm's avatar
Lukas Böhm committed
237
	t.Run("happy path", func(t *testing.T) {
Lukas Böhm's avatar
Lukas Böhm committed
238
		req, _ := http.NewRequest("POST", ts.URL+"/share/a558aca3-fb40-400b-8dc6-ae49c705c791", nil)
Lukas Böhm's avatar
Lukas Böhm committed
239
		res, _ := http.DefaultClient.Do(req)
Lukas Böhm's avatar
Lukas Böhm committed
240

Lukas Böhm's avatar
Lukas Böhm committed
241
242
		body, _ := ioutil.ReadAll(res.Body)
		fmt.Println(string(body))
Lukas Böhm's avatar
Lukas Böhm committed
243

Lukas Böhm's avatar
Lukas Böhm committed
244
		assert.Equal(t, http.StatusOK, res.StatusCode)
Lukas Böhm's avatar
Lukas Böhm committed
245
246
247
	})

	t.Run("not found", func(t *testing.T) {
Lukas Böhm's avatar
Lukas Böhm committed
248
		req, _ := http.NewRequest("POST", ts.URL+"/share/e3334044-eeee-4864-85be-555fb158009f", nil)
Lukas Böhm's avatar
Lukas Böhm committed
249
250
		res, _ := http.DefaultClient.Do(req)
		assert.Equal(t, http.StatusNotFound, res.StatusCode)
Lukas Böhm's avatar
Lukas Böhm committed
251
252
253
254
255
256
257
258
	})

	t.Run("bad request", func(t *testing.T) {

	})
}

func TestUploadAttachment(t *testing.T) {
Lukas Böhm's avatar
Lukas Böhm committed
259
260
261
262
263
264
	Reset()
	router := mux.NewRouter()
	ts := httptest.NewServer(router)
	router.Handle("/share/{id}/attachments", endpointREST(UploadAttachment)).Methods("POST")
	defer ts.Close()

Lukas Böhm's avatar
Lukas Böhm committed
265
	t.Run("happy path", func(t *testing.T) {
Lukas Böhm's avatar
Lukas Böhm committed
266
267
		body := &bytes.Buffer{}
		writer := multipart.NewWriter(body)
Lukas Böhm's avatar
Lukas Böhm committed
268
		fw, _ := writer.CreateFormFile("file", "poggers.txt")
Lukas Böhm's avatar
Lukas Böhm committed
269
		io.Copy(fw, strings.NewReader("POG POG POG POG"))
Lukas Böhm's avatar
Lukas Böhm committed
270
271
		writer.Close()

Lukas Böhm's avatar
Lukas Böhm committed
272
		req, _ := http.NewRequest("POST", ts.URL+"/share/a558aca3-fb40-400b-8dc6-ae49c705c791/attachments", bytes.NewReader(body.Bytes()))
Lukas Böhm's avatar
Lukas Böhm committed
273
274
		req.Header.Set("Content-Type", writer.FormDataContentType())
		res, _ := http.DefaultClient.Do(req)
Lukas Böhm's avatar
Lukas Böhm committed
275

Lukas Böhm's avatar
Lukas Böhm committed
276
		assert.EqualValues(t, http.StatusOK, res.StatusCode)
Lukas Böhm's avatar
Lukas Böhm committed
277
278
279
280
281
282
283
	})

	t.Run("not found", func(t *testing.T) {

	})

	t.Run("bad request", func(t *testing.T) {
Lukas Böhm's avatar
Lukas Böhm committed
284
		req, _ := http.NewRequest("POST", ts.URL+"/share/a558aca3-fb40-400b-8dc6-ae49c705c791/attachments", nil)
Lukas Böhm's avatar
Lukas Böhm committed
285
286
287
288
289
		res, _ := http.DefaultClient.Do(req)
		assert.Equal(t, http.StatusBadRequest, res.StatusCode)
	})

	t.Run("forbidden", func(t *testing.T) {
Lukas Böhm's avatar
Lukas Böhm committed
290
291
292

	})
}