@@ -31,138 +31,109 @@ import OAuth2
3131#endif
3232
3333
34- //class OAuth2ImplicitGrantTests: XCTestCase
35- //{
36- // func testInit() {
37- // let oauth = OAuth2ImplicitGrant(settings: [
38- // "client_id": "abc",
39- // "keychain": false,
40- // "authorize_uri": "https://auth.ful.io",
41- // ])
42- // XCTAssertEqual(oauth.clientId, "abc", "Must init `client_id`")
43- // XCTAssertNil(oauth.scope, "Empty scope")
44- //
45- // XCTAssertEqual(oauth.authURL, URL(string: "https://auth.ful.io")!, "Must init `authorize_uri`")
46- // }
47- //
48- // func testReturnURLHandling() {
49- // let oauth = OAuth2ImplicitGrant(settings: [
50- // "client_id": "abc",
51- // "authorize_uri": "https://auth.ful.io",
52- // "keychain": false,
53- // ])
54- //
55- // // Empty redirect URL
56- // oauth.didAuthorizeOrFail = { authParameters, error in
57- // XCTAssertNil(authParameters, "Nil auth dict expected")
58- // XCTAssertNotNil(error, "Error message expected")
59- // XCTAssertEqual(error, OAuth2Error.invalidRedirectURL("file:///"))
60- // }
61- // oauth.afterAuthorizeOrFail = { authParameters, error in
62- // XCTAssertNil(authParameters, "Nil auth dict expected")
63- // XCTAssertNotNil(error, "Error message expected")
64- // }
65- // oauth.context._state = "ONSTUH"
66- // oauth.handleRedirectURL(URL(string: "file:///")!)
67- // XCTAssertNil(oauth.accessToken, "Must not have an access token")
68- //
69- // // No params in redirect URL
70- // oauth.didAuthorizeOrFail = { authParameters, error in
71- // XCTAssertNil(authParameters, "Nil auth dict expected")
72- // XCTAssertNotNil(error, "Error message expected")
73- // XCTAssertEqual(error, OAuth2Error.invalidRedirectURL("https://auth.ful.io"))
74- // }
75- // oauth.handleRedirectURL(URL(string: "https://auth.ful.io")!)
76- // XCTAssertNil(oauth.accessToken, "Must not have an access token")
77- //
78- // // standard error
79- // oauth.context._state = "ONSTUH" // because it has been reset
80- // oauth.didAuthorizeOrFail = { authParameters, error in
81- // XCTAssertNil(authParameters, "Nil auth dict expected")
82- // XCTAssertNotNil(error, "Error message expected")
83- // XCTAssertEqual(error, OAuth2Error.accessDenied(nil))
84- // XCTAssertEqual(error?.description, "The resource owner or authorization server denied the request.")
85- // }
86- // oauth.handleRedirectURL(URL(string: "https://auth.ful.io#error=access_denied")!)
87- // XCTAssertNil(oauth.accessToken, "Must not have an access token")
88- //
89- // // explicit error
90- // oauth.context._state = "ONSTUH" // because it has been reset
91- // oauth.didAuthorizeOrFail = { authParameters, error in
92- // XCTAssertNil(authParameters, "Nil auth dict expected")
93- // XCTAssertNotNil(error, "Error message expected")
94- // XCTAssertNotEqual(error, OAuth2Error.generic("Not good"))
95- // XCTAssertEqual(error, OAuth2Error.responseError("Not good"))
96- // XCTAssertEqual(error?.description, "Not good")
97- // }
98- // oauth.handleRedirectURL(URL(string: "https://auth.ful.io#error_description=Not+good")!)
99- // XCTAssertNil(oauth.accessToken, "Must not have an access token")
100- //
101- // // no token type
102- // oauth.context._state = "ONSTUH" // because it has been reset
103- // oauth.didAuthorizeOrFail = { authParameters, error in
104- // XCTAssertNil(authParameters, "Nil auth dict expected")
105- // XCTAssertNotNil(error, "Error message expected")
106- // XCTAssertEqual(error, OAuth2Error.noTokenType)
107- // }
108- // oauth.handleRedirectURL(URL(string: "https://auth.ful.io#access_token=abc&state=\(oauth.context.state)")!)
109- // XCTAssertNil(oauth.accessToken, "Must not have an access token")
110- //
111- // // unsupported token type
112- // oauth.context._state = "ONSTUH" // because it has been reset
113- // oauth.didAuthorizeOrFail = { authParameters, error in
114- // XCTAssertNil(authParameters, "Nil auth dict expected")
115- // XCTAssertNotNil(error, "Error message expected")
116- // XCTAssertEqual(error, OAuth2Error.unsupportedTokenType("Only “bearer” token is supported, but received “helicopter”"))
117- // }
118- // oauth.handleRedirectURL(URL(string: "https://auth.ful.io#token_type=helicopter&access_token=abc&state=\(oauth.context.state)")!)
119- // XCTAssertNil(oauth.accessToken, "Must not have an access token")
120- //
121- // // Missing state
122- // oauth.context._state = "ONSTUH" // because it has been reset
123- // oauth.didAuthorizeOrFail = { authParameters, error in
124- // XCTAssertNil(authParameters, "Nil auth dict expected")
125- // XCTAssertNotNil(error, "Error message expected")
126- // XCTAssertEqual(error, OAuth2Error.missingState)
127- // }
128- // oauth.handleRedirectURL(URL(string: "https://auth.ful.io#token_type=bearer&access_token=abc")!)
129- // XCTAssertNil(oauth.accessToken, "Must not have an access token")
130- //
131- // // Invalid state
132- // oauth.context._state = "ONSTUH" // because it has been reset
133- // oauth.didAuthorizeOrFail = { authParameters, error in
134- // XCTAssertNil(authParameters, "Nil auth dict expected")
135- // XCTAssertNotNil(error, "Error message expected")
136- // XCTAssertEqual(error, OAuth2Error.invalidState)
137- // }
138- // oauth.handleRedirectURL(URL(string: "https://auth.ful.io#token_type=bearer&access_token=abc&state=ONSTOH")!)
139- // XCTAssertNil(oauth.accessToken, "Must not have an access token")
140- //
141- // // success 1
142- // oauth.didAuthorizeOrFail = { authParameters, error in
143- // XCTAssertNotNil(authParameters, "auth parameters expected")
144- // XCTAssertNil(error, "No error message expected")
145- // }
146- // oauth.afterAuthorizeOrFail = { authParameters, error in
147- // XCTAssertNotNil(authParameters, "auth parameters expected")
148- // XCTAssertNil(error, "No error message expected")
149- // }
150- // oauth.handleRedirectURL(URL(string: "https://auth.ful.io#token_type=bearer&access_token=abc&state=\(oauth.context.state)&expires_in=3599")!)
151- // XCTAssertNotNil(oauth.accessToken, "Must have an access token")
152- // XCTAssertEqual(oauth.accessToken!, "abc")
153- // XCTAssertNotNil(oauth.accessTokenExpiry)
154- // XCTAssertTrue(oauth.hasUnexpiredAccessToken())
155- //
156- // // success 2
157- // oauth.didAuthorizeOrFail = { authParameters, error in
158- // XCTAssertNotNil(authParameters, "auth parameters expected")
159- // XCTAssertNil(error, "No error message expected")
160- // }
161- // oauth.handleRedirectURL(URL(string: "https://auth.ful.io#token_type=bearer&access_token=abc&state=\(oauth.context.state)")!)
162- // XCTAssertNotNil(oauth.accessToken, "Must have an access token")
163- // XCTAssertEqual(oauth.accessToken!, "abc")
164- // XCTAssertNil(oauth.accessTokenExpiry)
165- // XCTAssertTrue(oauth.hasUnexpiredAccessToken())
166- // }
167- //}
168-
34+ @OAuth2Actor
35+ class OAuth2ImplicitGrantTests : XCTestCase
36+ {
37+ func testInit( ) {
38+ let oauth = OAuth2ImplicitGrant ( settings: [
39+ " client_id " : " abc " ,
40+ " keychain " : false ,
41+ " authorize_uri " : " https://auth.ful.io " ,
42+ ] )
43+ XCTAssertEqual ( oauth. clientId, " abc " , " Must init `client_id` " )
44+ XCTAssertNil ( oauth. scope, " Empty scope " )
45+
46+ XCTAssertEqual ( oauth. authURL, URL ( string: " https://auth.ful.io " ) !, " Must init `authorize_uri` " )
47+ }
48+
49+ func testReturnURLHandling( ) async {
50+ let oauth = OAuth2ImplicitGrant ( settings: [
51+ " client_id " : " abc " ,
52+ " authorize_uri " : " https://auth.ful.io " ,
53+ " keychain " : false ,
54+ ] )
55+
56+ // Empty redirect URL
57+ oauth. afterAuthorizeOrFail = { authParameters, error in
58+ XCTAssertNil ( authParameters, " Nil auth dict expected " )
59+ XCTAssertNotNil ( error, " Error message expected " )
60+ }
61+
62+ oauth. context. _state = " ONSTUH "
63+ await XCTAssertThrowsErrorAsync ( try await oauth. handleRedirectURL ( URL ( string: " file:/// " ) !) ) { error in
64+ XCTAssertEqual ( error. asOAuth2Error, OAuth2Error . invalidRedirectURL ( " file:/// " ) )
65+ }
66+ XCTAssertNil ( oauth. accessToken, " Must not have an access token " )
67+
68+ // No params in redirect URL
69+ await XCTAssertThrowsErrorAsync ( try await oauth. handleRedirectURL ( URL ( string: " https://auth.ful.io " ) !) ) { error in
70+ XCTAssertEqual ( error. asOAuth2Error, OAuth2Error . invalidRedirectURL ( " https://auth.ful.io " ) )
71+ }
72+ XCTAssertNil ( oauth. accessToken, " Must not have an access token " )
73+
74+ // standard error
75+ oauth. context. _state = " ONSTUH " // because it has been reset
76+ await XCTAssertThrowsErrorAsync ( try await oauth. handleRedirectURL ( URL ( string: " https://auth.ful.io#error=access_denied " ) !) ) { error in
77+ XCTAssertEqual ( error. asOAuth2Error, OAuth2Error . accessDenied ( nil ) )
78+ XCTAssertEqual ( error. asOAuth2Error. description, " The resource owner or authorization server denied the request. " )
79+ }
80+ XCTAssertNil ( oauth. accessToken, " Must not have an access token " )
81+
82+ // explicit error
83+ oauth. context. _state = " ONSTUH " // because it has been reset
84+ await XCTAssertThrowsErrorAsync ( try await oauth. handleRedirectURL ( URL ( string: " https://auth.ful.io#error_description=Not+good " ) !) ) { error in
85+ XCTAssertNotEqual ( error. asOAuth2Error, OAuth2Error . generic ( " Not good " ) )
86+ XCTAssertEqual ( error. asOAuth2Error, OAuth2Error . responseError ( " Not good " ) )
87+ XCTAssertEqual ( error. asOAuth2Error. description, " Not good " )
88+ }
89+ XCTAssertNil ( oauth. accessToken, " Must not have an access token " )
90+
91+ // no token type
92+ oauth. context. _state = " ONSTUH " // because it has been reset
93+ await XCTAssertThrowsErrorAsync ( try await oauth. handleRedirectURL ( URL ( string: " https://auth.ful.io#access_token=abc&state= \( oauth. context. state) " ) !) ) { error in
94+ XCTAssertEqual ( error. asOAuth2Error, OAuth2Error . noTokenType)
95+ }
96+ XCTAssertNil ( oauth. accessToken, " Must not have an access token " )
97+
98+ // unsupported token type
99+ oauth. context. _state = " ONSTUH " // because it has been reset
100+ await XCTAssertThrowsErrorAsync ( try await oauth. handleRedirectURL ( URL ( string: " https://auth.ful.io#token_type=helicopter&access_token=abc&state= \( oauth. context. state) " ) !) ) { error in
101+ XCTAssertEqual ( error. asOAuth2Error, OAuth2Error . unsupportedTokenType ( " Only “bearer” token is supported, but received “helicopter” " ) )
102+ }
103+ XCTAssertNil ( oauth. accessToken, " Must not have an access token " )
104+
105+ // Missing state
106+ oauth. context. _state = " ONSTUH " // because it has been reset
107+ await XCTAssertThrowsErrorAsync ( try await oauth. handleRedirectURL ( URL ( string: " https://auth.ful.io#token_type=bearer&access_token=abc " ) !) ) { error in
108+ XCTAssertEqual ( error. asOAuth2Error, OAuth2Error . missingState)
109+ }
110+ XCTAssertNil ( oauth. accessToken, " Must not have an access token " )
111+
112+ // Invalid state
113+ oauth. context. _state = " ONSTUH " // because it has been reset
114+ await XCTAssertThrowsErrorAsync ( try await oauth. handleRedirectURL ( URL ( string: " https://auth.ful.io#token_type=bearer&access_token=abc&state=ONSTOH " ) !) ) { error in
115+ XCTAssertEqual ( error. asOAuth2Error, OAuth2Error . invalidState)
116+ }
117+ XCTAssertNil ( oauth. accessToken, " Must not have an access token " )
118+
119+ // success 1
120+ oauth. afterAuthorizeOrFail = { authParameters, error in
121+ XCTAssertNotNil ( authParameters, " auth parameters expected " )
122+ XCTAssertNil ( error, " No error message expected " )
123+ }
124+ let authParameters1 = try ? await oauth. handleRedirectURL ( URL ( string: " https://auth.ful.io#token_type=bearer&access_token=abc&state= \( oauth. context. state) &expires_in=3599 " ) !)
125+ XCTAssertNotNil ( authParameters1, " auth parameters expected " )
126+ XCTAssertNotNil ( oauth. accessToken, " Must have an access token " )
127+ XCTAssertEqual ( oauth. accessToken!, " abc " )
128+ XCTAssertNotNil ( oauth. accessTokenExpiry)
129+ XCTAssertTrue ( oauth. hasUnexpiredAccessToken ( ) )
130+
131+ // success 2
132+ let authParameters2 = try ? await oauth. handleRedirectURL ( URL ( string: " https://auth.ful.io#token_type=bearer&access_token=abc&state= \( oauth. context. state) " ) !)
133+ XCTAssertNotNil ( authParameters2, " auth parameters expected " )
134+ XCTAssertNotNil ( oauth. accessToken, " Must have an access token " )
135+ XCTAssertEqual ( oauth. accessToken!, " abc " )
136+ XCTAssertNil ( oauth. accessTokenExpiry)
137+ XCTAssertTrue ( oauth. hasUnexpiredAccessToken ( ) )
138+ }
139+ }
0 commit comments