|
| 1 | +import XCTFluent |
| 2 | +import XCTVapor |
| 3 | +import Fluent |
| 4 | +import Vapor |
| 5 | + |
| 6 | +final class CredentialTests: XCTestCase { |
| 7 | + |
| 8 | + func testCredentialsAuthentication() throws { |
| 9 | + let app = Application(.testing) |
| 10 | + defer { app.shutdown() } |
| 11 | + |
| 12 | + // Setup test db. |
| 13 | + let testDB = ArrayTestDatabase() |
| 14 | + app.databases.use(testDB.configuration, as: .test) |
| 15 | + |
| 16 | + // Configure sessions. |
| 17 | + app.middleware.use(app.sessions.middleware) |
| 18 | + |
| 19 | + // Setup routes. |
| 20 | + let sessionRoutes = app.grouped(CredentialsUser.sessionAuthenticator()) |
| 21 | + |
| 22 | + let credentialRoutes = sessionRoutes.grouped(CredentialsUser.credentialsAuthenticator()) |
| 23 | + credentialRoutes.post("login") { req -> Response in |
| 24 | + guard req.auth.has(CredentialsUser.self) else { |
| 25 | + throw Abort(.unauthorized) |
| 26 | + } |
| 27 | + return req.redirect(to: "/protected") |
| 28 | + } |
| 29 | + |
| 30 | + let protectedRoutes = sessionRoutes.grouped(CredentialsUser.redirectMiddleware(path: "/login")) |
| 31 | + protectedRoutes.get("protected") { req -> HTTPStatus in |
| 32 | + _ = try req.auth.require(CredentialsUser.self) |
| 33 | + return .ok |
| 34 | + } |
| 35 | + |
| 36 | + // Create user |
| 37 | + let password = "password-\(Int.random())" |
| 38 | + let passwordHash = try Bcrypt.hash(password) |
| 39 | + let testUser = CredentialsUser(id: UUID(), username: "user-\(Int.random())", password: passwordHash) |
| 40 | + testDB.append([TestOutput(testUser)]) |
| 41 | + testDB.append([TestOutput(testUser)]) |
| 42 | + testDB.append([TestOutput(testUser)]) |
| 43 | + testDB.append([TestOutput(testUser)]) |
| 44 | + |
| 45 | + // Test login |
| 46 | + let loginData = ModelCredentials(username: testUser.username, password: password) |
| 47 | + try app.test(.POST, "/login", beforeRequest: { req in |
| 48 | + try req.content.encode(loginData, as: .urlEncodedForm) |
| 49 | + }) { res in |
| 50 | + XCTAssertEqual(res.status, .seeOther) |
| 51 | + XCTAssertEqual(res.headers[.location].first, "/protected") |
| 52 | + let sessionID = try XCTUnwrap(res.headers.setCookie?["vapor-session"]?.string) |
| 53 | + |
| 54 | + // Test accessing protected route |
| 55 | + try app.test(.GET, "/protected", beforeRequest: { req in |
| 56 | + var cookies = HTTPCookies() |
| 57 | + cookies["vapor-session"] = .init(string: sessionID) |
| 58 | + req.headers.cookie = cookies |
| 59 | + }) { res in |
| 60 | + XCTAssertEqual(res.status, .ok) |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +final class CredentialsUser: Model { |
| 69 | + static let schema = "users" |
| 70 | + |
| 71 | + @ID(key: .id) |
| 72 | + var id: UUID? |
| 73 | + |
| 74 | + @Field(key: "username") |
| 75 | + var username: String |
| 76 | + |
| 77 | + @Field(key: "password") |
| 78 | + var password: String |
| 79 | + |
| 80 | + init() { } |
| 81 | + |
| 82 | + init(id: UUID? = nil, username: String, password: String) { |
| 83 | + self.id = id |
| 84 | + self.username = username |
| 85 | + self.password = password |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | + |
| 90 | +extension CredentialsUser: ModelCredentialsAuthenticatable { |
| 91 | + static let usernameKey = \CredentialsUser.$username |
| 92 | + static let passwordHashKey = \CredentialsUser.$password |
| 93 | + |
| 94 | + func verify(password: String) throws -> Bool { |
| 95 | + try Bcrypt.verify(password, created: self.password) |
| 96 | + } |
| 97 | +} |
| 98 | +extension CredentialsUser: ModelSessionAuthenticatable {} |
0 commit comments