generated from bitwarden/template
-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[PM-9842] Verify email token services (#849)
- Loading branch information
1 parent
94ea62e
commit 0c98785
Showing
7 changed files
with
223 additions
and
0 deletions.
There are no files selected for viewing
22 changes: 22 additions & 0 deletions
22
BitwardenShared/Core/Auth/Models/Request/VerifyEmailTokenRequestModel.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import Foundation | ||
import Networking | ||
|
||
// MARK: - VerifyEmailTokenRequestRequestModel | ||
|
||
/// The data to include in the body of a `VerifyEmailTokenRequestRequest`. | ||
/// | ||
struct VerifyEmailTokenRequestModel: Equatable { | ||
// MARK: Properties | ||
|
||
/// The email being used to create the account. | ||
let email: String | ||
|
||
/// The token used to verify the email. | ||
let emailVerificationToken: String | ||
} | ||
|
||
// MARK: JSONRequestBody | ||
|
||
extension VerifyEmailTokenRequestModel: JSONRequestBody { | ||
static let encoder = JSONEncoder() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 8 additions & 0 deletions
8
BitwardenShared/Core/Auth/Services/API/Account/Fixtures/VerifyEmailTokenExpiredLink.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"message":"Expired link. Please restart registration or try logging in. You may already have an account", | ||
"validationErrors": null, | ||
"exceptionMessage":null, | ||
"exceptionStackTrace":null, | ||
"innerExceptionMessage":null, | ||
"object":"error" | ||
} |
52 changes: 52 additions & 0 deletions
52
BitwardenShared/Core/Auth/Services/API/Account/Requests/VerifyEmailTokenRequest.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import Foundation | ||
import Networking | ||
|
||
// MARK: - VerifyEmailTokenRequestError | ||
|
||
/// Errors that can occur when sending a `VerifyEmailTokenRequest`. | ||
enum VerifyEmailTokenRequestError: Error, Equatable { | ||
/// The token provided by email is expired or user is already used. | ||
/// | ||
case tokenExpired | ||
} | ||
|
||
// MARK: - VerificationEmailClickedRequest | ||
|
||
/// The API request sent when verifying the token received by email. | ||
/// | ||
struct VerifyEmailTokenRequest: Request { | ||
typealias Response = EmptyResponse | ||
|
||
typealias Body = VerifyEmailTokenRequestModel | ||
|
||
/// The body of this request. | ||
var body: VerifyEmailTokenRequestModel? { | ||
requestModel | ||
} | ||
|
||
/// The HTTP method for this request. | ||
let method: HTTPMethod = .post | ||
|
||
/// The URL path for this request. | ||
var path: String = "/accounts/register/verification-email-clicked" | ||
|
||
/// The request details to include in the body of the request. | ||
let requestModel: VerifyEmailTokenRequestModel | ||
|
||
// MARK: Methods | ||
|
||
func validate(_ response: HTTPResponse) throws { | ||
switch response.statusCode { | ||
case 400 ..< 500: | ||
guard let errorResponse = try? ErrorResponseModel(response: response) else { return } | ||
|
||
if errorResponse.message.contains("Expired link") { | ||
throw VerifyEmailTokenRequestError.tokenExpired | ||
} | ||
|
||
throw ServerError.error(errorResponse: errorResponse) | ||
default: | ||
return | ||
} | ||
} | ||
} |
89 changes: 89 additions & 0 deletions
89
BitwardenShared/Core/Auth/Services/API/Account/Requests/VerifyEmailTokenRequestTests.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
import Networking | ||
import XCTest | ||
|
||
@testable import BitwardenShared | ||
|
||
// MARK: - VerifyEmailTokenRequestTests | ||
|
||
class VerifyEmailTokenRequestTests: BitwardenTestCase { | ||
// MARK: Properties | ||
|
||
var subject: VerifyEmailTokenRequest! | ||
|
||
// MARK: Setup & Teardown | ||
|
||
override func setUp() { | ||
super.setUp() | ||
|
||
subject = VerifyEmailTokenRequest( | ||
requestModel: VerifyEmailTokenRequestModel( | ||
email: "example@email.com", | ||
emailVerificationToken: "email-verification-token" | ||
) | ||
) | ||
} | ||
|
||
override func tearDown() { | ||
super.tearDown() | ||
|
||
subject = nil | ||
} | ||
|
||
// MARK: Tests | ||
|
||
/// Validate that the method is correct. | ||
func test_method() { | ||
XCTAssertEqual(subject.method, .post) | ||
} | ||
|
||
/// Validate that the path is correct. | ||
func test_path() { | ||
XCTAssertEqual(subject.path, "/accounts/register/verification-email-clicked") | ||
} | ||
|
||
/// Validate that the body is not nil. | ||
func test_body() { | ||
XCTAssertNotNil(subject.body) | ||
} | ||
|
||
/// `validate(_:)` with a `400` status code and an expired link error in the response body | ||
/// throws an `.tokenExpired` error. | ||
func test_validate_with400ExpiredLink() throws { | ||
let response = HTTPResponse.failure( | ||
statusCode: 400, | ||
body: APITestData.verifyEmailTokenExpiredLink.data | ||
) | ||
|
||
XCTAssertThrowsError(try subject.validate(response)) { error in | ||
XCTAssertEqual( | ||
error as? VerifyEmailTokenRequestError, | ||
.tokenExpired | ||
) | ||
} | ||
} | ||
|
||
/// `validate(_:)` with a `400` status code but no captcha error does not throw a validation error. | ||
func test_validate_with400() { | ||
let response = HTTPResponse.failure( | ||
statusCode: 400, | ||
body: Data("example data".utf8) | ||
) | ||
|
||
XCTAssertNoThrow(try subject.validate(response)) | ||
} | ||
|
||
/// `validate(_:)` with a valid response does not throw a validation error. | ||
func test_validate_with200() { | ||
let response = HTTPResponse.success() | ||
|
||
XCTAssertNoThrow(try subject.validate(response)) | ||
} | ||
|
||
// MARK: Init | ||
|
||
/// Validate that the value provided to the init method is correct. | ||
func test_init_body() { | ||
XCTAssertEqual(subject.body?.email, "example@email.com") | ||
XCTAssertEqual(subject.body?.emailVerificationToken, "email-verification-token") | ||
} | ||
} |