-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
202 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) | ||
} | ||
} |