Skip to content
This repository has been archived by the owner on Oct 29, 2024. It is now read-only.

Commit

Permalink
Add tests for code challenge and code challenge method
Browse files Browse the repository at this point in the history
  • Loading branch information
vamsii777 committed Dec 23, 2023
1 parent 07b91be commit 39d2d54
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,67 @@ class AuthorizationRequestTests: XCTestCase {

XCTAssertEqual(capturingAuthoriseHandler.state, state)
}

func testThatCodeChallengePassedThroughToAuthorizationHandler() async throws {
let codeChallenge = "E9Melhoa2OwvFrEMTJguCHaoeK1t8URWbuGJSstw-cM"
let codeChallengeMethod = "S256"

_ = try await respondToOAuthRequest(
clientID: clientID,
redirectURI: redirectURI,
codeChallenge: codeChallenge,
codeChallengeMethod: codeChallengeMethod
)
XCTAssertEqual(capturingAuthoriseHandler.codeChallenge, codeChallenge)
}

func testMissingCodeChallenge() async throws {
let codeChallengeMethod = "S256"

_ = try await respondToOAuthRequest(
clientID: clientID,
redirectURI: redirectURI,
codeChallengeMethod: codeChallengeMethod
)
XCTAssertNil(capturingAuthoriseHandler.codeChallenge)
}

func testMissingCodeChallengeMethod() async throws {
let codeChallenge = "E9Melhoa2OwvFrEMTJguCHaoeK1t8URWbuGJSstw-cM"

let response = try await respondToOAuthRequest(
clientID: clientID,
redirectURI: redirectURI,
codeChallenge: codeChallenge
)

// Check if the response is an error response indicating the missing codeChallengeMethod
XCTAssertEqual(response.status, .seeOther) // Assuming error responses are redirected
XCTAssertTrue(response.headers.first(name: .location)?.contains("error=") ?? false)
}

func testInvalidCodeChallengeMethod() async throws {
let codeChallenge = "E9Melhoa2OwvFrEMTJguCHaoeK1t8URWbuGJSstw-cM"
let invalidMethod = "invalid_method"

_ = try await respondToOAuthRequest(
clientID: clientID,
redirectURI: redirectURI,
codeChallenge: codeChallenge,
codeChallengeMethod: invalidMethod
)
XCTAssertNotEqual(capturingAuthoriseHandler.codeChallengeMethod, invalidMethod)
}

func testBothCodeChallengeAndMethodMissing() async throws {
_ = try await respondToOAuthRequest(
clientID: clientID,
redirectURI: redirectURI
)
XCTAssertNil(capturingAuthoriseHandler.codeChallenge)
XCTAssertNil(capturingAuthoriseHandler.codeChallengeMethod)
}

func testAllPropertiesPassedThroughToAuthorizationHandler() async throws {
let responseType = "code"
let scope1 = "profile"
Expand Down Expand Up @@ -374,7 +434,7 @@ class AuthorizationRequestTests: XCTestCase {
codeChallenge: String? = nil, // Add PKCE parameter
codeChallengeMethod: String? = nil // Add PKCE parameter
) async throws -> XCTHTTPResponse {
try await TestDataBuilder.getAuthRequestResponse(
return try await TestDataBuilder.getAuthRequestResponse(
with: app,
responseType: responseType,
clientID: clientID,
Expand All @@ -385,7 +445,7 @@ class AuthorizationRequestTests: XCTestCase {
codeChallengeMethod: codeChallengeMethod // Pass PKCE parameter
)
}

}

extension URI: Equatable {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@ class DefaultImplementationTests: XCTestCase {
redirectURI: uri,
scope: ["email"],
state: "abcdef",
csrfToken: "01234"
csrfToken: "01234",
codeChallenge: "E9Melhoa2OwvFrEMTJguCHaoeK1t8URWbuGJSstw-cM",
codeChallengeMethod: "S256", nonce: nil
)

let body = try await emptyAuthHandler.handleAuthorizationRequest(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ class CapturingAuthoriseHandler: AuthorizeHandler {
// Capture PKCE parameters
self.codeChallenge = authorizationRequestObject.codeChallenge
self.codeChallengeMethod = authorizationRequestObject.codeChallengeMethod

return Response(body: .init(string: "Allow/Deny"))
}

Expand Down

0 comments on commit 39d2d54

Please sign in to comment.