Skip to content

Commit 9628d99

Browse files
committed
fix
1 parent 0188d82 commit 9628d99

File tree

3 files changed

+18
-8
lines changed

3 files changed

+18
-8
lines changed

modules/test/utils.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,10 @@ func IsNormalPageCompleted(s string) bool {
3434
return strings.Contains(s, `<footer class="page-footer"`) && strings.Contains(s, `</html>`)
3535
}
3636

37-
func MockVariableValue[T any](p *T, v T) (reset func()) {
37+
func MockVariableValue[T any](p *T, v ...T) (reset func()) {
3838
old := *p
39-
*p = v
39+
if len(v) > 0 {
40+
*p = v[0]
41+
}
4042
return func() { *p = old }
4143
}

routers/api/packages/container/container.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,9 +116,9 @@ func apiErrorDefined(ctx *context.Context, err *namedError) {
116116
}
117117

118118
func apiUnauthorizedError(ctx *context.Context) {
119-
// TODO: it doesn't seem quite right but it doesn't really cause problem at the moment.
120-
// container registry requires that the "/v2" must be in the root, so the sub-path in AppURL should be removed, ideally.
121-
ctx.Resp.Header().Add("WWW-Authenticate", `Bearer realm="`+httplib.GuessCurrentAppURL(ctx)+`v2/token",service="container_registry",scope="*"`)
119+
// container registry requires that the "/v2" must be in the root, so the sub-path in AppURL should be removed
120+
realmURL := strings.TrimSuffix(httplib.GuessCurrentAppURL(ctx), setting.AppSubURL+"/") + "/v2/token"
121+
ctx.Resp.Header().Add("WWW-Authenticate", `Bearer realm="`+realmURL+`",service="container_registry",scope="*"`)
122122
apiErrorDefined(ctx, errUnauthorized)
123123
}
124124

tests/integration/api_packages_container_test.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,15 +84,17 @@ func TestPackageContainer(t *testing.T) {
8484
Token string `json:"token"`
8585
}
8686

87-
authenticate := []string{`Bearer realm="` + setting.AppURL + `v2/token",service="container_registry",scope="*"`}
87+
wwwAuthenticateValues := func() []string {
88+
return []string{`Bearer realm="` + setting.AppURL + `v2/token",service="container_registry",scope="*"`}
89+
}
8890

8991
t.Run("Anonymous", func(t *testing.T) {
9092
defer tests.PrintCurrentTest(t)()
9193

9294
req := NewRequest(t, "GET", fmt.Sprintf("%sv2", setting.AppURL))
9395
resp := MakeRequest(t, req, http.StatusUnauthorized)
9496

95-
assert.ElementsMatch(t, authenticate, resp.Header().Values("WWW-Authenticate"))
97+
assert.ElementsMatch(t, wwwAuthenticateValues(), resp.Header().Values("WWW-Authenticate"))
9698

9799
req = NewRequest(t, "GET", fmt.Sprintf("%sv2/token", setting.AppURL))
98100
resp = MakeRequest(t, req, http.StatusOK)
@@ -115,6 +117,12 @@ func TestPackageContainer(t *testing.T) {
115117

116118
req = NewRequest(t, "GET", fmt.Sprintf("%sv2/token", setting.AppURL))
117119
MakeRequest(t, req, http.StatusUnauthorized)
120+
121+
defer test.MockVariableValue(&setting.AppURL, "http://domain/sub-path/")()
122+
defer test.MockVariableValue(&setting.AppSubURL, "/sub-path")()
123+
req = NewRequest(t, "GET", fmt.Sprintf("/v2"))
124+
resp = MakeRequest(t, req, http.StatusUnauthorized)
125+
assert.Equal(t, `Bearer realm="http://domain/v2/token",service="container_registry",scope="*"`, resp.Header().Get("WWW-Authenticate"))
118126
})
119127

120128
t.Run("User", func(t *testing.T) {
@@ -123,7 +131,7 @@ func TestPackageContainer(t *testing.T) {
123131
req := NewRequest(t, "GET", fmt.Sprintf("%sv2", setting.AppURL))
124132
resp := MakeRequest(t, req, http.StatusUnauthorized)
125133

126-
assert.ElementsMatch(t, authenticate, resp.Header().Values("WWW-Authenticate"))
134+
assert.ElementsMatch(t, wwwAuthenticateValues(), resp.Header().Values("WWW-Authenticate"))
127135

128136
req = NewRequest(t, "GET", fmt.Sprintf("%sv2/token", setting.AppURL)).
129137
AddBasicAuth(user.Name)

0 commit comments

Comments
 (0)