onasty/e2e/apiv1_notes_authorized_test.go (view raw)
Smirnov Oleksandr
Smirnov Oleksandr
ss2316544@gmail.com feat: notes manipulations for the note authors (#117)..., 1 year ago
ss2316544@gmail.com feat: notes manipulations for the note authors (#117)..., 1 year ago
| 1 | package e2e_test |
| 2 | |
| 3 | import ( |
| 4 | "net/http" |
| 5 | "time" |
| 6 | ) |
| 7 | |
| 8 | func (e *AppTestSuite) TestNoteV1_Create_authorized() { |
| 9 | uid, toks := e.createAndSingIn(e.uuid()+"@test.com", "password") |
| 10 | httpResp := e.httpRequest( |
| 11 | http.MethodPost, |
| 12 | "/api/v1/note", |
| 13 | e.jsonify(apiv1NoteCreateRequest{ //nolint:exhaustruct |
| 14 | Content: "sample content for the test", |
| 15 | }), |
| 16 | toks.AccessToken, |
| 17 | ) |
| 18 | |
| 19 | var body apiv1NoteCreateResponse |
| 20 | e.readBodyAndUnjsonify(httpResp.Body, &body) |
| 21 | |
| 22 | dbNote := e.getNoteBySlug(body.Slug) |
| 23 | dbNoteAuthor := e.getLastNoteAuthorsRecordByAuthorID(uid) |
| 24 | |
| 25 | e.Equal(http.StatusCreated, httpResp.Code) |
| 26 | e.Equal(dbNote.ID.String(), dbNoteAuthor.noteID.String()) |
| 27 | } |
| 28 | |
| 29 | func (e *AppTestSuite) TestNoteV1_Delete() { |
| 30 | _, toks := e.createAndSingIn(e.uuid()+"@test.com", "password") |
| 31 | httpResp := e.httpRequest( |
| 32 | http.MethodPost, |
| 33 | "/api/v1/note", |
| 34 | e.jsonify(apiv1NoteCreateRequest{ //nolint:exhaustruct |
| 35 | Content: "sample content for the test", |
| 36 | }), |
| 37 | toks.AccessToken, |
| 38 | ) |
| 39 | |
| 40 | e.Equal(httpResp.Code, http.StatusCreated) |
| 41 | |
| 42 | var body apiv1NoteCreateResponse |
| 43 | e.readBodyAndUnjsonify(httpResp.Body, &body) |
| 44 | |
| 45 | dbNote := e.getNoteBySlug(body.Slug) |
| 46 | e.NotEmpty(dbNote) |
| 47 | |
| 48 | httpResp = e.httpRequest( |
| 49 | http.MethodDelete, |
| 50 | "/api/v1/note/"+body.Slug, |
| 51 | nil, |
| 52 | toks.AccessToken, |
| 53 | ) |
| 54 | e.Equal(httpResp.Code, http.StatusNoContent) |
| 55 | |
| 56 | dbNote = e.getNoteBySlug(body.Slug) |
| 57 | e.Empty(dbNote) |
| 58 | } |
| 59 | |
| 60 | type apiV1NotePatchRequest struct { |
| 61 | ExpiresAt time.Time `json:"expires_at"` |
| 62 | BurnBeforeExpiration bool `json:"burn_before_expiration"` |
| 63 | } |
| 64 | |
| 65 | func (e *AppTestSuite) TestNoteV1_updateExpirationTime() { |
| 66 | _, toks := e.createAndSingIn(e.uuid()+"@test.com", "password") |
| 67 | httpResp := e.httpRequest( |
| 68 | http.MethodPost, |
| 69 | "/api/v1/note", |
| 70 | e.jsonify(apiv1NoteCreateRequest{ //nolint:exhaustruct |
| 71 | Content: "sample content for the test", |
| 72 | ExpiresAt: time.Now().Add(time.Minute), |
| 73 | BurnBeforeExpiration: false, |
| 74 | }), |
| 75 | toks.AccessToken, |
| 76 | ) |
| 77 | |
| 78 | e.Equal(httpResp.Code, http.StatusCreated) |
| 79 | |
| 80 | var body apiv1NoteCreateResponse |
| 81 | e.readBodyAndUnjsonify(httpResp.Body, &body) |
| 82 | |
| 83 | patchTime := time.Now().Add(time.Hour) |
| 84 | httpResp = e.httpRequest( |
| 85 | http.MethodPatch, |
| 86 | "/api/v1/note/"+body.Slug+"/expires", |
| 87 | e.jsonify(apiV1NotePatchRequest{ |
| 88 | ExpiresAt: patchTime, |
| 89 | BurnBeforeExpiration: true, |
| 90 | }), |
| 91 | toks.AccessToken, |
| 92 | ) |
| 93 | |
| 94 | e.Equal(httpResp.Code, http.StatusOK) |
| 95 | |
| 96 | dbNote := e.getNoteBySlug(body.Slug) |
| 97 | e.Equal(true, dbNote.BurnBeforeExpiration) |
| 98 | e.Equal(patchTime.Unix(), dbNote.ExpiresAt.Unix()) |
| 99 | } |
| 100 | |
| 101 | func (e *AppTestSuite) TestNoteV1_updateExpirationTime_notFound() { |
| 102 | _, toks := e.createAndSingIn(e.uuid()+"@test.com", "password") |
| 103 | httpResp := e.httpRequest( |
| 104 | http.MethodPatch, |
| 105 | "/api/v1/note/"+e.uuid(), |
| 106 | e.jsonify(apiV1NotePatchRequest{ |
| 107 | ExpiresAt: time.Now().Add(time.Hour), |
| 108 | BurnBeforeExpiration: true, |
| 109 | }), |
| 110 | toks.AccessToken, |
| 111 | ) |
| 112 | |
| 113 | e.Equal(httpResp.Code, http.StatusNotFound) |
| 114 | } |
| 115 | |
| 116 | type apiV1NoteSetPasswordRequest struct { |
| 117 | Password string `json:"password"` |
| 118 | } |
| 119 | |
| 120 | func (e *AppTestSuite) TestNoteV1_UpdatePassword() { |
| 121 | _, toks := e.createAndSingIn(e.uuid()+"@test.com", "password") |
| 122 | httpResp := e.httpRequest( |
| 123 | http.MethodPost, |
| 124 | "/api/v1/note", |
| 125 | e.jsonify(apiv1NoteCreateRequest{ //nolint:exhaustruct |
| 126 | Content: "content", |
| 127 | }), |
| 128 | toks.AccessToken, |
| 129 | ) |
| 130 | |
| 131 | e.Equal(httpResp.Code, http.StatusCreated) |
| 132 | |
| 133 | var body apiv1NoteCreateResponse |
| 134 | e.readBodyAndUnjsonify(httpResp.Body, &body) |
| 135 | |
| 136 | dbNoteOriginal := e.getNoteBySlug(body.Slug) |
| 137 | e.Empty(dbNoteOriginal.Password) |
| 138 | |
| 139 | passwd := "new-password" |
| 140 | httpResp = e.httpRequest( |
| 141 | http.MethodPatch, |
| 142 | "/api/v1/note/"+body.Slug+"/password", |
| 143 | e.jsonify(apiV1NoteSetPasswordRequest{ |
| 144 | Password: passwd, |
| 145 | }), |
| 146 | toks.AccessToken, |
| 147 | ) |
| 148 | |
| 149 | e.Equal(httpResp.Code, http.StatusOK) |
| 150 | |
| 151 | dbNote := e.getNoteBySlug(body.Slug) |
| 152 | e.NotEmpty(dbNote.Password) |
| 153 | |
| 154 | err := e.hasher.Compare(dbNote.Password, passwd) |
| 155 | e.require.NoError(err) |
| 156 | } |
| 157 | |
| 158 | func (e *AppTestSuite) TestNoteV1_UpdatePassword_notFound() { |
| 159 | _, toks := e.createAndSingIn(e.uuid()+"@test.com", "password") |
| 160 | httpResp := e.httpRequest( |
| 161 | http.MethodPatch, |
| 162 | "/api/v1/note/"+e.uuid()+"/password", |
| 163 | e.jsonify(apiV1NoteSetPasswordRequest{ |
| 164 | Password: "passwd", |
| 165 | }), |
| 166 | toks.AccessToken, |
| 167 | ) |
| 168 | |
| 169 | e.Equal(httpResp.Code, http.StatusNotFound) |
| 170 | } |
| 171 | |
| 172 | func (e *AppTestSuite) TestNoteV1_UpdatePassword_passwordNotProvided() { |
| 173 | _, toks := e.createAndSingIn(e.uuid()+"@test.com", "password") |
| 174 | httpResp := e.httpRequest( |
| 175 | http.MethodPost, |
| 176 | "/api/v1/note", |
| 177 | e.jsonify(apiv1NoteCreateRequest{ //nolint:exhaustruct |
| 178 | Content: "content", |
| 179 | }), |
| 180 | toks.AccessToken, |
| 181 | ) |
| 182 | |
| 183 | e.Equal(httpResp.Code, http.StatusCreated) |
| 184 | |
| 185 | var body apiv1NoteCreateResponse |
| 186 | e.readBodyAndUnjsonify(httpResp.Body, &body) |
| 187 | |
| 188 | dbNoteOriginal := e.getNoteBySlug(body.Slug) |
| 189 | e.Empty(dbNoteOriginal.Password) |
| 190 | |
| 191 | httpResp = e.httpRequest( |
| 192 | http.MethodPatch, |
| 193 | "/api/v1/note/"+body.Slug+"/password", |
| 194 | e.jsonify(apiV1NoteSetPasswordRequest{ |
| 195 | Password: "", |
| 196 | }), |
| 197 | toks.AccessToken, |
| 198 | ) |
| 199 | |
| 200 | e.Equal(httpResp.Code, http.StatusBadRequest) |
| 201 | } |