Add unit tests for requireWriteScope middleware#713
Merged
rickyrombo merged 2 commits intomjp-require-scopefrom Mar 11, 2026
Merged
Add unit tests for requireWriteScope middleware#713rickyrombo merged 2 commits intomjp-require-scopefrom
requireWriteScope middleware#713rickyrombo merged 2 commits intomjp-require-scopefrom
Conversation
Co-authored-by: rickyrombo <3690498+rickyrombo@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] WIP address feedback on requiring write scope in OAuth handling
Add unit tests for Mar 11, 2026
requireWriteScope middleware
rickyrombo
added a commit
that referenced
this pull request
Mar 11, 2026
`requireWriteScope` lacked test coverage for its three distinct code
paths: PKCE tokens with `scope=read` being rejected, `scope=write` being
allowed, and non-OAuth auth methods bypassing the scope check entirely.
## Changes
- **`api/auth_middleware_test.go`**: Adds `TestRequireWriteScope` with
three sub-tests covering each path. Since `requireWriteScope` only reads
`c.Locals("oauthScope")` and never touches the receiver, tests use a
bare `&ApiServer{}` — no DB required.
```go
testApp.Post("/write", func(c *fiber.Ctx) error {
if scope := c.Get("X-Test-Oauth-Scope"); scope != "" {
c.Locals("oauthScope", scope)
}
return c.Next()
}, app.requireWriteScope, ...)
// read scope → 403
// write scope → 200
// no scope (non-OAuth) → 200
```
<!-- START COPILOT CODING AGENT TIPS -->
---
✨ Let Copilot coding agent [set things up for
you](https://github.com/AudiusProject/api/issues/new?title=✨+Set+up+Copilot+instructions&body=Configure%20instructions%20for%20this%20repository%20as%20documented%20in%20%5BBest%20practices%20for%20Copilot%20coding%20agent%20in%20your%20repository%5D%28https://gh.io/copilot-coding-agent-tips%29%2E%0A%0A%3COnboard%20this%20repo%3E&assignees=copilot)
— coding agent works faster and does higher quality work when set up for
your repo.
---------
Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: rickyrombo <3690498+rickyrombo@users.noreply.github.com>
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
requireWriteScopelacked test coverage for its three distinct code paths: PKCE tokens withscope=readbeing rejected,scope=writebeing allowed, and non-OAuth auth methods bypassing the scope check entirely.Changes
api/auth_middleware_test.go: AddsTestRequireWriteScopewith three sub-tests covering each path. SincerequireWriteScopeonly readsc.Locals("oauthScope")and never touches the receiver, tests use a bare&ApiServer{}— no DB required.✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.