mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2024-11-08 18:04:14 +01:00
[TESTS] oauth2: add integration test helpers
(cherry picked from commite11dcc60f2
) use backticks to avoid backslash (cherry picked from commit34212791ee
) (cherry picked from commitbde9473c69
) (cherry picked from commitd4deb43084
) (cherry picked from commit08e91649b0
) (cherry picked from commit2b988e5415
) [TESTS] auth LinkAccount test coverage (squash) (cherry picked from commita2b2e3066b
) (cherry picked from commit841d1b5073
) (cherry picked from commit35da630ad8
) (cherry picked from commitcaf2dc4fa7
) (cherry picked from commit6eb81e67ba
) (cherry picked from commitd59757239f
) (cherry picked from commit38a121b688
) (cherry picked from commit20613874ee
) (cherry picked from commit6d2705e108
) (cherry picked from commitf177b72814
) (cherry picked from commit75e1fc4c83
) (cherry picked from commitba64fa9867
) (cherry picked from commit0b8ab0893e
) (cherry picked from commit1419d11435
) (cherry picked from commit38766847e0
) (cherry picked from commit6f23426a6a
) (cherry picked from commit9e0ff9ca54
) (cherry picked from commit353f3601c3
) (cherry picked from commit6e4ae401d8
) (cherry picked from commit1a7afe4153
) (cherry picked from commitf9f3e0cc02
) (cherry picked from commit22fd0337f3
) (cherry picked from commitee57e138d1
)
This commit is contained in:
parent
95f0b8eefa
commit
21f9b7e73d
2 changed files with 45 additions and 0 deletions
|
@ -296,6 +296,17 @@ func GetSourceByID(ctx context.Context, id int64) (*Source, error) {
|
|||
return source, nil
|
||||
}
|
||||
|
||||
func GetSourceByName(ctx context.Context, name string) (*Source, error) {
|
||||
source := &Source{}
|
||||
has, err := db.GetEngine(ctx).Where("name = ?", name).Get(source)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
} else if !has {
|
||||
return nil, ErrSourceNotExist{}
|
||||
}
|
||||
return source, nil
|
||||
}
|
||||
|
||||
// UpdateSource updates a Source record in DB.
|
||||
func UpdateSource(ctx context.Context, source *Source) error {
|
||||
var originalSource *Source
|
||||
|
|
|
@ -39,6 +39,7 @@ import (
|
|||
"code.gitea.io/gitea/tests"
|
||||
|
||||
"github.com/PuerkitoBio/goquery"
|
||||
goth_gitlab "github.com/markbates/goth/providers/gitlab"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/xeipuuv/gojsonschema"
|
||||
)
|
||||
|
@ -231,6 +232,39 @@ func getUserToken(t testing.TB, userName string, scope ...auth.AccessTokenScope)
|
|||
return getTokenForLoggedInUser(t, loginUser(t, userName), scope...)
|
||||
}
|
||||
|
||||
func addAuthSource(t *testing.T, payload map[string]string) *auth.Source {
|
||||
session := loginUser(t, "user1")
|
||||
payload["_csrf"] = GetCSRF(t, session, "/admin/auths/new")
|
||||
req := NewRequestWithValues(t, "POST", "/admin/auths/new", payload)
|
||||
session.MakeRequest(t, req, http.StatusSeeOther)
|
||||
source, err := auth.GetSourceByName(context.Background(), payload["name"])
|
||||
assert.NoError(t, err)
|
||||
return source
|
||||
}
|
||||
|
||||
func authSourcePayloadOAuth2(name string) map[string]string {
|
||||
return map[string]string{
|
||||
"type": fmt.Sprintf("%d", auth.OAuth2),
|
||||
"name": name,
|
||||
"is_active": "on",
|
||||
}
|
||||
}
|
||||
|
||||
func authSourcePayloadGitLab(name string) map[string]string {
|
||||
payload := authSourcePayloadOAuth2(name)
|
||||
payload["oauth2_provider"] = "gitlab"
|
||||
return payload
|
||||
}
|
||||
|
||||
func authSourcePayloadGitLabCustom(name string) map[string]string {
|
||||
payload := authSourcePayloadGitLab(name)
|
||||
payload["oauth2_use_custom_url"] = "on"
|
||||
payload["oauth2_auth_url"] = goth_gitlab.AuthURL
|
||||
payload["oauth2_token_url"] = goth_gitlab.TokenURL
|
||||
payload["oauth2_profile_url"] = goth_gitlab.ProfileURL
|
||||
return payload
|
||||
}
|
||||
|
||||
func createUser(ctx context.Context, t testing.TB, user *user_model.User) func() {
|
||||
user.MustChangePassword = false
|
||||
user.LowerName = strings.ToLower(user.Name)
|
||||
|
|
Loading…
Reference in a new issue