Skip to content

Add unit tests for requireWriteScope middleware#713

Merged
rickyrombo merged 2 commits intomjp-require-scopefrom
copilot/sub-pr-711
Mar 11, 2026
Merged

Add unit tests for requireWriteScope middleware#713
rickyrombo merged 2 commits intomjp-require-scopefrom
copilot/sub-pr-711

Conversation

Copy link
Contributor

Copilot AI commented 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.
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

✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

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 requireWriteScope middleware Mar 11, 2026
@rickyrombo rickyrombo marked this pull request as ready for review March 11, 2026 02:39
@rickyrombo rickyrombo merged commit ecaa886 into mjp-require-scope Mar 11, 2026
4 checks passed
@rickyrombo rickyrombo deleted the copilot/sub-pr-711 branch March 11, 2026 02:44
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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants