Skip to content

Commit

Permalink
HandleDeleteSchedule 테스트 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
cseteram committed Mar 27, 2022
1 parent 0114101 commit 798b077
Show file tree
Hide file tree
Showing 2 changed files with 202 additions and 1 deletion.
2 changes: 1 addition & 1 deletion backend/handler/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
201 changes: 201 additions & 0 deletions backend/handler/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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: "[email protected]",
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: "[email protected]",
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: "[email protected]",
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)
}
}

0 comments on commit 798b077

Please sign in to comment.