all repos

onasty @ a705d9c2248d300dee212abc6a1e453379f84d3a

a one-time notes service

onasty/e2e/apiv1_notes_authorized_test.go(view raw)

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
package e2e_test

import (
	"net/http"
	"time"
)

func (e *AppTestSuite) TestNoteV1_Create_authorized() {
	uid, toks := e.createAndSingIn(e.uuid()+"@test.com", "password")
	httpResp := e.httpRequest(
		http.MethodPost,
		"/api/v1/note",
		e.jsonify(apiv1NoteCreateRequest{ //nolint:exhaustruct
			Content: "sample content for the test",
		}),
		toks.AccessToken,
	)

	var body apiv1NoteCreateResponse
	e.readBodyAndUnjsonify(httpResp.Body, &body)

	dbNote := e.getNoteBySlug(body.Slug)
	dbNoteAuthor := e.getLastNoteAuthorsRecordByAuthorID(uid)

	e.Equal(http.StatusCreated, httpResp.Code)
	e.Equal(dbNote.ID.String(), dbNoteAuthor.noteID.String())
}

func (e *AppTestSuite) TestNoteV1_Delete() {
	_, toks := e.createAndSingIn(e.uuid()+"@test.com", "password")
	httpResp := e.httpRequest(
		http.MethodPost,
		"/api/v1/note",
		e.jsonify(apiv1NoteCreateRequest{ //nolint:exhaustruct
			Content: "sample content for the test",
		}),
		toks.AccessToken,
	)

	e.Equal(httpResp.Code, http.StatusCreated)

	var body apiv1NoteCreateResponse
	e.readBodyAndUnjsonify(httpResp.Body, &body)

	dbNote := e.getNoteBySlug(body.Slug)
	e.NotEmpty(dbNote)

	httpResp = e.httpRequest(
		http.MethodDelete,
		"/api/v1/note/"+body.Slug,
		nil,
		toks.AccessToken,
	)
	e.Equal(httpResp.Code, http.StatusNoContent)

	dbNote = e.getNoteBySlug(body.Slug)
	e.Empty(dbNote)
}

type apiV1NotePatchRequest struct {
	ExpiresAt            time.Time `json:"expires_at"`
	BurnBeforeExpiration bool      `json:"burn_before_expiration"`
}

func (e *AppTestSuite) TestNoteV1_updateExpirationTime() {
	_, toks := e.createAndSingIn(e.uuid()+"@test.com", "password")
	httpResp := e.httpRequest(
		http.MethodPost,
		"/api/v1/note",
		e.jsonify(apiv1NoteCreateRequest{ //nolint:exhaustruct
			Content:              "sample content for the test",
			ExpiresAt:            time.Now().Add(time.Minute),
			BurnBeforeExpiration: false,
		}),
		toks.AccessToken,
	)

	e.Equal(httpResp.Code, http.StatusCreated)

	var body apiv1NoteCreateResponse
	e.readBodyAndUnjsonify(httpResp.Body, &body)

	patchTime := time.Now().Add(time.Hour)
	httpResp = e.httpRequest(
		http.MethodPatch,
		"/api/v1/note/"+body.Slug+"/expires",
		e.jsonify(apiV1NotePatchRequest{
			ExpiresAt:            patchTime,
			BurnBeforeExpiration: true,
		}),
		toks.AccessToken,
	)

	e.Equal(httpResp.Code, http.StatusOK)

	dbNote := e.getNoteBySlug(body.Slug)
	e.Equal(true, dbNote.BurnBeforeExpiration)
	e.Equal(patchTime.Unix(), dbNote.ExpiresAt.Unix())
}

func (e *AppTestSuite) TestNoteV1_updateExpirationTime_notFound() {
	_, toks := e.createAndSingIn(e.uuid()+"@test.com", "password")
	httpResp := e.httpRequest(
		http.MethodPatch,
		"/api/v1/note/"+e.uuid(),
		e.jsonify(apiV1NotePatchRequest{
			ExpiresAt:            time.Now().Add(time.Hour),
			BurnBeforeExpiration: true,
		}),
		toks.AccessToken,
	)

	e.Equal(httpResp.Code, http.StatusNotFound)
}

type apiV1NoteSetPasswordRequest struct {
	Password string `json:"password"`
}

func (e *AppTestSuite) TestNoteV1_UpdatePassword() {
	_, toks := e.createAndSingIn(e.uuid()+"@test.com", "password")
	httpResp := e.httpRequest(
		http.MethodPost,
		"/api/v1/note",
		e.jsonify(apiv1NoteCreateRequest{ //nolint:exhaustruct
			Content: "content",
		}),
		toks.AccessToken,
	)

	e.Equal(httpResp.Code, http.StatusCreated)

	var body apiv1NoteCreateResponse
	e.readBodyAndUnjsonify(httpResp.Body, &body)

	dbNoteOriginal := e.getNoteBySlug(body.Slug)
	e.Empty(dbNoteOriginal.Password)

	passwd := "new-password"
	httpResp = e.httpRequest(
		http.MethodPatch,
		"/api/v1/note/"+body.Slug+"/password",
		e.jsonify(apiV1NoteSetPasswordRequest{
			Password: passwd,
		}),
		toks.AccessToken,
	)

	e.Equal(httpResp.Code, http.StatusOK)

	dbNote := e.getNoteBySlug(body.Slug)
	e.NotEmpty(dbNote.Password)

	err := e.hasher.Compare(dbNote.Password, passwd)
	e.require.NoError(err)
}

func (e *AppTestSuite) TestNoteV1_UpdatePassword_notFound() {
	_, toks := e.createAndSingIn(e.uuid()+"@test.com", "password")
	httpResp := e.httpRequest(
		http.MethodPatch,
		"/api/v1/note/"+e.uuid()+"/password",
		e.jsonify(apiV1NoteSetPasswordRequest{
			Password: "passwd",
		}),
		toks.AccessToken,
	)

	e.Equal(httpResp.Code, http.StatusNotFound)
}

func (e *AppTestSuite) TestNoteV1_UpdatePassword_passwordNotProvided() {
	_, toks := e.createAndSingIn(e.uuid()+"@test.com", "password")
	httpResp := e.httpRequest(
		http.MethodPost,
		"/api/v1/note",
		e.jsonify(apiv1NoteCreateRequest{ //nolint:exhaustruct
			Content: "content",
		}),
		toks.AccessToken,
	)

	e.Equal(httpResp.Code, http.StatusCreated)

	var body apiv1NoteCreateResponse
	e.readBodyAndUnjsonify(httpResp.Body, &body)

	dbNoteOriginal := e.getNoteBySlug(body.Slug)
	e.Empty(dbNoteOriginal.Password)

	httpResp = e.httpRequest(
		http.MethodPatch,
		"/api/v1/note/"+body.Slug+"/password",
		e.jsonify(apiV1NoteSetPasswordRequest{
			Password: "",
		}),
		toks.AccessToken,
	)

	e.Equal(httpResp.Code, http.StatusBadRequest)
}