Skip to content

Commit

Permalink
Fixups, invite tests
Browse files Browse the repository at this point in the history
  • Loading branch information
evan-goode committed Jul 9, 2024
1 parent 8e1b44d commit 418c49d
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 8 deletions.
19 changes: 11 additions & 8 deletions api.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ import (
"time"
)

// @title Drasl API
// @version 1.0
// @title Drasl API
// @version 1.0
// @description Manage Drasl users and invitations

// @contact.name Unmojang
// @contact.url https://github.com/unmojang/drasl
Expand Down Expand Up @@ -231,12 +232,13 @@ func (app *App) APIGetSelf() func(c echo.Context) error {
// @Tags users
// @Accept json
// @Produce json
// @Success 200 {object} APIUser
// @Failure 400 {object} APIError
// @Failure 401 {object} APIError
// @Failure 403 {object} APIError
// @Failure 404 {object} APIError
// @Failure 500 {object} APIError
// @Param uuid path string true "User UUID"
// @Success 200 {object} APIUser
// @Failure 400 {object} APIError
// @Failure 401 {object} APIError
// @Failure 403 {object} APIError
// @Failure 404 {object} APIError
// @Failure 500 {object} APIError
// @Router /drasl/api/v1/users/{uuid} [get]
func (app *App) APIGetUser() func(c echo.Context) error {
return app.withAPITokenAdmin(func(c echo.Context, user *User) error {
Expand Down Expand Up @@ -624,6 +626,7 @@ func (app *App) APICreateInvite() func(c echo.Context) error {
// @Tags invites
// @Accept json
// @Produce json
// @Param code path string true "Invite code"
// @Success 204
// @Failure 403 {object} APIError
// @Failure 404 {object} APIError
Expand Down
76 changes: 76 additions & 0 deletions api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ func TestAPI(t *testing.T) {
t.Run("Test DELETE /drasl/api/v1/users/{uuid}", ts.testAPIDeleteUser)
t.Run("Test DELETE /drasl/api/v1/user", ts.testAPIDeleteSelf)
t.Run("Test GET /drasl/api/v1/challenge-skin", ts.testAPIGetChallengeSkin)
t.Run("Test GET /drasl/api/v1/invites", ts.testAPIGetInvites)
t.Run("Test POST /drasl/api/v1/invites", ts.testAPICreateInvite)
}
}

Expand Down Expand Up @@ -198,3 +200,77 @@ func (ts *TestSuite) testAPIGetChallengeSkin(t *testing.T) {
var challenge APIChallenge
assert.Nil(t, json.NewDecoder(rec.Body).Decode(&challenge))
}

func (ts *TestSuite) testAPIGetInvites(t *testing.T) {
username1 := "admin"
admin, _ := ts.CreateTestUser(ts.Server, username1)
username2 := "user2"
user2, _ := ts.CreateTestUser(ts.Server, username2)

_, err := ts.App.CreateInvite()
assert.Nil(t, err)
_, err = ts.App.CreateInvite()
assert.Nil(t, err)

var invites []Invite
result := ts.App.DB.Find(&invites)
assert.Nil(t, result.Error)
inviteCount := len(invites)

assert.Equal(t, 2, inviteCount)

// admin should get a response
rec := ts.Get(t, ts.Server, "/drasl/api/v1/invites", nil, &admin.APIToken)
assert.Equal(t, http.StatusOK, rec.Code)
var response []APIUser
assert.Nil(t, json.NewDecoder(rec.Body).Decode(&response))
assert.Equal(t, inviteCount, len(response))

// user2 (not admin) should get a StatusForbidden
rec = ts.Get(t, ts.Server, "/drasl/api/v1/invites", nil, &user2.APIToken)
assert.Equal(t, http.StatusForbidden, rec.Code)
var apiErr APIError
assert.Nil(t, json.NewDecoder(rec.Body).Decode(&apiErr))

assert.Nil(t, ts.App.DB.Delete(&admin).Error)
assert.Nil(t, ts.App.DB.Delete(&user2).Error)

for _, invite := range invites {
assert.Nil(t, ts.App.DB.Delete(invite).Error)
}
}

func (ts *TestSuite) testAPICreateInvite(t *testing.T) {
username1 := "admin"
admin, _ := ts.CreateTestUser(ts.Server, username1)
username2 := "user2"
user2, _ := ts.CreateTestUser(ts.Server, username2)

var invites []Invite
result := ts.App.DB.Find(&invites)
assert.Nil(t, result.Error)
assert.Equal(t, 0, len(invites))

// admin should get a response
rec := ts.PostJSON(t, ts.Server, "/drasl/api/v1/invites", nil, nil, &admin.APIToken)
assert.Equal(t, http.StatusOK, rec.Code)
var response APIInvite
assert.Nil(t, json.NewDecoder(rec.Body).Decode(&response))

// user2 (not admin) should get a StatusForbidden
rec = ts.Get(t, ts.Server, "/drasl/api/v1/invites", nil, &user2.APIToken)
assert.Equal(t, http.StatusForbidden, rec.Code)
var apiErr APIError
assert.Nil(t, json.NewDecoder(rec.Body).Decode(&apiErr))

assert.Nil(t, ts.App.DB.Delete(&admin).Error)
assert.Nil(t, ts.App.DB.Delete(&user2).Error)

result = ts.App.DB.Find(&invites)
assert.Nil(t, result.Error)
assert.Equal(t, 1, len(invites))

for _, invite := range invites {
assert.Nil(t, ts.App.DB.Delete(invite).Error)
}
}
19 changes: 19 additions & 0 deletions swagger.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"swagger": "2.0",
"info": {
"description": "Manage Drasl users and invitations",
"title": "Drasl API",
"contact": {
"name": "Unmojang",
Expand Down Expand Up @@ -55,6 +56,15 @@
"invites"
],
"summary": "Delete an invite",
"parameters": [
{
"type": "string",
"description": "Invite code",
"name": "code",
"in": "path",
"required": true
}
],
"responses": {
"204": {
"description": "No Content"
Expand Down Expand Up @@ -385,6 +395,15 @@
"users"
],
"summary": "Get user by UUID",
"parameters": [
{
"type": "string",
"description": "User UUID",
"name": "uuid",
"in": "path",
"required": true
}
],
"responses": {
"200": {
"description": "OK",
Expand Down

0 comments on commit 418c49d

Please sign in to comment.