From 5da13e7f935f3effb077c46c71ca7d632d41ebd7 Mon Sep 17 00:00:00 2001 From: Pyeongseok Oh Date: Sun, 27 Mar 2022 16:40:43 +0900 Subject: [PATCH 1/4] Add tests array --- backend/handler/handler_test.go | 249 ++++++++++++++------------------ 1 file changed, 108 insertions(+), 141 deletions(-) diff --git a/backend/handler/handler_test.go b/backend/handler/handler_test.go index 2eca149..d77a1ae 100644 --- a/backend/handler/handler_test.go +++ b/backend/handler/handler_test.go @@ -194,94 +194,6 @@ func TestHandleAddSchedule(t *testing.T) { assert.Equal(t, http.StatusBadRequest, resp.StatusCode) } config.Config.ScheduleRepeatLimit = 10 - { - // less than one repeats - body := types.AddScheduleReq{ - RoomId: 1, - Reservee: "doge", - Email: "doge@foo.com", - PhoneNumber: "010", - Reason: "bacchus", - StartTimestamp: 10000, - EndTimestamp: 11000, - Repeats: 0, - } - b, err := json.Marshal(body) - require.Nil(t, err) - req := httptest.NewRequest("POST", "/api/schedule/add", bytes.NewReader(b)) - setJWTToken(t, req, userIdx, username, permissionIdx) - w := httptest.NewRecorder() - - handler.HandleAddSchedule(w, req) - resp := w.Result() - assert.Equal(t, http.StatusBadRequest, resp.StatusCode) - } - { - // too many repeats - body := types.AddScheduleReq{ - RoomId: 1, - Reservee: "doge", - Email: "doge@foo.com", - PhoneNumber: "010", - Reason: "bacchus", - StartTimestamp: 10000, - EndTimestamp: 11000, - Repeats: config.Config.ScheduleRepeatLimit + 100, - } - b, err := json.Marshal(body) - require.Nil(t, err) - req := httptest.NewRequest("POST", "/api/schedule/add", bytes.NewReader(b)) - setJWTToken(t, req, userIdx, username, permissionIdx) - w := httptest.NewRecorder() - - handler.HandleAddSchedule(w, req) - resp := w.Result() - assert.Equal(t, http.StatusBadRequest, resp.StatusCode) - } - { - // invalid time range - body := types.AddScheduleReq{ - RoomId: 1, - Reservee: "doge", - Email: "doge@foo.com", - PhoneNumber: "010", - Reason: "bacchus", - StartTimestamp: 11000, - EndTimestamp: 10000, - Repeats: 1, - } - b, err := json.Marshal(body) - require.Nil(t, err) - req := httptest.NewRequest("POST", "/api/schedule/add", bytes.NewReader(b)) - setJWTToken(t, req, userIdx, username, permissionIdx) - w := httptest.NewRecorder() - - handler.HandleAddSchedule(w, req) - resp := w.Result() - assert.Equal(t, http.StatusBadRequest, resp.StatusCode) - } - { - // invalid room - body := types.AddScheduleReq{ - RoomId: 123, - Reservee: "doge", - Email: "doge@foo.com", - PhoneNumber: "010", - Reason: "bacchus", - StartTimestamp: 10000, - 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, userIdx, username, permissionIdx) - w := httptest.NewRecorder() - - handler.HandleAddSchedule(w, req) - resp := w.Result() - assert.Equal(t, http.StatusBadRequest, resp.StatusCode) - } category := &types.Category{ Name: "test category", Description: "test category description", @@ -306,63 +218,118 @@ func TestHandleAddSchedule(t *testing.T) { }) require.Nil(t, err) - { + var tests = []struct { + body types.AddScheduleReq + want int + }{ + // less than one repeats + { + types.AddScheduleReq{ + RoomId: 1, + Reservee: "doge", + Email: "doge@foo.com", + PhoneNumber: "010", + Reason: "bacchus", + StartTimestamp: 10000, + EndTimestamp: 11000, + Repeats: 0, + }, + http.StatusBadRequest, + }, + + // too many repeats + { + types.AddScheduleReq{ + RoomId: 1, + Reservee: "doge", + Email: "doge@foo.com", + PhoneNumber: "010", + Reason: "bacchus", + StartTimestamp: 10000, + EndTimestamp: 11000, + Repeats: config.Config.ScheduleRepeatLimit + 100, + }, + http.StatusBadRequest, + }, + + // invalid time range + { + types.AddScheduleReq{ + RoomId: 1, + Reservee: "doge", + Email: "doge@foo.com", + PhoneNumber: "010", + Reason: "bacchus", + StartTimestamp: 11000, + EndTimestamp: 10000, + Repeats: 1, + }, + http.StatusBadRequest, + }, + + // invalid room + { + types.AddScheduleReq{ + RoomId: 123, + Reservee: "doge", + Email: "doge@foo.com", + PhoneNumber: "010", + Reason: "bacchus", + StartTimestamp: 10000, + EndTimestamp: 11000, + Repeats: 1, + }, + http.StatusBadRequest, + }, + // ok - body := types.AddScheduleReq{ - RoomId: room.Id, - Reservee: "doge", - Email: "doge@foo.com", - PhoneNumber: "010", - Reason: "bacchus", - StartTimestamp: 10000, - 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, userIdx, username, permissionIdx) - w := httptest.NewRecorder() + { + types.AddScheduleReq{ + RoomId: room.Id, + Reservee: "doge", + Email: "doge@foo.com", + PhoneNumber: "010", + Reason: "bacchus", + StartTimestamp: 10000, + EndTimestamp: 11000, + Repeats: 1, + }, + http.StatusOK, + }, - handler.HandleAddSchedule(w, req) - resp := w.Result() - assert.Equal(t, http.StatusOK, resp.StatusCode) - } - { // overlapping time range - body := types.AddScheduleReq{ - RoomId: room.Id, - Reservee: "doge", - Email: "doge@foo.com", - PhoneNumber: "010", - Reason: "bacchus", - StartTimestamp: 10500, - 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, userIdx, username, permissionIdx) - w := httptest.NewRecorder() + { + types.AddScheduleReq{ + RoomId: room.Id, + Reservee: "doge", + Email: "doge@foo.com", + PhoneNumber: "010", + Reason: "bacchus", + StartTimestamp: 10500, + EndTimestamp: 11500, + Repeats: 1, + }, + http.StatusBadRequest, + }, - handler.HandleAddSchedule(w, req) - resp := w.Result() - assert.Equal(t, http.StatusBadRequest, resp.StatusCode) - } - { // repeat - body := types.AddScheduleReq{ - RoomId: room.Id, - Reservee: "doge", - Email: "doge@foo.com", - PhoneNumber: "010", - Reason: "bacchus", - StartTimestamp: 11000, - EndTimestamp: 12000, - Repeats: config.Config.ScheduleRepeatLimit, - } - b, err := json.Marshal(body) + { + types.AddScheduleReq{ + RoomId: room.Id, + Reservee: "doge", + Email: "doge@foo.com", + PhoneNumber: "010", + Reason: "bacchus", + StartTimestamp: 11000, + EndTimestamp: 12000, + Repeats: config.Config.ScheduleRepeatLimit, + }, + http.StatusOK, + }, + } + + for _, tc := range tests { + b, err := json.Marshal(tc.body) require.Nil(t, err) req := httptest.NewRequest("POST", "/api/schedule/add", bytes.NewReader(b)) setJWTToken(t, req, userIdx, username, permissionIdx) @@ -370,6 +337,6 @@ func TestHandleAddSchedule(t *testing.T) { handler.HandleAddSchedule(w, req) resp := w.Result() - assert.Equal(t, http.StatusOK, resp.StatusCode) + assert.Equal(t, tc.want, resp.StatusCode) } } From dc62a8384d76a2839da2b82c1b344800b543456d Mon Sep 17 00:00:00 2001 From: Pyeongseok Oh Date: Sun, 27 Mar 2022 19:14:45 +0900 Subject: [PATCH 2/4] =?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) + } +} From 53f0b057e96ea3e8fb51dae9151eadb59caa5276 Mon Sep 17 00:00:00 2001 From: Pyeongseok Oh Date: Sun, 3 Apr 2022 23:04:37 +0900 Subject: [PATCH 3/4] Update sql.go to prepare db for test --- backend/sql/sql.go | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/backend/sql/sql.go b/backend/sql/sql.go index 4085fc1..6c57705 100644 --- a/backend/sql/sql.go +++ b/backend/sql/sql.go @@ -7,6 +7,7 @@ import ( "fmt" "strings" "time" + "io/ioutil" "github.com/bacchus-snu/reservation/config" "github.com/bacchus-snu/reservation/types" @@ -60,6 +61,32 @@ func TruncateForTest(tableName ...string) error { return nil } +func DropTableForTest(tableName ...string) error { + if !config.Config.IsTest { + panic("this function should be called only in test") + } + _, err := db.Exec(fmt.Sprintf("drop table %s", strings.Join(tableName, ","))) + if err != nil { + return err + } + return nil +} + +func LoadScriptForTest(filePath string) error { + if !config.Config.IsTest { + panic("this function should be called only in test") + } + buf, err := ioutil.ReadFile(filePath) + if err != nil { + return err + } + _, err = db.Exec(string(buf)) + if err != nil { + return err + } + return nil +} + type Tx struct { tx *sql.Tx } From c3dae1b5f1572e55fecf3088531bf2ad28e21e03 Mon Sep 17 00:00:00 2001 From: Pyeongseok Oh Date: Sun, 10 Apr 2022 19:16:32 +0900 Subject: [PATCH 4/4] Add tests for HandleAddRoom and HandleAddCategory --- backend/handler/handler_test.go | 238 ++++++++++++++++++++++++++++++-- 1 file changed, 223 insertions(+), 15 deletions(-) diff --git a/backend/handler/handler_test.go b/backend/handler/handler_test.go index 68d80d2..80c8b5d 100644 --- a/backend/handler/handler_test.go +++ b/backend/handler/handler_test.go @@ -222,10 +222,10 @@ func TestHandleAddSchedule(t *testing.T) { body types.AddScheduleReq want int }{ - // less than one repeats + // #0: error (less than one repeats) { types.AddScheduleReq{ - RoomId: 1, + RoomId: room.Id, Reservee: "doge", Email: "doge@foo.com", PhoneNumber: "010", @@ -237,10 +237,10 @@ func TestHandleAddSchedule(t *testing.T) { http.StatusBadRequest, }, - // too many repeats + // #1: error (too many repeats) { types.AddScheduleReq{ - RoomId: 1, + RoomId: room.Id, Reservee: "doge", Email: "doge@foo.com", PhoneNumber: "010", @@ -252,10 +252,10 @@ func TestHandleAddSchedule(t *testing.T) { http.StatusBadRequest, }, - // invalid time range + // #2: error (invalid time range) { types.AddScheduleReq{ - RoomId: 1, + RoomId: room.Id, Reservee: "doge", Email: "doge@foo.com", PhoneNumber: "010", @@ -267,10 +267,10 @@ func TestHandleAddSchedule(t *testing.T) { http.StatusBadRequest, }, - // invalid room + // #3: error (invalid room) { types.AddScheduleReq{ - RoomId: 123, + RoomId: room.Id + 123, Reservee: "doge", Email: "doge@foo.com", PhoneNumber: "010", @@ -282,7 +282,7 @@ func TestHandleAddSchedule(t *testing.T) { http.StatusBadRequest, }, - // ok + // #4: ok { types.AddScheduleReq{ RoomId: room.Id, @@ -297,7 +297,7 @@ func TestHandleAddSchedule(t *testing.T) { http.StatusOK, }, - // overlapping time range + // #5: error (overlapping time range) { types.AddScheduleReq{ RoomId: room.Id, @@ -312,7 +312,7 @@ func TestHandleAddSchedule(t *testing.T) { http.StatusBadRequest, }, - // repeat + // #6: ok (repeat) { types.AddScheduleReq{ RoomId: room.Id, @@ -326,9 +326,24 @@ func TestHandleAddSchedule(t *testing.T) { }, http.StatusOK, }, + + // #7: error (overlapping time range) + { + types.AddScheduleReq{ + RoomId: room.Id, + Reservee: "doge", + Email: "doge@foo.com", + PhoneNumber: "010", + Reason: "bacchus", + StartTimestamp: 11000 + 7*24*60*60, + EndTimestamp: 12000 + 7*24*60*60, + Repeats: 1, + }, + http.StatusBadRequest, + }, } - for _, tc := range tests { + for idx, tc := range tests { b, err := json.Marshal(tc.body) require.Nil(t, err) req := httptest.NewRequest("POST", "/api/schedule/add", bytes.NewReader(b)) @@ -337,7 +352,7 @@ func TestHandleAddSchedule(t *testing.T) { handler.HandleAddSchedule(w, req) resp := w.Result() - assert.Equal(t, tc.want, resp.StatusCode) + assert.Equal(t, tc.want, resp.StatusCode, idx) } } @@ -527,7 +542,7 @@ func TestHandleDeleteSchedule(t *testing.T) { }, } - for num, tc := range tests { + for idx, tc := range tests { b, err := json.Marshal(tc.body) require.Nil(t, err) req := httptest.NewRequest("POST", "/api/schedule/delete", bytes.NewReader(b)) @@ -538,6 +553,199 @@ func TestHandleDeleteSchedule(t *testing.T) { handler.HandleDeleteSchedule(w, req) resp := w.Result() - assert.Equal(t, tc.want, resp.StatusCode, num) + assert.Equal(t, tc.want, resp.StatusCode, idx) + } +} + +func TestGetSchedule(t *testing.T) { + // TODO +} + +func TestGetScheduleInfo(t *testing.T) { + // TODO +} + +func TestHandleGetRoomsAndCategories(t *testing.T) { + // TODO +} + +func TestHandleAddRoom(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) + config.Config.AdminPermissionIdx = 9 + var users = []struct { + userIdx int + username string + permissionIdx int + }{ + {1, "doge", 1}, + {1000, "admin", config.Config.AdminPermissionIdx}, + } + + { + // no jwt token + req := httptest.NewRequest("POST", "/api/rooms/add", nil) + w := httptest.NewRecorder() + + handler.HandleAddRoom(w, req) + resp := w.Result() + assert.Equal(t, http.StatusUnauthorized, resp.StatusCode) + } + { + // empty body + req := httptest.NewRequest("POST", "/api/rooms/add", nil) + setJWTToken(t, req, users[1].userIdx, users[1].username, users[1].permissionIdx) + w := httptest.NewRecorder() + + handler.HandleAddRoom(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.AddRoomReq + want int + }{ + // #0: ok + { + user: users[1], + body: types.AddRoomReq{ + Name: "test room", + Seats: 10, + CategoryId: category.Id, + }, + want: http.StatusOK, + }, + + // #1: error (non-admin user cannot add room) + { + user: users[0], + body: types.AddRoomReq{ + Name: "test room", + Seats: 10, + CategoryId: category.Id, + }, + want: http.StatusUnauthorized, + }, + + // #2: error (invalid category id) + { + user: users[1], + body: types.AddRoomReq{ + Name: "test room", + Seats: 10, + CategoryId: category.Id + 123, + }, + want: http.StatusBadRequest, + }, + } + + for idx, tc := range tests { + b, err := json.Marshal(tc.body) + require.Nil(t, err) + req := httptest.NewRequest("POST", "/api/rooms/add", bytes.NewReader(b)) + setJWTToken(t, req, tc.user.userIdx, tc.user.username, tc.user.permissionIdx) + w := httptest.NewRecorder() + + handler.HandleAddRoom(w, req) + resp := w.Result() + assert.Equal(t, tc.want, resp.StatusCode, idx) } } + +func TestHandleAddCategory(t *testing.T) { + require.Nil(t, sql.TruncateForTest("categories", "rooms", "schedule_groups", "schedules")) + config.Config.AdminPermissionIdx = 9 + var users = []struct { + userIdx int + username string + permissionIdx int + }{ + {1, "doge", 1}, + {1000, "admin", config.Config.AdminPermissionIdx}, + } + + { + // no jwt token + req := httptest.NewRequest("POST", "/api/categories/add", nil) + w := httptest.NewRecorder() + + handler.HandleAddCategory(w, req) + resp := w.Result() + assert.Equal(t, http.StatusUnauthorized, resp.StatusCode) + } + { + // empty body + req := httptest.NewRequest("POST", "/api/categories/add", nil) + setJWTToken(t, req, users[1].userIdx, users[1].username, users[1].permissionIdx) + w := httptest.NewRecorder() + + handler.HandleAddCategory(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.AddCategoryReq + want int + }{ + // #0: ok + { + user: users[1], + body: types.AddCategoryReq{ + Name: "test category", + Description: "test category description", + }, + want: http.StatusOK, + }, + // #1: error (non-admin user cannot add category) + { + user: users[0], + body: types.AddCategoryReq{ + Name: "test category", + Description: "test category description", + }, + want: http.StatusUnauthorized, + }, + } + + for idx, tc := range tests { + b, err := json.Marshal(tc.body) + require.Nil(t, err) + req := httptest.NewRequest("POST", "/api/categories/add", bytes.NewReader(b)) + setJWTToken(t, req, tc.user.userIdx, tc.user.username, tc.user.permissionIdx) + w := httptest.NewRecorder() + + handler.HandleAddCategory(w, req) + resp := w.Result() + assert.Equal(t, tc.want, resp.StatusCode, idx) + } +} + +func TestHandleDeleteRoom(t *testing.T) { + // TODO +} + +func TestHandleDeleteCategory(t *testing.T) { + // TODO +}