Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix a couple API bugs #138

Merged
merged 3 commits into from
Feb 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions api.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,9 @@ func IsDeprecatedAPIPath(path_ string) mo.Option[int] {
match := re.FindStringSubmatch(split[3])
if len(match) == 2 {
version, err := strconv.Atoi(match[1])
if err != nil {
return mo.None[int]()
if err == nil && version != API_MAJOR_VERSION {
return mo.Some(version)
}
return mo.Some(version)
}
}

Expand Down
8 changes: 7 additions & 1 deletion api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,10 +104,16 @@ func (ts *TestSuite) testAPIGetUser(t *testing.T) {
assert.Nil(t, json.NewDecoder(rec.Body).Decode(&response))
assert.Equal(t, nonAdmin.UUID, response.UUID)

// nonexistent user should get StatusNotFound
var err APIError
rec = ts.Get(t, ts.Server, DRASL_API_PREFIX+"/users/00000000-0000-0000-0000-000000000000", nil, &admin.APIToken)
assert.Equal(t, http.StatusNotFound, rec.Code)
assert.Nil(t, json.NewDecoder(rec.Body).Decode(&err))
assert.Equal(t, "Unknown UUID", err.Message)

// user2 (not admin) should get a StatusForbidden
rec = ts.Get(t, ts.Server, DRASL_API_PREFIX+"/users/"+admin.UUID, nil, &nonAdmin.APIToken)
assert.Equal(t, http.StatusForbidden, rec.Code)
var err APIError
assert.Nil(t, json.NewDecoder(rec.Body).Decode(&err))

assert.Nil(t, ts.App.DeleteUser(&GOD, admin))
Expand Down
3 changes: 3 additions & 0 deletions user.go
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,9 @@ func (app *App) UpdateUser(
if !callerIsAdmin {
return User{}, NewBadRequestUserError("Cannot change admin status of user without having admin privileges yourself.")
}
if !(*isAdmin) && app.IsDefaultAdmin(&user) {
return User{}, NewBadRequestUserError("Cannot revoke admin status of a default admin.")
}
user.IsAdmin = *isAdmin
}

Expand Down
Loading