diff --git a/Tests/AppTests/AppTests.swift b/Tests/AppTests/AppTests.swift deleted file mode 100644 index 8cfd310..0000000 --- a/Tests/AppTests/AppTests.swift +++ /dev/null @@ -1,15 +0,0 @@ -@testable import App -import XCTVapor - -final class AppTests: XCTestCase { - func testHelloWorld() async throws { - let app = Application(.testing) - defer { app.shutdown() } - try await configure(app) - - try app.test(.GET, "hello", afterResponse: { res in - XCTAssertEqual(res.status, .ok) - XCTAssertEqual(res.body.string, "Hello, world!") - }) - } -} diff --git a/Tests/AppTests/RoleTest.swift b/Tests/AppTests/RoleTest.swift new file mode 100644 index 0000000..b56ff7f --- /dev/null +++ b/Tests/AppTests/RoleTest.swift @@ -0,0 +1,8 @@ +// +// File.swift +// +// +// Created by Michiel Carman on 4/5/24. +// + +import Foundation diff --git a/Tests/AppTests/UserTests.swift b/Tests/AppTests/UserTests.swift new file mode 100644 index 0000000..8a453b6 --- /dev/null +++ b/Tests/AppTests/UserTests.swift @@ -0,0 +1,80 @@ +@testable import App +import Fluent +import XCTVapor + +final class AppTests: XCTestCase { + + func testUsersIndex() async throws{ + let app = Application(.testing) + defer { app.shutdown() } + try await configure(app) + try app.test(.GET, "api/v2.0/users/", afterResponse: { res in + XCTAssertEqual(res.status, .ok) + XCTAssertEqual(res.headers.contentType, .json) + _ = try res.content.decode([mdlUser].self) + }) + } + + func testUsersRead() async throws{ + let app = Application(.testing) + defer{app.shutdown()} + try await configure(app) + let basepath = "api/v2.0/users/" + let parameter = try CustomCrypto.EncryptString(input: "1") + print(parameter) + let path = "\(basepath)\(parameter)" + try app.test(.GET, path, afterResponse: { res in + XCTAssertEqual(res.status, .ok) + XCTAssertEqual(res.headers.contentType, .json) + _ = try res.content.decode(mdlUser.self) + }) + } + + func testUserLogin() async throws{ + let app = Application(.testing) + defer{app.shutdown()} + try await configure(app) + let api = "api/v2.0/users/login/" + let gooduser = "bryckman" + let goodpwd = "password" + + + let baduser = "brycman_" + let badpwd = "p@$$w0rd" + + var combo = "\(gooduser):\(goodpwd)" + var parameter = try CustomCrypto.EncryptString(input: combo) + var path = "\(api)\(parameter)" + try app.test(.GET, path, afterResponse: { + res in + XCTAssertEqual(res.status, .ok) + XCTAssertEqual(res.headers.contentType, .json) + _ = try res.content.decode(mdlUser.self) + }) + combo = "\(gooduser):\(badpwd)" + parameter = try CustomCrypto.EncryptString(input: combo) + path = "\(api)\(parameter)" + try app.test(.GET, path, afterResponse: { + res in + XCTAssertEqual(res.status, .unauthorized) + XCTAssertEqual(res.headers.contentType, .json) + }) + combo = "\(baduser):\(badpwd)" + parameter = try CustomCrypto.EncryptString(input: combo) + path = "\(api)\(parameter)" + try app.test(.GET, path, afterResponse: { + res in + XCTAssertEqual(res.status, .unauthorized) + XCTAssertEqual(res.headers.contentType, .json) + }) + combo = "\(baduser):\(goodpwd)" + parameter = try CustomCrypto.EncryptString(input: combo) + path = "\(api)\(parameter)" + try app.test(.GET, path, afterResponse: { + res in + XCTAssertEqual(res.status, .unauthorized) + XCTAssertEqual(res.headers.contentType, .json) + }) + } + +}