all repos

onasty @ 39d6b8e

a one-time notes service

onasty/e2e/apiv1_notes_test.go (view raw)

Smirnov Olexandr Smirnov Olexandr
ss2316544@gmail.com
fix(api): get note with password (#151)..., 11 months ago
1
package e2e_test
2
3
import (
4
	"net/http"
5
	"net/http/httptest"
6
	"time"
7
8
	"github.com/gofrs/uuid/v5"
9
)
10
11
type (
12
	apiv1NoteCreateRequest struct {
13
		Content              string    `json:"content"`
14
		Slug                 string    `json:"slug"`
15
		Password             string    `json:"password"`
16
		BurnBeforeExpiration bool      `json:"burn_before_expiration"`
17
		ExpiresAt            time.Time `json:"expires_at"`
18
	}
19
	apiv1NoteCreateResponse struct {
20
		Slug string `json:"slug"`
21
	}
22
)
23
24
func (e *AppTestSuite) TestNoteV1_Create() {
25
	tests := []struct {
26
		name   string
27
		inp    apiv1NoteCreateRequest
28
		assert func(*httptest.ResponseRecorder, apiv1NoteCreateRequest)
29
	}{
30
		{
31
			name: "empty request",
32
			inp:  apiv1NoteCreateRequest{}, //nolint:exhaustruct
33
			assert: func(r *httptest.ResponseRecorder, _ apiv1NoteCreateRequest) {
34
				e.Equal(r.Code, http.StatusBadRequest)
35
			},
36
		},
37
		{
38
			name: "content only",
39
			inp:  apiv1NoteCreateRequest{Content: e.uuid()}, //nolint:exhaustruct
40
			assert: func(r *httptest.ResponseRecorder, _ apiv1NoteCreateRequest) {
41
				e.Equal(r.Code, http.StatusCreated)
42
43
				var body apiv1NoteCreateResponse
44
				e.readBodyAndUnjsonify(r.Body, &body)
45
46
				_, err := uuid.FromString(body.Slug)
47
				e.require.NoError(err)
48
49
				dbNote := e.getNoteBySlug(body.Slug)
50
				e.NotEmpty(dbNote)
51
			},
52
		},
53
		{
54
			name: "set slug",
55
			inp: apiv1NoteCreateRequest{ //nolint:exhaustruct
56
				Slug:    e.uuid() + "fuker",
57
				Content: e.uuid(),
58
			},
59
			assert: func(r *httptest.ResponseRecorder, inp apiv1NoteCreateRequest) {
60
				e.Equal(r.Code, http.StatusCreated)
61
62
				var body apiv1NoteCreateResponse
63
				e.readBodyAndUnjsonify(r.Body, &body)
64
65
				dbNote := e.getNoteBySlug(inp.Slug)
66
				e.NotEmpty(dbNote)
67
			},
68
		},
69
		{
70
			name: "set password",
71
			inp: apiv1NoteCreateRequest{ //nolint:exhaustruct
72
				Content:  e.uuid(),
73
				Password: e.uuid(),
74
			},
75
			assert: func(r *httptest.ResponseRecorder, _ apiv1NoteCreateRequest) {
76
				e.Equal(r.Code, http.StatusCreated)
77
			},
78
		},
79
		{
80
			name: "all possible fields",
81
			inp: apiv1NoteCreateRequest{ //nolint:exhaustruct
82
				Content:              e.uuid(),
83
				BurnBeforeExpiration: true,
84
				ExpiresAt:            time.Now().Add(time.Hour),
85
			},
86
			assert: func(r *httptest.ResponseRecorder, inp apiv1NoteCreateRequest) {
87
				e.Equal(r.Code, http.StatusCreated)
88
89
				var body apiv1NoteCreateResponse
90
				e.readBodyAndUnjsonify(r.Body, &body)
91
92
				dbNote := e.getNoteBySlug(body.Slug)
93
				e.NotEmpty(dbNote)
94
95
				e.Equal(dbNote.Content, inp.Content)
96
				e.Equal(dbNote.BurnBeforeExpiration, inp.BurnBeforeExpiration)
97
				e.Equal(dbNote.ExpiresAt.Unix(), inp.ExpiresAt.Unix())
98
			},
99
		},
100
	}
101
102
	for _, tt := range tests {
103
		httpResp := e.httpRequest(http.MethodPost, "/api/v1/note", e.jsonify(tt.inp))
104
		tt.assert(httpResp, tt.inp)
105
	}
106
}
107
108
type apiv1NoteGetResponse struct {
109
	Content   string     `json:"content"`
110
	ReadAt    *time.Time `json:"read_at"`
111
	CreatedAt time.Time  `json:"created_at"`
112
	ExpiresAt time.Time  `json:"expires_at"`
113
}
114
115
func (e *AppTestSuite) TestNoteV1_Get() {
116
	content := e.uuid()
117
	httpResp := e.httpRequest(
118
		http.MethodPost,
119
		"/api/v1/note",
120
		e.jsonify(apiv1NoteCreateRequest{ //nolint:exhaustruct
121
			Content: content,
122
		}),
123
	)
124
	e.Equal(http.StatusCreated, httpResp.Code)
125
126
	var bodyCreated apiv1NoteCreateResponse
127
	e.readBodyAndUnjsonify(httpResp.Body, &bodyCreated)
128
129
	httpResp = e.httpRequest(http.MethodGet, "/api/v1/note/"+bodyCreated.Slug, nil)
130
	e.Equal(httpResp.Code, http.StatusOK)
131
132
	var body apiv1NoteGetResponse
133
	e.readBodyAndUnjsonify(httpResp.Body, &body)
134
135
	e.Equal(content, body.Content)
136
137
	dbNote := e.getNoteBySlug(bodyCreated.Slug)
138
	e.Equal(dbNote.Content, "")
139
	e.Equal(dbNote.ReadAt.IsZero(), false)
140
}
141
142
func (e *AppTestSuite) TestNoteV1_GetWithPassword() {
143
	content := e.uuid()
144
	passwd := e.uuid()
145
	httpResp := e.httpRequest(
146
		http.MethodPost,
147
		"/api/v1/note",
148
		e.jsonify(apiv1NoteCreateRequest{ //nolint:exhaustruct
149
			Content:  content,
150
			Password: passwd,
151
		}),
152
	)
153
	e.Equal(http.StatusCreated, httpResp.Code)
154
155
	var bodyCreated apiv1NoteCreateResponse
156
	e.readBodyAndUnjsonify(httpResp.Body, &bodyCreated)
157
158
	httpResp = e.httpRequest(
159
		http.MethodGet,
160
		"/api/v1/note/"+bodyCreated.Slug+"?password="+passwd,
161
		nil,
162
	)
163
	e.Equal(httpResp.Code, http.StatusOK)
164
165
	var body apiv1NoteGetResponse
166
	e.readBodyAndUnjsonify(httpResp.Body, &body)
167
168
	e.Equal(content, body.Content)
169
170
	dbNote := e.getNoteBySlug(bodyCreated.Slug)
171
	e.Equal(dbNote.Content, "")
172
	e.Equal(dbNote.ReadAt.IsZero(), false)
173
}
174
175
func (e *AppTestSuite) TestNoteV1_GetWithPassword_wrongNoPassword() {
176
	content := e.uuid()
177
	passwd := e.uuid()
178
	httpResp := e.httpRequest(
179
		http.MethodPost,
180
		"/api/v1/note",
181
		e.jsonify(apiv1NoteCreateRequest{ //nolint:exhaustruct
182
			Content:  content,
183
			Password: passwd,
184
		}),
185
	)
186
	e.Equal(http.StatusCreated, httpResp.Code)
187
188
	var bodyCreated apiv1NoteCreateResponse
189
	e.readBodyAndUnjsonify(httpResp.Body, &bodyCreated)
190
191
	httpResp = e.httpRequest(http.MethodGet, "/api/v1/note/"+bodyCreated.Slug, nil)
192
	e.Equal(httpResp.Code, http.StatusNotFound)
193
}
194
195
func (e *AppTestSuite) TestNoteV1_GetWithPassword_wrong() {
196
	content := e.uuid()
197
	httpResp := e.httpRequest(
198
		http.MethodPost,
199
		"/api/v1/note",
200
		e.jsonify(apiv1NoteCreateRequest{ //nolint:exhaustruct
201
			Content:  content,
202
			Password: e.uuid(),
203
		}),
204
	)
205
	e.Equal(http.StatusCreated, httpResp.Code)
206
207
	var bodyCreated apiv1NoteCreateResponse
208
	e.readBodyAndUnjsonify(httpResp.Body, &bodyCreated)
209
210
	httpResp = e.httpRequest(
211
		http.MethodGet,
212
		"/api/v1/note/"+bodyCreated.Slug+"?password="+e.uuid(),
213
		nil,
214
	)
215
	e.Equal(httpResp.Code, http.StatusNotFound)
216
}
217
218
type apiv1NoteMetadataResponse struct {
219
	CreatedAt   time.Time `json:"created_at"`
220
	HasPassword bool      `json:"has_password"`
221
}
222
223
func (e *AppTestSuite) TestNoteV1_GetMetadata() {
224
	// create note
225
	httpResp := e.httpRequest(
226
		http.MethodPost,
227
		"/api/v1/note",
228
		e.jsonify(apiv1NoteCreateRequest{Content: "content"}), //nolint:exhaustruct
229
	)
230
	e.Equal(http.StatusCreated, httpResp.Code)
231
232
	var bodyCreated apiv1NoteCreateResponse
233
	e.readBodyAndUnjsonify(httpResp.Body, &bodyCreated)
234
235
	// get metadata
236
	metaResp := e.httpRequest(http.MethodGet, "/api/v1/note/"+bodyCreated.Slug+"/meta", []byte{})
237
	e.Equal(metaResp.Code, http.StatusOK)
238
239
	var metadata apiv1NoteMetadataResponse
240
	e.readBodyAndUnjsonify(metaResp.Body, &metadata)
241
242
	e.False(metadata.HasPassword)
243
	e.NotEmpty(metadata.CreatedAt)
244
}
245
246
func (e *AppTestSuite) TestNoteV1_GetMetadata_withPassword() {
247
	// create note
248
	httpResp := e.httpRequest(
249
		http.MethodPost,
250
		"/api/v1/note",
251
		e.jsonify(apiv1NoteCreateRequest{ //nolint:exhaustruct
252
			Content:  "content",
253
			Password: "pass",
254
		}),
255
	)
256
	e.Equal(http.StatusCreated, httpResp.Code)
257
258
	var bodyCreated apiv1NoteCreateResponse
259
	e.readBodyAndUnjsonify(httpResp.Body, &bodyCreated)
260
261
	// get metadata
262
	metaResp := e.httpRequest(http.MethodGet, "/api/v1/note/"+bodyCreated.Slug+"/meta", []byte{})
263
	e.Equal(http.StatusOK, metaResp.Code)
264
265
	var metadata apiv1NoteMetadataResponse
266
	e.readBodyAndUnjsonify(metaResp.Body, &metadata)
267
268
	e.True(metadata.HasPassword)
269
	e.NotEmpty(metadata.CreatedAt)
270
}
271
272
func (e *AppTestSuite) TestNoteV1_GetMetadata_notFound() {
273
	metaResp := e.httpRequest(http.MethodGet, "/api/v1/note/"+e.uuid()+"/meta", []byte{})
274
	e.Equal(http.StatusNotFound, metaResp.Code)
275
}
276
277
func (e *AppTestSuite) TestNoteV1_GetMetadata_readNote() {
278
	// create note
279
	createdResp := e.httpRequest(
280
		http.MethodPost,
281
		"/api/v1/note",
282
		e.jsonify(apiv1NoteCreateRequest{Content: "content"}), //nolint:exhaustruct
283
	)
284
	e.Equal(http.StatusCreated, createdResp.Code)
285
286
	var bodyCreated apiv1NoteCreateResponse
287
	e.readBodyAndUnjsonify(createdResp.Body, &bodyCreated)
288
289
	// read note
290
	readResp := e.httpRequest(http.MethodGet, "/api/v1/note/"+bodyCreated.Slug, nil)
291
	e.Equal(http.StatusOK, readResp.Code)
292
293
	// get metadata
294
	metaResp := e.httpRequest(http.MethodGet, "/api/v1/note/"+e.uuid()+"/meta", nil)
295
	e.Equal(http.StatusNotFound, metaResp.Code)
296
}