@@ -27,6 +27,7 @@ import (
2727 "code.gitea.io/gitea/services/auth/source/oauth2"
2828 "code.gitea.io/gitea/services/oauth2_provider"
2929 "code.gitea.io/gitea/tests"
30+ "github.com/PuerkitoBio/goquery"
3031
3132 "github.com/markbates/goth"
3233 "github.com/markbates/goth/gothic"
@@ -44,7 +45,7 @@ func TestOAuth2Provider(t *testing.T) {
4445 t .Run ("AuthorizeLoginRedirect" , testAuthorizeLoginRedirect )
4546
4647 t .Run ("OAuth2WellKnown" , testOAuth2WellKnown )
47- t .Run ("OAuthSourceWithSpace " , testOAuthSourceWithSpace )
48+ t .Run ("OAuthSourceSpecialChars " , testOAuthSourceSpecialChars )
4849 // TODO: move more tests as sub-tests here, avoid unnecessary PrepareTestEnv
4950}
5051
@@ -1098,19 +1099,40 @@ func TestSignInOauthCallbackSyncSSHKeys(t *testing.T) {
10981099
10991100// Checks if an OAuth provider with spaces within the name does work,
11001101// with the encoding of its names in the URL (PR#37327)
1101- func testOAuthSourceWithSpace (t * testing.T ) {
1102+ func testOAuthSourceSpecialChars (t * testing.T ) {
11021103 mockServer := createMockServer ()
11031104 defer mockServer .Close ()
11041105
1105- authName := "oauth test with spaces"
1106- oauth2Source := oauth2.Source {
1106+ addOAuth2Source (t , "test space" , oauth2.Source {
11071107 Provider : "openidConnect" ,
11081108 OpenIDConnectAutoDiscoveryURL : mockServer .URL + "/.well-known/openid-configuration" ,
1109+ })
1110+ addOAuth2Source (t , "test+plus" , oauth2.Source {
1111+ Provider : "openidConnect" ,
1112+ OpenIDConnectAutoDiscoveryURL : mockServer .URL + "/.well-known/openid-configuration" ,
1113+ })
1114+
1115+ testOAuth2 := func (t * testing.T , uri string , statusCode int ) {
1116+ req := NewRequest (t , "GET" , uri )
1117+ resp := MakeRequest (t , req , statusCode )
1118+ assert .NotEmpty (t , resp .Header ().Get ("Location" ))
11091119 }
1110- addOAuth2Source (t , authName , oauth2Source )
11111120
1112- session := emptyTestSession (t )
1113- req := NewRequest (t , "GET" , "/user/oauth2/" + url .QueryEscape (authName ))
1114- resp := session .MakeRequest (t , req , http .StatusTemporaryRedirect )
1115- assert .Contains (t , resp .Header ().Get ("Location" ), mockServer .URL + "/authorize" )
1121+ req := MakeRequest (t , NewRequest (t , "GET" , "/user/login" ), http .StatusOK )
1122+ doc := NewHTMLParser (t , req .Body )
1123+ var oauth2Links []string
1124+ doc .Find (".external-login-link" ).Each (func (i int , s * goquery.Selection ) {
1125+ oauth2Links = append (oauth2Links , s .AttrOr ("href" , "" ))
1126+ })
1127+ assert .Equal (t , []string {
1128+ "/user/oauth2/test%20space" ,
1129+ "/user/oauth2/test+plus" ,
1130+ }, oauth2Links )
1131+
1132+ testOAuth2 (t , "/user/oauth2/test%20space" , http .StatusTemporaryRedirect )
1133+ testOAuth2 (t , "/user/oauth2/test+space" , http .StatusNotFound )
1134+
1135+ testOAuth2 (t , "/user/oauth2/test+plus" , http .StatusTemporaryRedirect )
1136+ testOAuth2 (t , "/user/oauth2/test%2Bplus" , http .StatusTemporaryRedirect )
1137+ testOAuth2 (t , "/user/oauth2/test%20plus" , http .StatusNotFound )
11161138}
0 commit comments