Skip to content

Commit 33650d1

Browse files
committed
API tests
1 parent e35b94d commit 33650d1

File tree

3 files changed

+81
-4
lines changed

3 files changed

+81
-4
lines changed

Chapter 16/myProject/Package.swift

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ let package = Package(
77
.macOS(.v12)
88
],
99
products: [
10-
10+
.library(name: "AppApi", targets: ["AppApi"]),
1111
],
1212
dependencies: [
1313
.package(url: "https://github.com/vapor/vapor", from: "4.54.0"),
@@ -19,9 +19,7 @@ let package = Package(
1919
.package(url: "https://github.com/binarybirds/spec", from: "1.2.0"),
2020
],
2121
targets: [
22-
.target(name: "AppApi", dependencies: [
23-
24-
]),
22+
.target(name: "AppApi", dependencies: []),
2523
.target(name: "App", dependencies: [
2624
.product(name: "Vapor", package: "vapor"),
2725
.product(name: "Fluent", package: "fluent"),
@@ -38,6 +36,9 @@ let package = Package(
3836
.target(name: "App"),
3937
.product(name: "XCTVapor", package: "vapor"),
4038
.product(name: "Spec", package: "spec"),
39+
]),
40+
.testTarget(name: "AppApiTests", dependencies: [
41+
.target(name: "AppApi"),
4142
])
4243
]
4344
)

Chapter 16/myProject/Sources/AppApi/Modules/User/UserAccount.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,16 @@ public extension User {
1616

1717
public extension User.Account {
1818

19+
struct Login: Codable {
20+
public let email: String
21+
public let password: String
22+
23+
public init(email: String, password: String) {
24+
self.email = email
25+
self.password = password
26+
}
27+
}
28+
1929
struct List: Codable {
2030
public let id: UUID
2131
public let email: String
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
//
2+
// File.swift
3+
//
4+
//
5+
// Created by Tibor Bodecs on 2022. 01. 09..
6+
//
7+
8+
@testable import AppApi
9+
import XCTest
10+
11+
final class AppApiTests: XCTestCase {
12+
13+
enum HTTPError: Error {
14+
case invalidResponse
15+
case invalidStatusCode(Int)
16+
}
17+
18+
let baseUrl = URL(string: "http://localhost:8080/api/")!
19+
20+
private func authenticate(_ login: User.Account.Login) async throws -> User.Token.Detail {
21+
var req = URLRequest(url: baseUrl.appendingPathComponent("sign-in/"))
22+
req.httpMethod = "POST"
23+
req.addValue("application/json", forHTTPHeaderField: "Content-Type")
24+
req.httpBody = try JSONEncoder().encode(login)
25+
26+
let (data, response) = try await URLSession.shared.data(for: req)
27+
guard let response = response as? HTTPURLResponse else {
28+
throw HTTPError.invalidResponse
29+
}
30+
guard 200...299 ~= response.statusCode else {
31+
throw HTTPError.invalidStatusCode(response.statusCode)
32+
}
33+
return try JSONDecoder().decode(User.Token.Detail.self, from: data)
34+
}
35+
36+
private func authenticateRoot() async throws -> User.Token.Detail {
37+
try await authenticate(.init(email: "root@localhost.com", password: "ChangeMe1"))
38+
}
39+
40+
func testAuthorization() async throws {
41+
let login = User.Account.Login(email: "root@localhost.com", password: "ChangeMe1")
42+
let token = try await authenticate(login)
43+
XCTAssertEqual(token.value.count, 64)
44+
XCTAssertEqual(token.user.email, login.email)
45+
}
46+
47+
func testBlogCategories() async throws {
48+
let token = try await authenticateRoot()
49+
50+
var req = URLRequest(url: baseUrl.appendingPathComponent("\(Blog.pathKey)/\(Blog.Category.pathKey)/"))
51+
req.addValue("Bearer \(token.value)", forHTTPHeaderField: "Authorization")
52+
53+
let (data, response) = try await URLSession.shared.data(for: req)
54+
guard let response = response as? HTTPURLResponse else {
55+
throw HTTPError.invalidResponse
56+
}
57+
print(response.statusCode)
58+
guard 200...299 ~= response.statusCode else {
59+
throw HTTPError.invalidStatusCode(response.statusCode)
60+
}
61+
62+
let categories = try JSONDecoder().decode([Blog.Category.List].self, from: data)
63+
XCTAssertFalse(categories.isEmpty)
64+
}
65+
}
66+

0 commit comments

Comments
 (0)