|
| 1 | +package controller_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "github.com/EduOJ/backend/app/request" |
| 6 | + "github.com/EduOJ/backend/app/response" |
| 7 | + "github.com/EduOJ/backend/base" |
| 8 | + "github.com/EduOJ/backend/database/models" |
| 9 | + "github.com/stretchr/testify/assert" |
| 10 | + "net/http" |
| 11 | + "testing" |
| 12 | +) |
| 13 | + |
| 14 | +func TestCreateComment(t *testing.T) { |
| 15 | + t.Parallel() |
| 16 | + successTests := []struct { |
| 17 | + name string |
| 18 | + req request.CreateCommentRequest |
| 19 | + }{ |
| 20 | + { |
| 21 | + name: "SuccessRootCommentForProblem", |
| 22 | + req: request.CreateCommentRequest{ |
| 23 | + Content: "balabala1", |
| 24 | + FatherID: 0, |
| 25 | + TargetID: 1, |
| 26 | + TargetType: "problem", |
| 27 | + }, |
| 28 | + }, |
| 29 | + { |
| 30 | + name: "SuccessNoneRootCommentForProblem", |
| 31 | + req: request.CreateCommentRequest{ |
| 32 | + Content: "balabala2", |
| 33 | + FatherID: 1, |
| 34 | + TargetID: 1, |
| 35 | + TargetType: "problem", |
| 36 | + }, |
| 37 | + }, |
| 38 | + } |
| 39 | + t.Run("TestCreateCommentSuccess", func(t *testing.T) { |
| 40 | + test := successTests[0] |
| 41 | + t.Run("TestCreateComment"+test.name, func(t *testing.T) { |
| 42 | + t.Parallel() |
| 43 | + user := createUserForTest(t, "create_comment", 0) |
| 44 | + user.GrantRole("admin") |
| 45 | + httpReq := makeReq(t, "POST", base.Echo.Reverse("comment.createComment"), successTests[0].req, headerOption{ |
| 46 | + "Set-User-For-Test": {fmt.Sprintf("%d", user.ID)}, |
| 47 | + }) |
| 48 | + httpResp := makeResp(httpReq) |
| 49 | + databaseComment := models.Comment{} |
| 50 | + assert.NoError(t, base.DB.Where("content = ?", successTests[0].req.Content).First(&databaseComment).Error) |
| 51 | + |
| 52 | + assert.Equal(t, test.req.Content, databaseComment.Content) |
| 53 | + assert.Equal(t, test.req.FatherID, databaseComment.FatherID) |
| 54 | + assert.Equal(t, test.req.TargetID, databaseComment.TargetID) |
| 55 | + |
| 56 | + |
| 57 | + test = successTests[1] |
| 58 | + test.req.FatherID = databaseComment.ID |
| 59 | + var data interface{} |
| 60 | + data = test.req |
| 61 | + httpReq = makeReq(t, "POST", base.Echo.Reverse("comment.createComment"), data, headerOption{ |
| 62 | + "Set-User-For-Test": {fmt.Sprintf("%d", user.ID)}, |
| 63 | + }) |
| 64 | + httpResp = makeResp(httpReq) |
| 65 | + assert.Equal(t, http.StatusCreated, httpResp.StatusCode) |
| 66 | + |
| 67 | + databaseComment = models.Comment{} |
| 68 | + assert.NoError(t, base.DB.Where("content = ?", test.req.Content).First(&databaseComment).Error) |
| 69 | + // request == database |
| 70 | + assert.Equal(t, test.req.Content, databaseComment.Content) |
| 71 | + assert.Equal(t, test.req.FatherID, databaseComment.FatherID) |
| 72 | + assert.Equal(t, test.req.TargetID, databaseComment.TargetID) |
| 73 | + |
| 74 | + |
| 75 | + }) |
| 76 | + }) |
| 77 | + |
| 78 | +} |
| 79 | + |
| 80 | + |
| 81 | +func TestGetComment(t *testing.T) { |
| 82 | + t.Parallel() |
| 83 | + |
| 84 | + problem := models.Problem{ |
| 85 | + Name: "balabala", |
| 86 | + Description: "test_get_problems_temp1_description", |
| 87 | + AttachmentFileName: "test_get_problems_temp1_attachment_file_name", |
| 88 | + LanguageAllowed: []string{"test_get_problems_temp1_language_allowed"}, |
| 89 | + Public: true, |
| 90 | + } |
| 91 | + assert.NoError(t, base.DB.Create(&problem).Error) |
| 92 | + databaseProblem := models.Problem{} |
| 93 | + assert.NoError(t, base.DB.Where("Name = ?", "balabala").First(&databaseProblem).Error) |
| 94 | + |
| 95 | + comment1 := models.Comment{ |
| 96 | + Content: "test_get_comment_1", |
| 97 | + FatherID: 0, |
| 98 | + TargetID: databaseProblem.ID, |
| 99 | + TargetType: "problem", |
| 100 | + } |
| 101 | + assert.NoError(t, base.DB.Create(&comment1).Error) |
| 102 | + databaseComment := models.Comment{} |
| 103 | + assert.NoError(t, base.DB.Where("Content = ?", "test_get_comment_1").First(&databaseComment).Error) |
| 104 | + |
| 105 | + comment2 := models.Comment{ |
| 106 | + Content: "test_get_comment_2", |
| 107 | + FatherID: databaseComment.ID, |
| 108 | + TargetID: databaseProblem.ID, |
| 109 | + TargetType: "problem", |
| 110 | + } |
| 111 | + assert.NoError(t, base.DB.Create(&comment2).Error) |
| 112 | + |
| 113 | + |
| 114 | + successTests := []struct { |
| 115 | + name string |
| 116 | + req request.GetCommentRequest |
| 117 | + }{ |
| 118 | + { |
| 119 | + name: "problem", |
| 120 | + req: request.GetCommentRequest{ |
| 121 | + TargetType: "problem", |
| 122 | + TargetID: databaseProblem.ID, |
| 123 | + Limit: 0, |
| 124 | + Offset: 5, |
| 125 | + }, |
| 126 | + }, |
| 127 | + } |
| 128 | + |
| 129 | + t.Run("testGetProblemsSuccess", func(t *testing.T) { |
| 130 | + t.Parallel() |
| 131 | + for i, test := range successTests { |
| 132 | + i := i |
| 133 | + test := test |
| 134 | + user := createUserForTest(t, "get_comments", i) |
| 135 | + // assert.False(t,user.Can("manage_problem")) |
| 136 | + httpResp := makeResp(makeReq(t, "GET", base.Echo.Reverse("comment.getComment"), test.req, headerOption{ |
| 137 | + "Set-User-For-Test": {fmt.Sprintf("%d", user.ID)}, |
| 138 | + })) |
| 139 | + body := httpResp.Body |
| 140 | + assert.Equal(t, body,body) |
| 141 | + } |
| 142 | + }) |
| 143 | + |
| 144 | +} |
| 145 | + |
| 146 | +func TestAddReactionAndDeleteReaction(t *testing.T) { |
| 147 | + problem := models.Problem{ |
| 148 | + Name: "balabala", |
| 149 | + Description: "test_get_problems_temp1_description", |
| 150 | + AttachmentFileName: "test_get_problems_temp1_attachment_file_name", |
| 151 | + LanguageAllowed: []string{"test_get_problems_temp1_language_allowed"}, |
| 152 | + Public: true, |
| 153 | + } |
| 154 | + assert.NoError(t, base.DB.Create(&problem).Error) |
| 155 | + databaseProblem := models.Problem{} |
| 156 | + assert.NoError(t, base.DB.Where("Name = ?", "balabala").First(&databaseProblem).Error) |
| 157 | + |
| 158 | + comment1 := models.Comment{ |
| 159 | + Content: "test_get_comment_1", |
| 160 | + FatherID: 0, |
| 161 | + TargetID: databaseProblem.ID, |
| 162 | + TargetType: "problem", |
| 163 | + } |
| 164 | + assert.NoError(t, base.DB.Create(&comment1).Error) |
| 165 | + databaseComment := models.Comment{} |
| 166 | + assert.NoError(t, base.DB.Where("Content = ?", "test_get_comment_1").First(&databaseComment).Error) |
| 167 | + |
| 168 | + req := request.AddReactionRequest{ |
| 169 | + TargetType: "comment", |
| 170 | + TargetID: databaseComment.ID, |
| 171 | + EmojiType: "1", |
| 172 | + } |
| 173 | + |
| 174 | + // assert.False(t,user.Can("manage_problem")) |
| 175 | + makeResp(makeReq(t, "POST", base.Echo.Reverse("comment.addReaction"), req, applyNormalUser)) |
| 176 | + |
| 177 | + comment2 := models.Comment{ |
| 178 | + Content: "test_get_comment_2", |
| 179 | + FatherID: databaseComment.ID, |
| 180 | + TargetID: databaseProblem.ID, |
| 181 | + TargetType: "problem", |
| 182 | + } |
| 183 | + assert.NoError(t, base.DB.Create(&comment2).Error) |
| 184 | + |
| 185 | + |
| 186 | + //user := createUserForTest(t, "get_problems_submitter", 1) |
| 187 | + successTests := []struct { |
| 188 | + name string |
| 189 | + req request.AddReactionRequest |
| 190 | + }{ |
| 191 | + { |
| 192 | + name: "problem", |
| 193 | + req: request.AddReactionRequest{ |
| 194 | + TargetType: "comment", |
| 195 | + TargetID: databaseComment.ID, |
| 196 | + EmojiType: "1", |
| 197 | + }, |
| 198 | + }, |
| 199 | + { |
| 200 | + name: "problem", |
| 201 | + req: request.AddReactionRequest{ |
| 202 | + TargetType: "comment", |
| 203 | + TargetID: databaseComment.ID, |
| 204 | + EmojiType: "2", |
| 205 | + }, |
| 206 | + }, |
| 207 | + } |
| 208 | + |
| 209 | + t.Run("testAddReactionSuccess", func(t *testing.T) { |
| 210 | + t.Parallel() |
| 211 | + for i, test := range successTests { |
| 212 | + i := i |
| 213 | + test := test |
| 214 | + user := createUserForTest(t, "add_reaction", i+1) |
| 215 | + // assert.False(t,user.Can("manage_problem")) |
| 216 | + httpResp := makeResp(makeReq(t, "POST", base.Echo.Reverse("comment.addReaction"), test.req, headerOption{ |
| 217 | + "Set-User-For-Test": {fmt.Sprintf("%d", user.ID)}, |
| 218 | + })) |
| 219 | + assert.Equal(t, http.StatusCreated, httpResp.StatusCode) |
| 220 | + } |
| 221 | + }) |
| 222 | + |
| 223 | + |
| 224 | + failTests := []failTest{ |
| 225 | + { |
| 226 | + name: "InvalidStatus", |
| 227 | + method: "DELETE", |
| 228 | + path: base.Echo.Reverse("comment.deleteReaction"), |
| 229 | + req: request.DeleteReactionRequest{ |
| 230 | + TargetType: "comment", |
| 231 | + TargetID: databaseComment.ID, |
| 232 | + EmojiType: "5", |
| 233 | + }, |
| 234 | + reqOptions: []reqOption{ |
| 235 | + applyNormalUser, |
| 236 | + }, |
| 237 | + statusCode: http.StatusBadRequest, |
| 238 | + resp: response.ErrorResp("you have not actived!", nil), |
| 239 | + }, |
| 240 | + } |
| 241 | + |
| 242 | + runFailTests(t, failTests, "") |
| 243 | + |
| 244 | + |
| 245 | + successTests1 := []struct { |
| 246 | + name string |
| 247 | + req request.DeleteReactionRequest |
| 248 | + }{ |
| 249 | + { |
| 250 | + name: "problem", |
| 251 | + req: request.DeleteReactionRequest{ |
| 252 | + TargetType: "comment", |
| 253 | + TargetID: databaseComment.ID, |
| 254 | + EmojiType: "1", |
| 255 | + }, |
| 256 | + }, |
| 257 | + } |
| 258 | + |
| 259 | + t.Run("testDeleteReactionSuccess", func(t *testing.T) { |
| 260 | + t.Parallel() |
| 261 | + for _,test := range successTests1 { |
| 262 | + test := test |
| 263 | + httpResp := makeResp(makeReq(t, "DELETE", base.Echo.Reverse("comment.deleteReaction"), test.req, applyNormalUser)) |
| 264 | + assert.Equal(t, http.StatusCreated, httpResp.StatusCode) |
| 265 | + } |
| 266 | + }) |
| 267 | + |
| 268 | + |
| 269 | +} |
| 270 | + |
| 271 | + |
| 272 | +func TestDeleteComment(t *testing.T) { |
| 273 | + problem := models.Problem{ |
| 274 | + Name: "balabala", |
| 275 | + Description: "test_get_problems_temp1_description", |
| 276 | + AttachmentFileName: "test_get_problems_temp1_attachment_file_name", |
| 277 | + LanguageAllowed: []string{"test_get_problems_temp1_language_allowed"}, |
| 278 | + Public: true, |
| 279 | + } |
| 280 | + assert.NoError(t, base.DB.Create(&problem).Error) |
| 281 | + databaseProblem := models.Problem{} |
| 282 | + assert.NoError(t, base.DB.Where("Name = ?", "balabala").First(&databaseProblem).Error) |
| 283 | + |
| 284 | + comment1 := models.Comment{ |
| 285 | + Content: "test_get_comment_1", |
| 286 | + FatherID: 0, |
| 287 | + TargetID: databaseProblem.ID, |
| 288 | + TargetType: "problem", |
| 289 | + } |
| 290 | + assert.NoError(t, base.DB.Create(&comment1).Error) |
| 291 | + databaseComment := models.Comment{} |
| 292 | + assert.NoError(t, base.DB.Where("Content = ?", "test_get_comment_1").First(&databaseComment).Error) |
| 293 | + |
| 294 | + req := request.AddReactionRequest{ |
| 295 | + TargetType: "comment", |
| 296 | + TargetID: databaseComment.ID, |
| 297 | + EmojiType: "1", |
| 298 | + } |
| 299 | + |
| 300 | + // assert.False(t,user.Can("manage_problem")) |
| 301 | + makeResp(makeReq(t, "POST", base.Echo.Reverse("comment.addReaction"), req, applyNormalUser)) |
| 302 | + |
| 303 | + comment2 := models.Comment{ |
| 304 | + Content: "test_get_comment_2", |
| 305 | + FatherID: databaseComment.ID, |
| 306 | + TargetID: databaseProblem.ID, |
| 307 | + TargetType: "problem", |
| 308 | + } |
| 309 | + assert.NoError(t, base.DB.Create(&comment2).Error) |
| 310 | + |
| 311 | + |
| 312 | +} |
| 313 | + |
| 314 | + |
0 commit comments