From 1f4878219fe346c0e81f5e23ffc67cbe2e93fdc7 Mon Sep 17 00:00:00 2001 From: rpm-mcarman <65228808+rpm-mcarman@users.noreply.github.com> Date: Fri, 5 Apr 2024 22:23:58 -0400 Subject: [PATCH] Fixed all service endpoints with better error control. Changed default query for reading parameters to get rid of non use warning. --- Tests/AppTests/AppTests.swift | 15 ------- Tests/AppTests/RoleTest.swift | 8 ++++ Tests/AppTests/UserTests.swift | 80 ++++++++++++++++++++++++++++++++++ 3 files changed, 88 insertions(+), 15 deletions(-) delete mode 100644 Tests/AppTests/AppTests.swift create mode 100644 Tests/AppTests/RoleTest.swift create mode 100644 Tests/AppTests/UserTests.swift 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) + }) + } + +}