mirror of
https://codeberg.org/davrot/forgejo.git
synced 2025-06-29 09:00:04 +02:00
enable linter testifylint on v7 (#4572)
Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/4572 Co-authored-by: TheFox0x7 <thefox0x7@gmail.com> Co-committed-by: TheFox0x7 <thefox0x7@gmail.com>
This commit is contained in:
parent
c47bdf436b
commit
072dd9f8bc
494 changed files with 4897 additions and 4554 deletions
|
@ -22,6 +22,7 @@ import (
|
|||
|
||||
"github.com/markbates/goth"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestAuthorizeNoClientID(t *testing.T) {
|
||||
|
@ -46,7 +47,7 @@ func TestAuthorizeUnsupportedResponseType(t *testing.T) {
|
|||
ctx := loginUser(t, "user1")
|
||||
resp := ctx.MakeRequest(t, req, http.StatusSeeOther)
|
||||
u, err := resp.Result().Location()
|
||||
assert.NoError(t, err)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, "unsupported_response_type", u.Query().Get("error"))
|
||||
assert.Equal(t, "Only code response type is supported.", u.Query().Get("error_description"))
|
||||
}
|
||||
|
@ -57,7 +58,7 @@ func TestAuthorizeUnsupportedCodeChallengeMethod(t *testing.T) {
|
|||
ctx := loginUser(t, "user1")
|
||||
resp := ctx.MakeRequest(t, req, http.StatusSeeOther)
|
||||
u, err := resp.Result().Location()
|
||||
assert.NoError(t, err)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, "invalid_request", u.Query().Get("error"))
|
||||
assert.Equal(t, "unsupported code challenge method", u.Query().Get("error_description"))
|
||||
}
|
||||
|
@ -144,9 +145,9 @@ func TestAuthorizeRedirectWithExistingGrant(t *testing.T) {
|
|||
ctx := loginUser(t, "user1")
|
||||
resp := ctx.MakeRequest(t, req, http.StatusSeeOther)
|
||||
u, err := resp.Result().Location()
|
||||
assert.NoError(t, err)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, "thestate", u.Query().Get("state"))
|
||||
assert.Truef(t, len(u.Query().Get("code")) > 30, "authorization code '%s' should be longer then 30", u.Query().Get("code"))
|
||||
assert.Greaterf(t, len(u.Query().Get("code")), 30, "authorization code '%s' should be longer then 30", u.Query().Get("code"))
|
||||
u.RawQuery = ""
|
||||
assert.Equal(t, "https://example.com/xyzzy", u.String())
|
||||
}
|
||||
|
@ -157,7 +158,7 @@ func TestAuthorizePKCERequiredForPublicClient(t *testing.T) {
|
|||
ctx := loginUser(t, "user1")
|
||||
resp := ctx.MakeRequest(t, req, http.StatusSeeOther)
|
||||
u, err := resp.Result().Location()
|
||||
assert.NoError(t, err)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, "invalid_request", u.Query().Get("error"))
|
||||
assert.Equal(t, "PKCE is required for public clients", u.Query().Get("error_description"))
|
||||
}
|
||||
|
@ -181,9 +182,9 @@ func TestAccessTokenExchange(t *testing.T) {
|
|||
}
|
||||
parsed := new(response)
|
||||
|
||||
assert.NoError(t, json.Unmarshal(resp.Body.Bytes(), parsed))
|
||||
assert.True(t, len(parsed.AccessToken) > 10)
|
||||
assert.True(t, len(parsed.RefreshToken) > 10)
|
||||
require.NoError(t, json.Unmarshal(resp.Body.Bytes(), parsed))
|
||||
assert.Greater(t, len(parsed.AccessToken), 10)
|
||||
assert.Greater(t, len(parsed.RefreshToken), 10)
|
||||
}
|
||||
|
||||
func TestAccessTokenExchangeWithPublicClient(t *testing.T) {
|
||||
|
@ -204,9 +205,9 @@ func TestAccessTokenExchangeWithPublicClient(t *testing.T) {
|
|||
}
|
||||
parsed := new(response)
|
||||
|
||||
assert.NoError(t, json.Unmarshal(resp.Body.Bytes(), parsed))
|
||||
assert.True(t, len(parsed.AccessToken) > 10)
|
||||
assert.True(t, len(parsed.RefreshToken) > 10)
|
||||
require.NoError(t, json.Unmarshal(resp.Body.Bytes(), parsed))
|
||||
assert.Greater(t, len(parsed.AccessToken), 10)
|
||||
assert.Greater(t, len(parsed.RefreshToken), 10)
|
||||
}
|
||||
|
||||
func TestAccessTokenExchangeJSON(t *testing.T) {
|
||||
|
@ -228,9 +229,9 @@ func TestAccessTokenExchangeJSON(t *testing.T) {
|
|||
}
|
||||
parsed := new(response)
|
||||
|
||||
assert.NoError(t, json.Unmarshal(resp.Body.Bytes(), parsed))
|
||||
assert.True(t, len(parsed.AccessToken) > 10)
|
||||
assert.True(t, len(parsed.RefreshToken) > 10)
|
||||
require.NoError(t, json.Unmarshal(resp.Body.Bytes(), parsed))
|
||||
assert.Greater(t, len(parsed.AccessToken), 10)
|
||||
assert.Greater(t, len(parsed.RefreshToken), 10)
|
||||
}
|
||||
|
||||
func TestAccessTokenExchangeWithoutPKCE(t *testing.T) {
|
||||
|
@ -244,7 +245,7 @@ func TestAccessTokenExchangeWithoutPKCE(t *testing.T) {
|
|||
})
|
||||
resp := MakeRequest(t, req, http.StatusBadRequest)
|
||||
parsedError := new(auth.AccessTokenError)
|
||||
assert.NoError(t, json.Unmarshal(resp.Body.Bytes(), parsedError))
|
||||
require.NoError(t, json.Unmarshal(resp.Body.Bytes(), parsedError))
|
||||
assert.Equal(t, "unauthorized_client", string(parsedError.ErrorCode))
|
||||
assert.Equal(t, "failed PKCE code challenge", parsedError.ErrorDescription)
|
||||
}
|
||||
|
@ -262,7 +263,7 @@ func TestAccessTokenExchangeWithInvalidCredentials(t *testing.T) {
|
|||
})
|
||||
resp := MakeRequest(t, req, http.StatusBadRequest)
|
||||
parsedError := new(auth.AccessTokenError)
|
||||
assert.NoError(t, json.Unmarshal(resp.Body.Bytes(), parsedError))
|
||||
require.NoError(t, json.Unmarshal(resp.Body.Bytes(), parsedError))
|
||||
assert.Equal(t, "invalid_client", string(parsedError.ErrorCode))
|
||||
assert.Equal(t, "cannot load client with client id: '???'", parsedError.ErrorDescription)
|
||||
|
||||
|
@ -277,7 +278,7 @@ func TestAccessTokenExchangeWithInvalidCredentials(t *testing.T) {
|
|||
})
|
||||
resp = MakeRequest(t, req, http.StatusBadRequest)
|
||||
parsedError = new(auth.AccessTokenError)
|
||||
assert.NoError(t, json.Unmarshal(resp.Body.Bytes(), parsedError))
|
||||
require.NoError(t, json.Unmarshal(resp.Body.Bytes(), parsedError))
|
||||
assert.Equal(t, "unauthorized_client", string(parsedError.ErrorCode))
|
||||
assert.Equal(t, "invalid client secret", parsedError.ErrorDescription)
|
||||
|
||||
|
@ -292,7 +293,7 @@ func TestAccessTokenExchangeWithInvalidCredentials(t *testing.T) {
|
|||
})
|
||||
resp = MakeRequest(t, req, http.StatusBadRequest)
|
||||
parsedError = new(auth.AccessTokenError)
|
||||
assert.NoError(t, json.Unmarshal(resp.Body.Bytes(), parsedError))
|
||||
require.NoError(t, json.Unmarshal(resp.Body.Bytes(), parsedError))
|
||||
assert.Equal(t, "unauthorized_client", string(parsedError.ErrorCode))
|
||||
assert.Equal(t, "unexpected redirect URI", parsedError.ErrorDescription)
|
||||
|
||||
|
@ -307,7 +308,7 @@ func TestAccessTokenExchangeWithInvalidCredentials(t *testing.T) {
|
|||
})
|
||||
resp = MakeRequest(t, req, http.StatusBadRequest)
|
||||
parsedError = new(auth.AccessTokenError)
|
||||
assert.NoError(t, json.Unmarshal(resp.Body.Bytes(), parsedError))
|
||||
require.NoError(t, json.Unmarshal(resp.Body.Bytes(), parsedError))
|
||||
assert.Equal(t, "unauthorized_client", string(parsedError.ErrorCode))
|
||||
assert.Equal(t, "client is not authorized", parsedError.ErrorDescription)
|
||||
|
||||
|
@ -322,7 +323,7 @@ func TestAccessTokenExchangeWithInvalidCredentials(t *testing.T) {
|
|||
})
|
||||
resp = MakeRequest(t, req, http.StatusBadRequest)
|
||||
parsedError = new(auth.AccessTokenError)
|
||||
assert.NoError(t, json.Unmarshal(resp.Body.Bytes(), parsedError))
|
||||
require.NoError(t, json.Unmarshal(resp.Body.Bytes(), parsedError))
|
||||
assert.Equal(t, "unsupported_grant_type", string(parsedError.ErrorCode))
|
||||
assert.Equal(t, "Only refresh_token or authorization_code grant type is supported", parsedError.ErrorDescription)
|
||||
}
|
||||
|
@ -345,9 +346,9 @@ func TestAccessTokenExchangeWithBasicAuth(t *testing.T) {
|
|||
}
|
||||
parsed := new(response)
|
||||
|
||||
assert.NoError(t, json.Unmarshal(resp.Body.Bytes(), parsed))
|
||||
assert.True(t, len(parsed.AccessToken) > 10)
|
||||
assert.True(t, len(parsed.RefreshToken) > 10)
|
||||
require.NoError(t, json.Unmarshal(resp.Body.Bytes(), parsed))
|
||||
assert.Greater(t, len(parsed.AccessToken), 10)
|
||||
assert.Greater(t, len(parsed.RefreshToken), 10)
|
||||
|
||||
// use wrong client_secret
|
||||
req = NewRequestWithValues(t, "POST", "/login/oauth/access_token", map[string]string{
|
||||
|
@ -359,7 +360,7 @@ func TestAccessTokenExchangeWithBasicAuth(t *testing.T) {
|
|||
req.Header.Add("Authorization", "Basic ZGE3ZGEzYmEtOWExMy00MTY3LTg1NmYtMzg5OWRlMGIwMTM4OmJsYWJsYQ==")
|
||||
resp = MakeRequest(t, req, http.StatusBadRequest)
|
||||
parsedError := new(auth.AccessTokenError)
|
||||
assert.NoError(t, json.Unmarshal(resp.Body.Bytes(), parsedError))
|
||||
require.NoError(t, json.Unmarshal(resp.Body.Bytes(), parsedError))
|
||||
assert.Equal(t, "unauthorized_client", string(parsedError.ErrorCode))
|
||||
assert.Equal(t, "invalid client secret", parsedError.ErrorDescription)
|
||||
|
||||
|
@ -372,7 +373,7 @@ func TestAccessTokenExchangeWithBasicAuth(t *testing.T) {
|
|||
})
|
||||
resp = MakeRequest(t, req, http.StatusBadRequest)
|
||||
parsedError = new(auth.AccessTokenError)
|
||||
assert.NoError(t, json.Unmarshal(resp.Body.Bytes(), parsedError))
|
||||
require.NoError(t, json.Unmarshal(resp.Body.Bytes(), parsedError))
|
||||
assert.Equal(t, "invalid_client", string(parsedError.ErrorCode))
|
||||
assert.Equal(t, "cannot load client with client id: ''", parsedError.ErrorDescription)
|
||||
|
||||
|
@ -386,7 +387,7 @@ func TestAccessTokenExchangeWithBasicAuth(t *testing.T) {
|
|||
req.Header.Add("Authorization", "Basic ZGE3ZGEzYmEtOWExMy00MTY3LTg1NmYtMzg5OWRlMGIwMTM4OjRNSzhOYTZSNTVzbWRDWTBXdUNDdW1aNmhqUlBuR1k1c2FXVlJISGpKaUE9")
|
||||
resp = MakeRequest(t, req, http.StatusBadRequest)
|
||||
parsedError = new(auth.AccessTokenError)
|
||||
assert.NoError(t, json.Unmarshal(resp.Body.Bytes(), parsedError))
|
||||
require.NoError(t, json.Unmarshal(resp.Body.Bytes(), parsedError))
|
||||
assert.Equal(t, "invalid_request", string(parsedError.ErrorCode))
|
||||
assert.Equal(t, "client_id in request body inconsistent with Authorization header", parsedError.ErrorDescription)
|
||||
|
||||
|
@ -400,7 +401,7 @@ func TestAccessTokenExchangeWithBasicAuth(t *testing.T) {
|
|||
req.Header.Add("Authorization", "Basic ZGE3ZGEzYmEtOWExMy00MTY3LTg1NmYtMzg5OWRlMGIwMTM4OjRNSzhOYTZSNTVzbWRDWTBXdUNDdW1aNmhqUlBuR1k1c2FXVlJISGpKaUE9")
|
||||
resp = MakeRequest(t, req, http.StatusBadRequest)
|
||||
parsedError = new(auth.AccessTokenError)
|
||||
assert.NoError(t, json.Unmarshal(resp.Body.Bytes(), parsedError))
|
||||
require.NoError(t, json.Unmarshal(resp.Body.Bytes(), parsedError))
|
||||
assert.Equal(t, "invalid_request", string(parsedError.ErrorCode))
|
||||
assert.Equal(t, "client_secret in request body inconsistent with Authorization header", parsedError.ErrorDescription)
|
||||
}
|
||||
|
@ -424,7 +425,7 @@ func TestRefreshTokenInvalidation(t *testing.T) {
|
|||
}
|
||||
parsed := new(response)
|
||||
|
||||
assert.NoError(t, json.Unmarshal(resp.Body.Bytes(), parsed))
|
||||
require.NoError(t, json.Unmarshal(resp.Body.Bytes(), parsed))
|
||||
|
||||
// test without invalidation
|
||||
setting.OAuth2.InvalidateRefreshTokens = false
|
||||
|
@ -438,7 +439,7 @@ func TestRefreshTokenInvalidation(t *testing.T) {
|
|||
})
|
||||
resp = MakeRequest(t, req, http.StatusBadRequest)
|
||||
parsedError := new(auth.AccessTokenError)
|
||||
assert.NoError(t, json.Unmarshal(resp.Body.Bytes(), parsedError))
|
||||
require.NoError(t, json.Unmarshal(resp.Body.Bytes(), parsedError))
|
||||
assert.Equal(t, "invalid_client", string(parsedError.ErrorCode))
|
||||
assert.Equal(t, "invalid empty client secret", parsedError.ErrorDescription)
|
||||
|
||||
|
@ -451,7 +452,7 @@ func TestRefreshTokenInvalidation(t *testing.T) {
|
|||
})
|
||||
resp = MakeRequest(t, req, http.StatusBadRequest)
|
||||
parsedError = new(auth.AccessTokenError)
|
||||
assert.NoError(t, json.Unmarshal(resp.Body.Bytes(), parsedError))
|
||||
require.NoError(t, json.Unmarshal(resp.Body.Bytes(), parsedError))
|
||||
assert.Equal(t, "unauthorized_client", string(parsedError.ErrorCode))
|
||||
assert.Equal(t, "unable to parse refresh token", parsedError.ErrorDescription)
|
||||
|
||||
|
@ -464,7 +465,7 @@ func TestRefreshTokenInvalidation(t *testing.T) {
|
|||
})
|
||||
|
||||
bs, err := io.ReadAll(req.Body)
|
||||
assert.NoError(t, err)
|
||||
require.NoError(t, err)
|
||||
|
||||
req.Body = io.NopCloser(bytes.NewReader(bs))
|
||||
MakeRequest(t, req, http.StatusOK)
|
||||
|
@ -481,7 +482,7 @@ func TestRefreshTokenInvalidation(t *testing.T) {
|
|||
req.Body = io.NopCloser(bytes.NewReader(bs))
|
||||
resp = MakeRequest(t, req, http.StatusBadRequest)
|
||||
parsedError = new(auth.AccessTokenError)
|
||||
assert.NoError(t, json.Unmarshal(resp.Body.Bytes(), parsedError))
|
||||
require.NoError(t, json.Unmarshal(resp.Body.Bytes(), parsedError))
|
||||
assert.Equal(t, "unauthorized_client", string(parsedError.ErrorCode))
|
||||
assert.Equal(t, "token was already used", parsedError.ErrorDescription)
|
||||
}
|
||||
|
@ -524,7 +525,7 @@ func TestSignInOAuthCallbackSignIn(t *testing.T) {
|
|||
})()
|
||||
req := NewRequest(t, "GET", fmt.Sprintf("/user/oauth2/%s/callback?code=XYZ&state=XYZ", gitlabName))
|
||||
resp := MakeRequest(t, req, http.StatusSeeOther)
|
||||
assert.Equal(t, test.RedirectURL(resp), "/")
|
||||
assert.Equal(t, "/", test.RedirectURL(resp))
|
||||
userAfterLogin := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: userGitLab.ID})
|
||||
assert.Greater(t, userAfterLogin.LastLoginUnix, userGitLab.LastLoginUnix)
|
||||
}
|
||||
|
@ -611,7 +612,7 @@ func TestSignUpViaOAuthWithMissingFields(t *testing.T) {
|
|||
})()
|
||||
req := NewRequest(t, "GET", fmt.Sprintf("/user/oauth2/%s/callback?code=XYZ&state=XYZ", gitlabName))
|
||||
resp := MakeRequest(t, req, http.StatusSeeOther)
|
||||
assert.Equal(t, test.RedirectURL(resp), "/user/link_account")
|
||||
assert.Equal(t, "/user/link_account", test.RedirectURL(resp))
|
||||
}
|
||||
|
||||
func TestOAuth_GrantApplicationOAuth(t *testing.T) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue