-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
28 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -35,11 +35,17 @@ class LoginTests: XCTestCase { | |
let loginRequest = LoginRequest(email: "[email protected]", password: "wrong password", csrf: "n/a") | ||
|
||
let loginResponse = try app.sendRequest(to: "/login", method: .POST, data: loginRequest, contentType: .json) | ||
XCTAssertEqual(loginResponse.http.status, .seeOther) | ||
XCTAssertEqual(loginResponse.http.headers.firstValue(name: .location), "/login") | ||
} | ||
|
||
/// Tests that a user with valid credentials can login | ||
func testLoginSuccessful() throws { | ||
let _ = try User(name: "name", email: "[email protected]", password: try BCrypt.hash("password")).save(on: conn).wait() | ||
let loginRequest = LoginRequest(email: "[email protected]", password: "password", csrf: "n/a") | ||
|
||
let loginResponse = try app.sendRequest(to: "/login", method: .POST, data: loginRequest, contentType: .json) | ||
XCTAssertEqual(loginResponse.http.status, .seeOther) | ||
XCTAssertEqual(loginResponse.http.headers.firstValue(name: .location), "/home") | ||
} | ||
} |
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 |
---|---|---|
@@ -1,6 +1,7 @@ | ||
import XCTest | ||
import Foundation | ||
import FluentMySQL | ||
import Crypto | ||
@testable import Vapor | ||
@testable import App | ||
|
||
|
@@ -30,21 +31,38 @@ class RegisterTests: XCTestCase { | |
|
||
/// Tests that an email cannot be registered twice | ||
func testRegisterEmailAlreadyExists() throws { | ||
let _ = try User(name: "name", email: "[email protected]", password: try BCrypt.hash("password")).save(on: conn).wait() | ||
let registerRequest = RegisterRequest(email: "[email protected]", name: "name", password: "password", confirmPassword: "password", csrf: "") | ||
|
||
let registerResponse = try app.sendRequest(to: "/register", method: .POST, data: registerRequest, contentType: .json) | ||
XCTAssertEqual(registerResponse.http.status, .seeOther) | ||
XCTAssertEqual(registerResponse.http.headers.firstValue(name: .location), "/register") | ||
} | ||
|
||
/// Tests that an invalid email cannot register | ||
func testInvalidEmailFails() throws { | ||
let registerRequest = RegisterRequest(email: "not an email", name: "name", password: "password", confirmPassword: "password", csrf: "") | ||
|
||
let registerResponse = try app.sendRequest(to: "/register", method: .POST, data: registerRequest, contentType: .json) | ||
XCTAssertEqual(registerResponse.http.status, .seeOther) | ||
XCTAssertEqual(registerResponse.http.headers.firstValue(name: .location), "/register") | ||
} | ||
|
||
/// Tests that passwords must match | ||
func testRegisterPasswordsDontMatch() throws { | ||
let registerRequest = RegisterRequest(email: "[email protected]", name: "name", password: "password1", confirmPassword: "password2", csrf: "") | ||
|
||
let registerResponse = try app.sendRequest(to: "/register", method: .POST, data: registerRequest, contentType: .json) | ||
XCTAssertEqual(registerResponse.http.status, .seeOther) | ||
XCTAssertEqual(registerResponse.http.headers.firstValue(name: .location), "/register") | ||
} | ||
|
||
/// Tests that users can register successfully when meeting validation requirements | ||
func testSuccessfulRegister() throws { | ||
let registerRequest = RegisterRequest(email: "[email protected]", name: "name", password: "password", confirmPassword: "password", csrf: "") | ||
|
||
let registerResponse = try app.sendRequest(to: "/register", method: .POST, data: registerRequest, contentType: .json) | ||
XCTAssertEqual(registerResponse.http.status, .seeOther) | ||
XCTAssertEqual(registerResponse.http.headers.firstValue(name: .location), "/home") | ||
} | ||
} |