Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Authentication framework #21

Closed
wants to merge 5 commits into from
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Testing authentication
  • Loading branch information
adam-fowler committed Jan 29, 2021
commit df1b816d12ccf0f4b9062ee6454cc89fbb606bcf
76 changes: 76 additions & 0 deletions Tests/HummingbirdTests/AuthenticationTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import ExtrasBase64
import Hummingbird
import XCTest

class AuthenticationTests: XCTestCase {
func testBasicAuthentication() {
let app = HBApplication(testing: .embedded, configuration: .init(maxUploadSize: 65536))
app.router.get("/authenticate") { request -> [String: String] in
guard let basic = request.auth.basic else { throw HBHTTPError(.unauthorized) }
return ["username": basic.username, "password": basic.password]
}
app.XCTStart()
defer { app.XCTStop() }

let basic = "adamfowler:testpassword"
let basicHeader = "Basic \(String(base64Encoding: basic.utf8))"
app.XCTExecute(uri: "/authenticate", method: .GET, headers: ["Authorization": basicHeader]) { response in
let body = try XCTUnwrap(response.body)
XCTAssertEqual(String(buffer: body), #"["username": "adamfowler", "password": "testpassword"]"#)
}

}

func testBearerAuthentication() {
let app = HBApplication(testing: .embedded, configuration: .init(maxUploadSize: 65536))
app.router.get("/authenticate") { request -> String? in
return request.auth.bearer?.token
}
app.XCTStart()
defer { app.XCTStop() }

app.XCTExecute(
uri: "/authenticate",
method: .GET,
headers: ["Authorization": "Bearer jh345jjefgi34rj"]
) { response in
let body = try XCTUnwrap(response.body)
XCTAssertEqual(String(buffer: body), "jh345jjefgi34rj")
}
}

func testAuthenticator() {
struct MyAuthenticator: HBAuthenticator {
func authenticate(request: HBRequest) -> EventLoopFuture<Void> {
guard let basic = request.auth.basic else { return request.success(Void()) }
if basic.username == "adamfowler" && basic.password == "password" {
request.auth.login(basic)
}
return request.success(Void())
}
}
let app = HBApplication(testing: .embedded, configuration: .init(maxUploadSize: 65536))
app.middleware.add(MyAuthenticator())
app.router.get("/authenticate") { request -> HTTPResponseStatus in
return request.auth.has(BasicAuthentication.self) ? .ok: .unauthorized
}
app.XCTStart()
defer { app.XCTStop() }

do {
let basic = "adamfowler:nopassword"
let basicHeader = "Basic \(String(base64Encoding: basic.utf8))"
app.XCTExecute(uri: "/authenticate", method: .GET, headers: ["Authorization": basicHeader]) { response in
XCTAssertEqual(response.status, .unauthorized)
}
}

do {
let basic = "adamfowler:password"
let basicHeader = "Basic \(String(base64Encoding: basic.utf8))"
app.XCTExecute(uri: "/authenticate", method: .GET, headers: ["Authorization": basicHeader]) { response in
XCTAssertEqual(response.status, .ok)
}
}
}
}