Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
1 change: 0 additions & 1 deletion Sources/HummingbirdAuthTesting/TestClient+Auth.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import ExtrasBase64
import Hummingbird
import HummingbirdTesting
import XCTest

/// Used to generate various authentication types for Testing framework
public struct TestAuthentication: Equatable {
Expand Down
68 changes: 33 additions & 35 deletions Tests/HummingbirdAuthTests/AuthTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ import HummingbirdBasicAuth
import HummingbirdBcrypt
import HummingbirdTesting
import NIOPosix
import XCTest
import Testing

final class AuthTests: XCTestCase {
func testBearer() async throws {
struct AuthTests {
@Test func testBearer() async throws {
struct User: Sendable {
let name: String
}
Expand All @@ -33,16 +33,16 @@ final class AuthTests: XCTestCase {
let app = Application(responder: router.buildResponder())
try await app.test(.router) { client in
try await client.execute(uri: "/", method: .get, auth: .bearer("1234567890")) { response in
let body = try XCTUnwrap(response.body)
XCTAssertEqual(String(buffer: body), "1234567890")
let body = response.body
#expect(String(buffer: body) == "1234567890")
}
try await client.execute(uri: "/", method: .get, auth: .basic(username: "adam", password: "1234")) { response in
XCTAssertEqual(response.status, .noContent)
#expect(response.status == .noContent)
}
}
}

func testBasic() async throws {
@Test func testBasic() async throws {
struct User: Sendable {
let name: String
}
Expand All @@ -53,13 +53,13 @@ final class AuthTests: XCTestCase {
let app = Application(responder: router.buildResponder())
try await app.test(.router) { client in
try await client.execute(uri: "/", method: .get, auth: .basic(username: "adam", password: "password")) { response in
let body = try XCTUnwrap(response.body)
XCTAssertEqual(String(buffer: body), "adam:password")
let body = response.body
#expect(String(buffer: body) == "adam:password")
}
}
}

func testBcryptThread() async throws {
@Test func testBcryptThread() async throws {
struct User: Sendable {
let name: String
}
Expand Down Expand Up @@ -88,15 +88,15 @@ final class AuthTests: XCTestCase {
let app = Application(responder: router.buildResponder())
try await app.test(.router) { client in
try await client.execute(uri: "/", method: .put, auth: .basic(username: "testuser", password: "testpassword123")) { response in
XCTAssertEqual(response.status, .ok)
#expect(response.status == .ok)
}
try await client.execute(uri: "/", method: .post, auth: .basic(username: "testuser", password: "testpassword123")) { response in
XCTAssertEqual(response.status, .ok)
#expect(response.status == .ok)
}
}
}

func testAuth() async throws {
@Test func testAuth() async throws {
struct User: Sendable {
let name: String
}
Expand All @@ -108,24 +108,22 @@ final class AuthTests: XCTestCase {
var context = context
context.identity = User(name: "Test")

XCTAssertNotNil(context.identity)
XCTAssertEqual(context.identity?.name, "Test")
#expect(context.identity != nil)
#expect(context.identity?.name == "Test")

context.identity = nil
XCTAssertNil(context.identity)

return .accepted
}
let app = Application(responder: router.buildResponder())

try await app.test(.router) { client in
try await client.execute(uri: "/", method: .get) { response in
XCTAssertEqual(response.status, .accepted)
#expect(response.status == .accepted)
}
}
}

func testLogin() async throws {
@Test func testLogin() async throws {
struct User: Sendable {
let name: String
}
Expand All @@ -144,12 +142,12 @@ final class AuthTests: XCTestCase {

try await app.test(.router) { client in
try await client.execute(uri: "/", method: .get) { response in
XCTAssertEqual(response.status, .ok)
#expect(response.status == .ok)
}
}
}

func testClosureAuthenticator() async throws {
@Test func testClosureAuthenticator() async throws {
struct User: Sendable {
let name: String
}
Expand All @@ -174,16 +172,16 @@ final class AuthTests: XCTestCase {

try await app.test(.router) { client in
try await client.execute(uri: "/authenticate?user=john", method: .get) { response in
XCTAssertEqual(response.status, .ok)
XCTAssertEqual(response.body, ByteBuffer(string: "john"))
#expect(response.status == .ok)
#expect(response.body == ByteBuffer(string: "john"))
}
try await client.execute(uri: "/authenticate", method: .get) { response in
XCTAssertEqual(response.status, .unauthorized)
#expect(response.status == .unauthorized)
}
}
}

func testIsAuthenticatedMiddleware() async throws {
@Test func testIsAuthenticatedMiddleware() async throws {
struct User: Sendable {
let name: String
}
Expand All @@ -208,15 +206,15 @@ final class AuthTests: XCTestCase {

try await app.test(.router) { client in
try await client.execute(uri: "/authenticated", method: .get) { response in
XCTAssertEqual(response.status, .ok)
#expect(response.status == .ok)
}
try await client.execute(uri: "/unauthenticated", method: .get) { response in
XCTAssertEqual(response.status, .unauthorized)
#expect(response.status == .unauthorized)
}
}
}

func testBasicAuthenticator() async throws {
@Test func testBasicAuthenticator() async throws {
struct User: PasswordAuthenticatable {
let username: String
let passwordHash: String?
Expand All @@ -240,16 +238,16 @@ final class AuthTests: XCTestCase {
let app = Application(responder: router.buildResponder())
try await app.test(.router) { client in
try await client.execute(uri: "/", method: .get, auth: .basic(username: "admin", password: "password")) { response in
let body = try XCTUnwrap(response.body)
XCTAssertEqual(String(buffer: body), "admin")
let body = response.body
#expect(String(buffer: body) == "admin")
}
try await client.execute(uri: "/", method: .get, auth: .basic(username: "adam", password: "password")) { response in
XCTAssertEqual(response.status, .unauthorized)
#expect(response.status == .unauthorized)
}
}
}

func testBasicAuthenticatorWithClosure() async throws {
@Test func testBasicAuthenticatorWithClosure() async throws {
struct User: PasswordAuthenticatable {
let username: String
let passwordHash: String?
Expand All @@ -270,11 +268,11 @@ final class AuthTests: XCTestCase {
let app = Application(responder: router.buildResponder())
try await app.test(.router) { client in
try await client.execute(uri: "/", method: .get, auth: .basic(username: "admin", password: "password")) { response in
let body = try XCTUnwrap(response.body)
XCTAssertEqual(String(buffer: body), "admin")
let body = response.body
#expect(String(buffer: body) == "admin")
}
try await client.execute(uri: "/", method: .get, auth: .basic(username: "adam", password: "password")) { response in
XCTAssertEqual(response.status, .unauthorized)
#expect(response.status == .unauthorized)
}
}
}
Expand Down
14 changes: 7 additions & 7 deletions Tests/HummingbirdAuthTests/BcryptTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,20 @@ import HummingbirdAuthTesting
import HummingbirdBcrypt
import HummingbirdTesting
import NIOPosix
import XCTest
import Testing

final class BcryptTests: XCTestCase {
func testBcrypt() {
struct BcryptTests {
@Test func testBcrypt() {
let hash = Bcrypt.hash("password")
XCTAssert(Bcrypt.verify("password", hash: hash))
#expect(Bcrypt.verify("password", hash: hash))
}

func testBcryptFalse() {
@Test func testBcryptFalse() {
let hash = Bcrypt.hash("password")
XCTAssertFalse(Bcrypt.verify("password1", hash: hash))
#expect(!Bcrypt.verify("password1", hash: hash))
}

func testMultipleBcrypt() async throws {
@Test func testMultipleBcrypt() async throws {
struct VerifyFailError: Error {}

try await withThrowingTaskGroup(of: Void.self) { group in
Expand Down
45 changes: 23 additions & 22 deletions Tests/HummingbirdAuthTests/OTPTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,32 +12,33 @@
//
//===----------------------------------------------------------------------===//

import Foundation
import HummingbirdOTP
import XCTest
import Testing

final class OTPTests: XCTestCase {
struct OTPTests {
func randomBuffer(size: Int) -> [UInt8] {
var data = [UInt8](repeating: 0, count: size)
data = data.map { _ in UInt8.random(in: 0...255) }
return data
}

func testHOTP() {
@Test func testHOTP() {
// test against RFC4226 example values https://tools.ietf.org/html/rfc4226#page-32
let secret = "12345678901234567890"
XCTAssertEqual(HOTP(secret: secret).compute(counter: 0), 755_224)
XCTAssertEqual(HOTP(secret: secret).compute(counter: 1), 287_082)
XCTAssertEqual(HOTP(secret: secret).compute(counter: 2), 359_152)
XCTAssertEqual(HOTP(secret: secret).compute(counter: 3), 969_429)
XCTAssertEqual(HOTP(secret: secret).compute(counter: 4), 338_314)
XCTAssertEqual(HOTP(secret: secret).compute(counter: 5), 254_676)
XCTAssertEqual(HOTP(secret: secret).compute(counter: 6), 287_922)
XCTAssertEqual(HOTP(secret: secret).compute(counter: 7), 162_583)
XCTAssertEqual(HOTP(secret: secret).compute(counter: 8), 399_871)
XCTAssertEqual(HOTP(secret: secret).compute(counter: 9), 520_489)
#expect(HOTP(secret: secret).compute(counter: 0) == 755_224)
#expect(HOTP(secret: secret).compute(counter: 1) == 287_082)
#expect(HOTP(secret: secret).compute(counter: 2) == 359_152)
#expect(HOTP(secret: secret).compute(counter: 3) == 969_429)
#expect(HOTP(secret: secret).compute(counter: 4) == 338_314)
#expect(HOTP(secret: secret).compute(counter: 5) == 254_676)
#expect(HOTP(secret: secret).compute(counter: 6) == 287_922)
#expect(HOTP(secret: secret).compute(counter: 7) == 162_583)
#expect(HOTP(secret: secret).compute(counter: 8) == 399_871)
#expect(HOTP(secret: secret).compute(counter: 9) == 520_489)
}

func testTOTP() {
@Test func testTOTP() {
// test against RFC6238 example values https://tools.ietf.org/html/rfc6238#page-15
let secret = "12345678901234567890"

Expand All @@ -46,17 +47,17 @@ final class OTPTests: XCTestCase {
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss'Z'"
dateFormatter.timeZone = TimeZone(secondsFromGMT: 0)

XCTAssertEqual(TOTP(secret: secret, length: 8).compute(date: dateFormatter.date(from: "1970-01-01T00:00:59Z")!), 94_287_082)
XCTAssertEqual(TOTP(secret: secret, length: 8).compute(date: dateFormatter.date(from: "2005-03-18T01:58:29Z")!), 7_081_804)
XCTAssertEqual(TOTP(secret: secret, length: 8).compute(date: dateFormatter.date(from: "2005-03-18T01:58:31Z")!), 14_050_471)
XCTAssertEqual(TOTP(secret: secret, length: 8).compute(date: dateFormatter.date(from: "2009-02-13T23:31:30Z")!), 89_005_924)
XCTAssertEqual(TOTP(secret: secret, length: 8).compute(date: dateFormatter.date(from: "2033-05-18T03:33:20Z")!), 69_279_037)
XCTAssertEqual(TOTP(secret: secret, length: 8).compute(date: dateFormatter.date(from: "2603-10-11T11:33:20Z")!), 65_353_130)
#expect(TOTP(secret: secret, length: 8).compute(date: dateFormatter.date(from: "1970-01-01T00:00:59Z")!) == 94_287_082)
#expect(TOTP(secret: secret, length: 8).compute(date: dateFormatter.date(from: "2005-03-18T01:58:29Z")!) == 7_081_804)
#expect(TOTP(secret: secret, length: 8).compute(date: dateFormatter.date(from: "2005-03-18T01:58:31Z")!) == 14_050_471)
#expect(TOTP(secret: secret, length: 8).compute(date: dateFormatter.date(from: "2009-02-13T23:31:30Z")!) == 89_005_924)
#expect(TOTP(secret: secret, length: 8).compute(date: dateFormatter.date(from: "2033-05-18T03:33:20Z")!) == 69_279_037)
#expect(TOTP(secret: secret, length: 8).compute(date: dateFormatter.date(from: "2603-10-11T11:33:20Z")!) == 65_353_130)
}

func testAuthenticatorURL() {
@Test func testAuthenticatorURL() {
let secret = "HB12345678901234567890"
let url = TOTP(secret: secret, length: 8).createAuthenticatorURL(label: "TOTP test", issuer: "Hummingbird")
XCTAssertEqual(url, "otpauth://totp/TOTP%20test?secret=JBBDCMRTGQ2TMNZYHEYDCMRTGQ2TMNZYHEYA&issuer=Hummingbird&digits=8")
#expect(url == "otpauth://totp/TOTP%20test?secret=JBBDCMRTGQ2TMNZYHEYDCMRTGQ2TMNZYHEYA&issuer=Hummingbird&digits=8")
}
}
Loading