From 798b07719c7660eb49064c9d78198687794a5234 Mon Sep 17 00:00:00 2001 From: Pyeongseok Oh Date: Sun, 27 Mar 2022 19:14:45 +0900 Subject: [PATCH] =?UTF-8?q?HandleDeleteSchedule=20=ED=85=8C=EC=8A=A4?= =?UTF-8?q?=ED=8A=B8=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/handler/handler.go | 2 +- backend/handler/handler_test.go | 201 ++++++++++++++++++++++++++++++++ 2 files changed, 202 insertions(+), 1 deletion(-) diff --git a/backend/handler/handler.go b/backend/handler/handler.go index b83a7fb..4fc58c5 100644 --- a/backend/handler/handler.go +++ b/backend/handler/handler.go @@ -136,7 +136,7 @@ func HandleDeleteSchedule(w http.ResponseWriter, r *http.Request) { return nil }) if err != nil { - httpError(w, http.StatusBadRequest, "failed to add schedule", err) + httpError(w, http.StatusBadRequest, "failed to delete schedule", err) return } diff --git a/backend/handler/handler_test.go b/backend/handler/handler_test.go index d77a1ae..68d80d2 100644 --- a/backend/handler/handler_test.go +++ b/backend/handler/handler_test.go @@ -340,3 +340,204 @@ func TestHandleAddSchedule(t *testing.T) { assert.Equal(t, tc.want, resp.StatusCode) } } + +func TestHandleDeleteSchedule(t *testing.T) { + require.Nil(t, sql.TruncateForTest("categories", "rooms", "schedule_groups", "schedules")) + + category := &types.Category{ + Name: "test category", + Description: "test category description", + } + err := sql.WithTx(context.Background(), func(tx *sql.Tx) error { + if err := tx.AddCategory(category); err != nil { + return err + } + return nil + }) + require.Nil(t, err) + room := &types.Room{ + Name: "test room", + Seats: 10, + CategoryId: category.Id, + } + err = sql.WithTx(context.Background(), func(tx *sql.Tx) error { + if err := tx.AddRoom(room); err != nil { + return err + } + return nil + }) + require.Nil(t, err) + + config.Config.AdminPermissionIdx = 9 + var users = []struct { + userIdx int + username string + permissionIdx int + }{ + {1, "doge", 1}, + {2, "foo", 1}, + {1000, "admin", config.Config.AdminPermissionIdx}, + } + { + body := types.AddScheduleReq{ + RoomId: room.Id, + Reservee: "doge", + Email: "doge@doge.com", + PhoneNumber: "010", + Reason: "bacchus", + StartTimestamp: 10000, + EndTimestamp: 10500, + Repeats: 1, + } + b, err := json.Marshal(body) + require.Nil(t, err) + req := httptest.NewRequest("POST", "/api/schedule/add", bytes.NewReader(b)) + setJWTToken(t, req, users[0].userIdx, users[0].username, users[0].permissionIdx) + w := httptest.NewRecorder() + + handler.HandleAddSchedule(w, req) + resp := w.Result() + require.Equal(t, resp.StatusCode, http.StatusOK) + } + { + body := types.AddScheduleReq{ + RoomId: room.Id, + Reservee: "foo", + Email: "foo@foo.com", + PhoneNumber: "010", + Reason: "bacchus2", + StartTimestamp: 10500, + EndTimestamp: 11000, + Repeats: 1, + } + b, err := json.Marshal(body) + require.Nil(t, err) + req := httptest.NewRequest("POST", "/api/schedule/add", bytes.NewReader(b)) + setJWTToken(t, req, users[1].userIdx, users[1].username, users[1].permissionIdx) + w := httptest.NewRecorder() + + handler.HandleAddSchedule(w, req) + resp := w.Result() + require.Equal(t, resp.StatusCode, http.StatusOK) + } + { + body := types.AddScheduleReq{ + RoomId: room.Id, + Reservee: "doge", + Email: "doge@doge.com", + PhoneNumber: "010", + Reason: "bacchus", + StartTimestamp: 11000, + EndTimestamp: 11500, + Repeats: 1, + } + b, err := json.Marshal(body) + require.Nil(t, err) + req := httptest.NewRequest("POST", "/api/schedule/add", bytes.NewReader(b)) + setJWTToken(t, req, users[0].userIdx, users[0].username, users[0].permissionIdx) + w := httptest.NewRecorder() + + handler.HandleAddSchedule(w, req) + resp := w.Result() + require.Equal(t, resp.StatusCode, http.StatusOK) + } + + var schedules []*types.Schedule + err = sql.WithTx(context.Background(), func(tx *sql.Tx) error { + if schedules, err = tx.GetSchedules(room.Id, 10000, 11500); err != nil { + return err + } + return nil + }) + require.Nil(t, err) + + { + // no jwt token + req := httptest.NewRequest("POST", "/api/schedule/delete", nil) + w := httptest.NewRecorder() + + handler.HandleDeleteSchedule(w, req) + resp := w.Result() + assert.Equal(t, http.StatusUnauthorized, resp.StatusCode) + } + { + // empty body + req := httptest.NewRequest("POST", "/api/schedule/delete", nil) + setJWTToken(t, req, users[0].userIdx, users[0].username, users[0].permissionIdx) + w := httptest.NewRecorder() + + handler.HandleDeleteSchedule(w, req) + resp := w.Result() + assert.Equal(t, http.StatusBadRequest, resp.StatusCode) + } + + var tests = []struct { + user struct { + userIdx int + username string + permissionIdx int + } + body types.DeleteScheduleReq + want int + }{ + // ok + { + user: users[0], + body: types.DeleteScheduleReq{ + ScheduleId: schedules[2].Id, + DeleteAllInGroup: false, + }, + want: http.StatusOK, + }, + // error - try to delete null schedule + { + user: users[0], + body: types.DeleteScheduleReq{ + ScheduleId: schedules[2].Id, + DeleteAllInGroup: false, + }, + want: http.StatusBadRequest, + }, + // error - try to delete the schules of other reservee + { + user: users[1], + body: types.DeleteScheduleReq{ + ScheduleId: schedules[0].Id, + DeleteAllInGroup: false, + }, + want: http.StatusBadRequest, + }, + // ok + { + user: users[1], + body: types.DeleteScheduleReq{ + ScheduleId: schedules[1].Id, + DeleteAllInGroup: false, + }, + want: http.StatusOK, + }, + // ok - admin can delete any schedules + { + user: users[2], + body: types.DeleteScheduleReq{ + ScheduleId: schedules[0].Id, + DeleteAllInGroup: false, + }, + want: http.StatusOK, + }, + } + + for num, tc := range tests { + b, err := json.Marshal(tc.body) + require.Nil(t, err) + req := httptest.NewRequest("POST", "/api/schedule/delete", bytes.NewReader(b)) + setJWTToken(t, req, tc.user.userIdx, tc.user.username, tc.user.permissionIdx) + w := httptest.NewRecorder() + + t.Log(tc) + + handler.HandleDeleteSchedule(w, req) + resp := w.Result() + assert.Equal(t, tc.want, resp.StatusCode, num) + } +}