forgejo/tests/integration/api_forgejo_version_test.go
fnoopv b98c8f696c fix: /api/forgejo/v1/version Content-Type error (#9897)
fix: #9782
## 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.
- [ ] 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.
- [ ] 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.

Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/9897
Reviewed-by: Gusted <gusted@noreply.codeberg.org>
Reviewed-by: Cyborus <cyborus@noreply.codeberg.org>
Co-authored-by: fnoopv <fnoopv@noreply.codeberg.org>
Co-committed-by: fnoopv <fnoopv@noreply.codeberg.org>
2025-11-05 17:35:50 +01:00

66 lines
1.9 KiB
Go

// Copyright The Forgejo Authors.
// SPDX-License-Identifier: MIT
package integration
import (
"net/http"
"testing"
auth_model "forgejo.org/models/auth"
"forgejo.org/modules/setting"
"forgejo.org/modules/test"
"forgejo.org/routers"
v1 "forgejo.org/routers/api/forgejo/v1"
"forgejo.org/tests"
"github.com/stretchr/testify/assert"
)
func TestAPIForgejoVersion(t *testing.T) {
defer tests.PrepareTestEnv(t)()
t.Run("Version", func(t *testing.T) {
req := NewRequest(t, "GET", "/api/forgejo/v1/version")
resp := MakeRequest(t, req, http.StatusOK)
var version v1.Version
DecodeJSON(t, resp, &version)
assert.Equal(t, "1.0.0", *version.Version)
})
t.Run("Version with Content-Type is json", func(t *testing.T) {
req := NewRequest(t, "GET", "/api/forgejo/v1/version")
resp := MakeRequest(t, req, http.StatusOK)
assert.Equal(t, "application/json; charset=utf-8", resp.Header().Get("Content-Type"))
})
t.Run("Versions with REQUIRE_SIGNIN_VIEW enabled", func(t *testing.T) {
defer test.MockVariableValue(&setting.Service.RequireSignInView, true)()
defer test.MockVariableValue(&testWebRoutes, routers.NormalRoutes())()
t.Run("Get forgejo version without auth", func(t *testing.T) {
defer tests.PrintCurrentTest(t)()
// GET api without auth
req := NewRequest(t, "GET", "/api/forgejo/v1/version")
MakeRequest(t, req, http.StatusForbidden)
})
t.Run("Get forgejo version without auth", func(t *testing.T) {
defer tests.PrintCurrentTest(t)()
username := "user1"
session := loginUser(t, username)
token := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeWriteRepository)
// GET api with auth
req := NewRequest(t, "GET", "/api/forgejo/v1/version").AddTokenAuth(token)
resp := MakeRequest(t, req, http.StatusOK)
var version v1.Version
DecodeJSON(t, resp, &version)
assert.Equal(t, "1.0.0", *version.Version)
})
})
}