forgejo/tests/integration/web_misc_test.go
Mai-Lapyst ed63f06d79 Move web app manifest to a own cache-able route and add a setting to set "display": "standalone"; Closes #2638 (#5384)
This PR does three things:
- First it moves the inline web app manifest into its own route `/manifest.json`
- Secondly, it add a setting `pwa.STANDALONE` that can be set to `true` if one wants users to be allowed to "install" forgejo as an pwa into their browser. This usually means an "install app" button, which essentially just creates an shortcut to use a single-tab window for browsing the app / forgejo.
- Thirdly since we have now an extra route, it checks if someone placed a `public/manifest.json` in forgejo's custom path; if yes, it's content is served instead. This allows more customization without the need on our side to completly implement every nuance of web app manifests.

This closes issue #2638

### Tests

- I added test coverage for Go changes...
  - [x] in their respective `*_test.go` for unit tests.

### Documentation

- [x] I created a pull request [to the documentation](https://codeberg.org/forgejo/docs/pulls/1669) to explain to Forgejo users how to use this change.

### 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.
- [x] 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/5384): <!--number 5384 --><!--line 0 --><!--description W2FsbG93IGZvcmdlam8gdG8gcnVuIGFzIGEgcHdhIHN0YW5kYWxvbmUgYXBwbGljYXRpb24gJiBvdmVycmlkZSBvZiB0aGUgd2ViYXBwIG1hbmlmZXN0Lmpzb24gdmlhIHRoZSBhIGN1c3RvbSBmaWxlIGluIGBwdWJsaWMvbWFuaWZlc3QuanNvbmBdKGh0dHBzOi8vY29kZWJlcmcub3JnL2Zvcmdlam8vZm9yZ2Vqby9wdWxscy81Mzg0KQ==-->[allow forgejo to run as a pwa standalone application & override of the webapp manifest.json via the a custom file in `public/manifest.json`](https://codeberg.org/forgejo/forgejo/pulls/5384)<!--description-->
<!--end release-notes-assistant-->

Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/5384
Reviewed-by: Otto <otto@codeberg.org>
Reviewed-by: Lucas <sclu1034@noreply.codeberg.org>
Co-authored-by: Mai-Lapyst <mai-lapyst@noreply.codeberg.org>
Co-committed-by: Mai-Lapyst <mai-lapyst@noreply.codeberg.org>
2026-01-09 17:49:29 +01:00

77 lines
2.2 KiB
Go

// Copyright 2026 The Forgejo Authors. All rights reserved.
// SPDX-License-Identifier: GPL-3.0-or-later
package integration
import (
"net/http"
"os"
"testing"
"forgejo.org/modules/setting"
"forgejo.org/modules/test"
"forgejo.org/modules/util"
"forgejo.org/tests"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestManifestJson(t *testing.T) {
defer tests.PrepareTestEnv(t)()
session := loginUser(t, "user2")
req := NewRequest(t, "GET", "/manifest.json")
resp := session.MakeRequest(t, req, http.StatusOK)
data := make(map[string]any)
DecodeJSON(t, resp, &data)
assert.Equal(t, setting.AppName, data["name"])
assert.Equal(t, setting.AppName, data["short_name"])
assert.Equal(t, setting.AppURL, data["start_url"])
assert.NotContains(t, data, "display")
}
func TestManifestJsonStandalone(t *testing.T) {
defer tests.PrepareTestEnv(t)()
defer test.MockVariableValue(&setting.PWA.Standalone, true)()
session := loginUser(t, "user2")
req := NewRequest(t, "GET", "/manifest.json")
resp := session.MakeRequest(t, req, http.StatusOK)
data := make(map[string]any)
DecodeJSON(t, resp, &data)
assert.Equal(t, setting.AppName, data["name"])
assert.Equal(t, setting.AppName, data["short_name"])
assert.Equal(t, setting.AppURL, data["start_url"])
assert.Contains(t, data, "display")
assert.Equal(t, "standalone", data["display"])
}
func TestManifestJsonCustomFile(t *testing.T) {
require.NoError(t, os.MkdirAll(util.FilePathJoinAbs(setting.CustomPath, "public"), 0o777))
manifestPath := util.FilePathJoinAbs(setting.CustomPath, "public/manifest.json")
file, err := os.OpenFile(manifestPath, os.O_CREATE|os.O_RDWR, 0o777)
require.NoError(t, err)
_, err = file.Write([]byte(`{"name":"MyCustomJson"}`))
require.NoError(t, err)
require.NoError(t, file.Close())
defer os.Remove(manifestPath)
defer tests.PrepareTestEnv(t)()
session := loginUser(t, "user2")
req := NewRequest(t, "GET", "/manifest.json")
resp := session.MakeRequest(t, req, http.StatusOK)
data := make(map[string]any)
DecodeJSON(t, resp, &data)
assert.Equal(t, "MyCustomJson", data["name"])
assert.NotContains(t, data, "short_name")
assert.NotContains(t, data, "start_url")
assert.NotContains(t, data, "display")
}