mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2024-11-14 05:56:14 +01:00
Merge pull request 'auth LinkAccount test coverage' (#803) from earl-warren/forgejo:wip-linkaccount into forgejo-development
Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/803
This commit is contained in:
commit
7adaafa760
7 changed files with 112 additions and 18 deletions
|
@ -14,6 +14,10 @@ import (
|
||||||
chi "github.com/go-chi/chi/v5"
|
chi "github.com/go-chi/chi/v5"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type KeyTestMiddlewareHookType string
|
||||||
|
|
||||||
|
var KeyTestMiddlewareHook = KeyTestMiddlewareHookType("testMiddlewareHook")
|
||||||
|
|
||||||
// Bind binding an obj to a handler
|
// Bind binding an obj to a handler
|
||||||
func Bind[T any](_ T) any {
|
func Bind[T any](_ T) any {
|
||||||
return func(ctx *context.Context) {
|
return func(ctx *context.Context) {
|
||||||
|
|
|
@ -167,6 +167,10 @@ func Routes(ctx gocontext.Context) *web.Route {
|
||||||
mid = append(mid, user.GetNotificationCount)
|
mid = append(mid, user.GetNotificationCount)
|
||||||
mid = append(mid, repo.GetActiveStopwatch)
|
mid = append(mid, repo.GetActiveStopwatch)
|
||||||
mid = append(mid, goGet)
|
mid = append(mid, goGet)
|
||||||
|
middlewareHook := ctx.Value(web.KeyTestMiddlewareHook)
|
||||||
|
if middlewareHook != nil {
|
||||||
|
mid = append(mid, middlewareHook)
|
||||||
|
}
|
||||||
|
|
||||||
others := web.NewRoute()
|
others := web.NewRoute()
|
||||||
others.Use(mid...)
|
others.Use(mid...)
|
||||||
|
|
|
@ -14,7 +14,6 @@ import (
|
||||||
user_model "code.gitea.io/gitea/models/user"
|
user_model "code.gitea.io/gitea/models/user"
|
||||||
"code.gitea.io/gitea/modules/activitypub"
|
"code.gitea.io/gitea/modules/activitypub"
|
||||||
"code.gitea.io/gitea/modules/setting"
|
"code.gitea.io/gitea/modules/setting"
|
||||||
"code.gitea.io/gitea/routers"
|
|
||||||
|
|
||||||
ap "github.com/go-ap/activitypub"
|
ap "github.com/go-ap/activitypub"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
|
@ -22,10 +21,10 @@ import (
|
||||||
|
|
||||||
func TestActivityPubPerson(t *testing.T) {
|
func TestActivityPubPerson(t *testing.T) {
|
||||||
setting.Federation.Enabled = true
|
setting.Federation.Enabled = true
|
||||||
c = routers.NormalRoutes(context.TODO())
|
setNormalRoutes()
|
||||||
defer func() {
|
defer func() {
|
||||||
setting.Federation.Enabled = false
|
setting.Federation.Enabled = false
|
||||||
c = routers.NormalRoutes(context.TODO())
|
setNormalRoutes()
|
||||||
}()
|
}()
|
||||||
|
|
||||||
onGiteaRun(t, func(*testing.T, *url.URL) {
|
onGiteaRun(t, func(*testing.T, *url.URL) {
|
||||||
|
@ -60,10 +59,10 @@ func TestActivityPubPerson(t *testing.T) {
|
||||||
|
|
||||||
func TestActivityPubMissingPerson(t *testing.T) {
|
func TestActivityPubMissingPerson(t *testing.T) {
|
||||||
setting.Federation.Enabled = true
|
setting.Federation.Enabled = true
|
||||||
c = routers.NormalRoutes(context.TODO())
|
setNormalRoutes()
|
||||||
defer func() {
|
defer func() {
|
||||||
setting.Federation.Enabled = false
|
setting.Federation.Enabled = false
|
||||||
c = routers.NormalRoutes(context.TODO())
|
setNormalRoutes()
|
||||||
}()
|
}()
|
||||||
|
|
||||||
onGiteaRun(t, func(*testing.T, *url.URL) {
|
onGiteaRun(t, func(*testing.T, *url.URL) {
|
||||||
|
@ -75,10 +74,10 @@ func TestActivityPubMissingPerson(t *testing.T) {
|
||||||
|
|
||||||
func TestActivityPubPersonInbox(t *testing.T) {
|
func TestActivityPubPersonInbox(t *testing.T) {
|
||||||
setting.Federation.Enabled = true
|
setting.Federation.Enabled = true
|
||||||
c = routers.NormalRoutes(context.TODO())
|
setNormalRoutes()
|
||||||
defer func() {
|
defer func() {
|
||||||
setting.Federation.Enabled = false
|
setting.Federation.Enabled = false
|
||||||
c = routers.NormalRoutes(context.TODO())
|
setNormalRoutes()
|
||||||
}()
|
}()
|
||||||
|
|
||||||
srv := httptest.NewServer(c)
|
srv := httptest.NewServer(c)
|
||||||
|
|
|
@ -4,24 +4,22 @@
|
||||||
package integration
|
package integration
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"code.gitea.io/gitea/modules/setting"
|
"code.gitea.io/gitea/modules/setting"
|
||||||
api "code.gitea.io/gitea/modules/structs"
|
api "code.gitea.io/gitea/modules/structs"
|
||||||
"code.gitea.io/gitea/routers"
|
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestNodeinfo(t *testing.T) {
|
func TestNodeinfo(t *testing.T) {
|
||||||
setting.Federation.Enabled = true
|
setting.Federation.Enabled = true
|
||||||
c = routers.NormalRoutes(context.TODO())
|
setNormalRoutes()
|
||||||
defer func() {
|
defer func() {
|
||||||
setting.Federation.Enabled = false
|
setting.Federation.Enabled = false
|
||||||
c = routers.NormalRoutes(context.TODO())
|
setNormalRoutes()
|
||||||
}()
|
}()
|
||||||
|
|
||||||
onGiteaRun(t, func(*testing.T, *url.URL) {
|
onGiteaRun(t, func(*testing.T, *url.URL) {
|
||||||
|
|
|
@ -4,7 +4,6 @@
|
||||||
package integration
|
package integration
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/http/httptest"
|
"net/http/httptest"
|
||||||
"os"
|
"os"
|
||||||
|
@ -13,7 +12,6 @@ import (
|
||||||
|
|
||||||
"code.gitea.io/gitea/modules/json"
|
"code.gitea.io/gitea/modules/json"
|
||||||
"code.gitea.io/gitea/modules/setting"
|
"code.gitea.io/gitea/modules/setting"
|
||||||
"code.gitea.io/gitea/routers"
|
|
||||||
"code.gitea.io/gitea/tests"
|
"code.gitea.io/gitea/tests"
|
||||||
|
|
||||||
"gitea.com/go-chi/session"
|
"gitea.com/go-chi/session"
|
||||||
|
@ -57,7 +55,7 @@ func TestSessionFileCreation(t *testing.T) {
|
||||||
oldSessionConfig := setting.SessionConfig.ProviderConfig
|
oldSessionConfig := setting.SessionConfig.ProviderConfig
|
||||||
defer func() {
|
defer func() {
|
||||||
setting.SessionConfig.ProviderConfig = oldSessionConfig
|
setting.SessionConfig.ProviderConfig = oldSessionConfig
|
||||||
c = routers.NormalRoutes(context.TODO())
|
setNormalRoutes()
|
||||||
}()
|
}()
|
||||||
|
|
||||||
var config session.Options
|
var config session.Options
|
||||||
|
@ -76,7 +74,7 @@ func TestSessionFileCreation(t *testing.T) {
|
||||||
|
|
||||||
setting.SessionConfig.ProviderConfig = string(newConfigBytes)
|
setting.SessionConfig.ProviderConfig = string(newConfigBytes)
|
||||||
|
|
||||||
c = routers.NormalRoutes(context.TODO())
|
setNormalRoutes()
|
||||||
|
|
||||||
t.Run("NoSessionOnViewIssue", func(t *testing.T) {
|
t.Run("NoSessionOnViewIssue", func(t *testing.T) {
|
||||||
defer tests.PrintCurrentTest(t)()
|
defer tests.PrintCurrentTest(t)()
|
||||||
|
|
|
@ -24,6 +24,7 @@ import (
|
||||||
|
|
||||||
"code.gitea.io/gitea/models/auth"
|
"code.gitea.io/gitea/models/auth"
|
||||||
"code.gitea.io/gitea/models/unittest"
|
"code.gitea.io/gitea/models/unittest"
|
||||||
|
user_model "code.gitea.io/gitea/models/user"
|
||||||
gitea_context "code.gitea.io/gitea/modules/context"
|
gitea_context "code.gitea.io/gitea/modules/context"
|
||||||
"code.gitea.io/gitea/modules/graceful"
|
"code.gitea.io/gitea/modules/graceful"
|
||||||
"code.gitea.io/gitea/modules/json"
|
"code.gitea.io/gitea/modules/json"
|
||||||
|
@ -33,6 +34,7 @@ import (
|
||||||
"code.gitea.io/gitea/modules/util"
|
"code.gitea.io/gitea/modules/util"
|
||||||
"code.gitea.io/gitea/modules/web"
|
"code.gitea.io/gitea/modules/web"
|
||||||
"code.gitea.io/gitea/routers"
|
"code.gitea.io/gitea/routers"
|
||||||
|
user_service "code.gitea.io/gitea/services/user"
|
||||||
"code.gitea.io/gitea/tests"
|
"code.gitea.io/gitea/tests"
|
||||||
|
|
||||||
"github.com/PuerkitoBio/goquery"
|
"github.com/PuerkitoBio/goquery"
|
||||||
|
@ -40,7 +42,19 @@ import (
|
||||||
"github.com/xeipuuv/gojsonschema"
|
"github.com/xeipuuv/gojsonschema"
|
||||||
)
|
)
|
||||||
|
|
||||||
var c *web.Route
|
var (
|
||||||
|
c *web.Route
|
||||||
|
testMiddlewareHook func(*gitea_context.Context)
|
||||||
|
)
|
||||||
|
|
||||||
|
func setNormalRoutes() {
|
||||||
|
middlewareHook := func(ctx *gitea_context.Context) {
|
||||||
|
if testMiddlewareHook != nil {
|
||||||
|
testMiddlewareHook(ctx)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
c = routers.NormalRoutes(context.WithValue(context.Background(), web.KeyTestMiddlewareHook, middlewareHook))
|
||||||
|
}
|
||||||
|
|
||||||
type NilResponseRecorder struct {
|
type NilResponseRecorder struct {
|
||||||
httptest.ResponseRecorder
|
httptest.ResponseRecorder
|
||||||
|
@ -87,8 +101,7 @@ func TestMain(m *testing.M) {
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
|
||||||
tests.InitTest(true)
|
tests.InitTest(true)
|
||||||
c = routers.NormalRoutes(context.TODO())
|
setNormalRoutes()
|
||||||
|
|
||||||
// integration test settings...
|
// integration test settings...
|
||||||
if setting.CfgProvider != nil {
|
if setting.CfgProvider != nil {
|
||||||
testingCfg := setting.CfgProvider.Section("integration-tests")
|
testingCfg := setting.CfgProvider.Section("integration-tests")
|
||||||
|
@ -228,6 +241,21 @@ func getUserToken(t testing.TB, userName string, scope ...auth.AccessTokenScope)
|
||||||
return getTokenForLoggedInUser(t, loginUser(t, userName), scope...)
|
return getTokenForLoggedInUser(t, loginUser(t, userName), scope...)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func createUser(t testing.TB, userName, email, password string) func() {
|
||||||
|
u := &user_model.User{
|
||||||
|
Name: userName,
|
||||||
|
Email: email,
|
||||||
|
Passwd: password,
|
||||||
|
MustChangePassword: false,
|
||||||
|
LoginType: auth.Plain,
|
||||||
|
}
|
||||||
|
|
||||||
|
assert.NoError(t, user_model.CreateUser(u, &user_model.CreateUserOverwriteOptions{}))
|
||||||
|
return func() {
|
||||||
|
assert.NoError(t, user_service.DeleteUser(context.Background(), u, true))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func loginUser(t testing.TB, userName string) *TestSession {
|
func loginUser(t testing.TB, userName string) *TestSession {
|
||||||
t.Helper()
|
t.Helper()
|
||||||
|
|
||||||
|
|
63
tests/integration/linkaccount_test.go
Normal file
63
tests/integration/linkaccount_test.go
Normal file
|
@ -0,0 +1,63 @@
|
||||||
|
// Copyright 2023 The Forgejo Authors. All rights reserved.
|
||||||
|
// SPDX-License-Identifier: MIT
|
||||||
|
|
||||||
|
package integration
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
gitea_context "code.gitea.io/gitea/modules/context"
|
||||||
|
"code.gitea.io/gitea/tests"
|
||||||
|
|
||||||
|
"github.com/markbates/goth"
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestLinkAccountChoose(t *testing.T) {
|
||||||
|
defer tests.PrepareTestEnv(t)()
|
||||||
|
username := "linkaccountuser"
|
||||||
|
email := "linkaccountuser@example.com"
|
||||||
|
password := "linkaccountuser"
|
||||||
|
defer createUser(t, username, email, password)()
|
||||||
|
|
||||||
|
defer func() {
|
||||||
|
testMiddlewareHook = nil
|
||||||
|
}()
|
||||||
|
|
||||||
|
for _, testCase := range []struct {
|
||||||
|
gothUser goth.User
|
||||||
|
signupTab string
|
||||||
|
signinTab string
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
gothUser: goth.User{},
|
||||||
|
signupTab: "item active",
|
||||||
|
signinTab: "item ",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
gothUser: goth.User{
|
||||||
|
Email: email,
|
||||||
|
},
|
||||||
|
signupTab: "item ",
|
||||||
|
signinTab: "item active",
|
||||||
|
},
|
||||||
|
} {
|
||||||
|
testMiddlewareHook = func(ctx *gitea_context.Context) {
|
||||||
|
ctx.Session.Set("linkAccountGothUser", testCase.gothUser)
|
||||||
|
}
|
||||||
|
|
||||||
|
req := NewRequest(t, "GET", "/user/link_account")
|
||||||
|
resp := MakeRequest(t, req, http.StatusOK)
|
||||||
|
assert.Equal(t, resp.Code, http.StatusOK, resp.Body)
|
||||||
|
doc := NewHTMLParser(t, resp.Body)
|
||||||
|
|
||||||
|
class, exists := doc.Find(`.new-menu-inner .item[data-tab="auth-link-signup-tab"]`).Attr("class")
|
||||||
|
assert.True(t, exists, resp.Body)
|
||||||
|
assert.Equal(t, testCase.signupTab, class)
|
||||||
|
|
||||||
|
class, exists = doc.Find(`.new-menu-inner .item[data-tab="auth-link-signin-tab"]`).Attr("class")
|
||||||
|
assert.True(t, exists)
|
||||||
|
assert.Equal(t, testCase.signinTab, class)
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue