107 lines
4.9 KiB
Swift
107 lines
4.9 KiB
Swift
import ManagableUsers
|
|
@testable import SampleApp
|
|
import VaporTesting
|
|
import SQLKit
|
|
import Testing
|
|
|
|
@Suite("App Tests", .serialized)
|
|
struct AdminTests {
|
|
private func withApp(_ test: (Application) async throws -> ()) async throws {
|
|
let app = try await Application.make (.testing)
|
|
do {
|
|
try await SampleApp.configure (app)
|
|
let mockDatabase = MockDatabase (eventLoop: app.eventLoopGroup.next())
|
|
app.storage[Application.MockDatabaseKey.self] = mockDatabase
|
|
try await test (app)
|
|
} catch {
|
|
try await app.asyncShutdown()
|
|
throw error
|
|
}
|
|
try await app.asyncShutdown()
|
|
}
|
|
|
|
@Test("Unauthenticated list")
|
|
func unauthenticatedList() async throws {
|
|
try await withApp { app in
|
|
try await app.testing().test(
|
|
.GET,
|
|
"api/admin",
|
|
afterResponse: { res async in
|
|
#expect(res.status == .unauthorized)
|
|
#expect(res.headers["Location"].isEmpty)
|
|
})
|
|
|
|
#expect(app.storage[Application.MockDatabaseKey.self]?.queries.count == 0)
|
|
}
|
|
}
|
|
|
|
@Test("Unauthorized list")
|
|
func unauthorizedList() async throws {
|
|
try await withApp { app in
|
|
var session: String?
|
|
|
|
app.storage[Application.MockDatabaseKey.self]?.results.append ([MockDatabase.Row (values: ["id": UUID(), "email": "gamma", "full_name": "delta", "password": "$2b$12$4bg4BftSpYAHiQsWjjqj2uZlw.LHbSWUsXA4gBL7njnvONYelCNFC", "active": true, "roles": Array<String>()])])
|
|
app.storage[Application.MockDatabaseKey.self]?.results.append ([MockDatabase.Row (values: ["id": UUID(), "email": "gamma", "full_name": "delta", "password": "$2b$12$4bg4BftSpYAHiQsWjjqj2uZlw.LHbSWUsXA4gBL7njnvONYelCNFC", "active": true, "roles": Array<String>()])])
|
|
|
|
try await app.testing().test(
|
|
.POST,
|
|
"api/auth/login",
|
|
beforeRequest: { request async in
|
|
request.headers.contentType = .urlEncodedForm
|
|
request.body = ByteBuffer (string: "email=gamma&password=")
|
|
},
|
|
afterResponse: { res async in
|
|
#expect(res.status == .ok)
|
|
#expect(res.headers.setCookie?["vapor-session"] != nil)
|
|
session = res.headers.setCookie?["vapor-session"]?.string
|
|
})
|
|
|
|
try await app.testing().test(.GET, "api/admin",
|
|
beforeRequest: { request async in
|
|
request.headers.cookie = ["vapor-session": HTTPCookies.Value (string: session ?? "")]
|
|
},
|
|
afterResponse: { res async in
|
|
#expect(res.status == .forbidden)
|
|
})
|
|
|
|
#expect(app.storage[Application.MockDatabaseKey.self]?.queries.count == 2)
|
|
}
|
|
}
|
|
|
|
@Test("Authorized list")
|
|
func authorizedList() async throws {
|
|
try await withApp { app in
|
|
var session: String?
|
|
|
|
app.storage[Application.MockDatabaseKey.self]?.results.append ([MockDatabase.Row (values: ["id": UUID(), "email": "gamma", "full_name": "delta", "password": "$2b$12$4bg4BftSpYAHiQsWjjqj2uZlw.LHbSWUsXA4gBL7njnvONYelCNFC", "active": true, "roles": ["admin"]])])
|
|
app.storage[Application.MockDatabaseKey.self]?.results.append ([MockDatabase.Row (values: ["id": UUID(), "email": "gamma", "full_name": "delta", "password": "$2b$12$4bg4BftSpYAHiQsWjjqj2uZlw.LHbSWUsXA4gBL7njnvONYelCNFC", "active": true, "roles": ["admin"]])])
|
|
app.storage[Application.MockDatabaseKey.self]?.results.append ([MockDatabase.Row (values: ["id": UUID(), "email": "gamma", "full_name": "delta", "active": true, "roles": ["admin"]])])
|
|
|
|
try await app.testing().test(
|
|
.POST,
|
|
"api/auth/login",
|
|
beforeRequest: { request async in
|
|
request.headers.contentType = .urlEncodedForm
|
|
request.body = ByteBuffer (string: "email=gamma&password=")
|
|
},
|
|
afterResponse: { res async in
|
|
#expect(res.status == .ok)
|
|
#expect(res.headers.setCookie?["vapor-session"] != nil)
|
|
session = res.headers.setCookie?["vapor-session"]?.string
|
|
})
|
|
|
|
try await app.testing().test(.GET, "api/admin",
|
|
beforeRequest: { request async in
|
|
request.headers.cookie = ["vapor-session": HTTPCookies.Value (string: session ?? "")]
|
|
},
|
|
afterResponse: { res async in
|
|
#expect(res.status == .ok)
|
|
#expect(res.headers.setCookie?["vapor-session"]?.string == session)
|
|
#expect(res.headers.contentType == .json)
|
|
})
|
|
|
|
#expect(app.storage[Application.MockDatabaseKey.self]?.queries.count == 3)
|
|
}
|
|
}
|
|
}
|