forgejo/services/auth/reverseproxy_test.go
Mathieu Fenniak 82ad5189fe feat: add foreign keys to the access table (#9557)
Adds two foreign keys:
- `access.user_id` -> `user`
- `access.repo_id` -> `repository`

Testing:
- Existing automated test suite
    - Minor adjustments required to `reverseproxy_test.go`
    - Test failures caused a reorganization of delete operations in `DeleteRepositoryDirectly`
- Manually tested
    - On "user" and "repo" Collaborator page, added, removed, and changed the access-level of collaborators

## Checklist

The [contributor guide](https://forgejo.org/docs/next/contributor/) contains information that will be helpful to first time contributors. There also are a few [conditions for merging Pull Requests in Forgejo repositories](https://codeberg.org/forgejo/governance/src/branch/main/PullRequestsAgreement.md). You are also welcome to join the [Forgejo development chatroom](https://matrix.to/#/#forgejo-development:matrix.org).

### Tests

- I added test coverage for Go changes...
  - [ ] in their respective `*_test.go` for unit tests.
  - [ ] in the `tests/integration` directory if it involves interactions with a live Forgejo server.
- I added test coverage for JavaScript changes...
  - [ ] in `web_src/js/*.test.js` if it can be unit tested.
  - [ ] in `tests/e2e/*.test.e2e.js` if it requires interactions with a live Forgejo server (see also the [developer guide for JavaScript testing](https://codeberg.org/forgejo/forgejo/src/branch/forgejo/tests/e2e/README.md#end-to-end-tests)).

### Documentation

- [ ] I created a pull request [to the documentation](https://codeberg.org/forgejo/docs) to explain to Forgejo users how to use this change.
- [x] I did not document these changes and I do not expect someone else to do it.

### Release notes

- [ ] I do not want this change to show in the release notes.
- [x] I want the title to show in the release notes with a link to this pull request.
- [ ] I want the content of the `release-notes/<pull request number>.md` to be be used for the release notes instead of the title.

<!--start release-notes-assistant-->

## Release notes
<!--URL:https://codeberg.org/forgejo/forgejo-->
- Features
  - [PR](https://codeberg.org/forgejo/forgejo/pulls/9557): <!--number 9557 --><!--line 0 --><!--description YWRkIGZvcmVpZ24ga2V5cyB0byB0aGUgYGFjY2Vzc2AgdGFibGU=-->add foreign keys to the `access` table<!--description-->
<!--end release-notes-assistant-->

Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/9557
Reviewed-by: Gusted <gusted@noreply.codeberg.org>
Co-authored-by: Mathieu Fenniak <mathieu@fenniak.net>
Co-committed-by: Mathieu Fenniak <mathieu@fenniak.net>
2025-10-14 05:39:47 +02:00

69 lines
2.6 KiB
Go

// Copyright 2024 The Forgejo Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package auth
import (
"net/http"
"testing"
"forgejo.org/models/db"
"forgejo.org/models/issues"
access_model "forgejo.org/models/perm/access"
"forgejo.org/models/unittest"
user_model "forgejo.org/models/user"
"forgejo.org/modules/setting"
"forgejo.org/modules/test"
"github.com/stretchr/testify/require"
)
func TestReverseProxyAuth(t *testing.T) {
defer test.MockVariableValue(&setting.Service.EnableReverseProxyEmail, true)()
defer test.MockVariableValue(&setting.Service.EnableReverseProxyFullName, true)()
defer test.MockVariableValue(&setting.Service.EnableReverseProxyFullName, true)()
require.NoError(t, unittest.PrepareTestDatabase())
require.NoError(t, db.TruncateBeans(db.DefaultContext, &issues.TrackedTime{}, &issues.Stopwatch{}, &access_model.Access{}, &user_model.User{}))
require.EqualValues(t, 0, user_model.CountUsers(db.DefaultContext, nil))
t.Run("First user should be admin", func(t *testing.T) {
req, err := http.NewRequest("GET", "/", nil)
require.NoError(t, err)
req.Header.Add(setting.ReverseProxyAuthUser, "Edgar")
req.Header.Add(setting.ReverseProxyAuthFullName, "Edgar Allan Poe")
req.Header.Add(setting.ReverseProxyAuthEmail, "edgar@example.org")
rp := &ReverseProxy{}
user := rp.newUser(req)
require.EqualValues(t, 1, user_model.CountUsers(db.DefaultContext, nil))
unittest.AssertExistsAndLoadBean(t, &user_model.User{Email: "edgar@example.org", Name: "Edgar", LowerName: "edgar", FullName: "Edgar Allan Poe", IsAdmin: true})
require.Equal(t, "edgar@example.org", user.Email)
require.Equal(t, "Edgar", user.Name)
require.Equal(t, "edgar", user.LowerName)
require.Equal(t, "Edgar Allan Poe", user.FullName)
require.True(t, user.IsAdmin)
})
t.Run("Second user shouldn't be admin", func(t *testing.T) {
req, err := http.NewRequest("GET", "/", nil)
require.NoError(t, err)
req.Header.Add(setting.ReverseProxyAuthUser, " Gusted ")
req.Header.Add(setting.ReverseProxyAuthFullName, "❤‿❤")
req.Header.Add(setting.ReverseProxyAuthEmail, "gusted@example.org")
rp := &ReverseProxy{}
user := rp.newUser(req)
require.EqualValues(t, 2, user_model.CountUsers(db.DefaultContext, nil))
unittest.AssertExistsAndLoadBean(t, &user_model.User{Email: "gusted@example.org", Name: "Gusted", LowerName: "gusted", FullName: "❤‿❤"}, "is_admin = false")
require.Equal(t, "gusted@example.org", user.Email)
require.Equal(t, "Gusted", user.Name)
require.Equal(t, "gusted", user.LowerName)
require.Equal(t, "❤‿❤", user.FullName)
require.False(t, user.IsAdmin)
})
}