Skip to content
Merged
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
236 changes: 127 additions & 109 deletions Tests/AppTests/PackageCollectionControllerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,143 +12,161 @@
// See the License for the specific language governing permissions and
// limitations under the License.

import Foundation

@testable import App

import Dependencies
import SnapshotTesting
import XCTVapor
import Testing


class PackageCollectionControllerTests: AppTestCase {
@Suite struct PackageCollectionControllerTests {

func test_owner_request() async throws {
try XCTSkipIf(!isRunningInCI && EnvironmentClient.liveValue.collectionSigningPrivateKey() == nil, "Skip test for local user due to unset COLLECTION_SIGNING_PRIVATE_KEY env variable")
@Test(
.disabled(
if: !isRunningInCI() && EnvironmentClient.liveValue.collectionSigningPrivateKey() == nil,
"Skip test for local user due to unset COLLECTION_SIGNING_PRIVATE_KEY env variable"
)
)
func owner_request() async throws {
try await withDependencies {
$0.date.now = .t0
$0.environment.collectionSigningCertificateChain = EnvironmentClient.liveValue.collectionSigningCertificateChain
$0.environment.collectionSigningPrivateKey = EnvironmentClient.liveValue.collectionSigningPrivateKey
} operation: {
let p = try await savePackage(on: app.db, "https://github.com/foo/1")
do {
let v = try Version(id: UUID(),
package: p,
packageName: "P1-main",
reference: .branch("main"),
toolsVersion: "5.0")
try await v.save(on: app.db)
try await Product(version: v, type: .library(.automatic), name: "P1Lib")
.save(on: app.db)
}
do {
let v = try Version(id: UUID(),
package: p,
latest: .release,
packageName: "P1-tag",
reference: .tag(1, 2, 3),
toolsVersion: "5.1")
try await v.save(on: app.db)
try await Product(version: v, type: .library(.automatic), name: "P1Lib", targets: ["t1"])
.save(on: app.db)
try await Build(version: v,
platform: .iOS,
status: .ok,
swiftVersion: .init(5, 6, 0)).save(on: app.db)
try await Target(version: v, name: "t1").save(on: app.db)
}
try await Repository(package: p,
defaultBranch: "main",
license: .mit,
licenseUrl: "https://foo/mit",
owner: "foo",
summary: "summary 1").create(on: app.db)
try await withApp { app in
let p = try await savePackage(on: app.db, "https://github.com/foo/1")
do {
let v = try Version(id: UUID(),
package: p,
packageName: "P1-main",
reference: .branch("main"),
toolsVersion: "5.0")
try await v.save(on: app.db)
try await Product(version: v, type: .library(.automatic), name: "P1Lib")
.save(on: app.db)
}
do {
let v = try Version(id: UUID(),
package: p,
latest: .release,
packageName: "P1-tag",
reference: .tag(1, 2, 3),
toolsVersion: "5.1")
try await v.save(on: app.db)
try await Product(version: v, type: .library(.automatic), name: "P1Lib", targets: ["t1"])
.save(on: app.db)
try await Build(version: v,
platform: .iOS,
status: .ok,
swiftVersion: .init(5, 6, 0)).save(on: app.db)
try await Target(version: v, name: "t1").save(on: app.db)
}
try await Repository(package: p,
defaultBranch: "main",
license: .mit,
licenseUrl: "https://foo/mit",
owner: "foo",
summary: "summary 1").create(on: app.db)

// MUT
let encoder = self.encoder
try await app.test(
.GET,
"foo/collection.json",
afterResponse: { @MainActor res async throws in
// validation
XCTAssertEqual(res.status, .ok)
let json = try res.content.decode(PackageCollection.self)
assertSnapshot(of: json, as: .json(encoder))
})
// MUT
let encoder = self.encoder
try await app.test(
.GET,
"foo/collection.json",
afterResponse: { @MainActor res async throws in
// validation
#expect(res.status == .ok)
let json = try res.content.decode(PackageCollection.self)
assertSnapshot(of: json, as: .json(encoder))
})
}
}
}

func test_custom_request() async throws {
try XCTSkipIf(!isRunningInCI && EnvironmentClient.liveValue.collectionSigningPrivateKey() == nil, "Skip test for local user due to unset COLLECTION_SIGNING_PRIVATE_KEY env variable")
@Test(
.disabled(
if: !isRunningInCI() && EnvironmentClient.liveValue.collectionSigningPrivateKey() == nil,
"Skip test for local user due to unset COLLECTION_SIGNING_PRIVATE_KEY env variable"
)
)
func custom_request() async throws {
try await withDependencies {
$0.date.now = .t0
$0.environment.collectionSigningCertificateChain = EnvironmentClient.liveValue.collectionSigningCertificateChain
$0.environment.collectionSigningPrivateKey = EnvironmentClient.liveValue.collectionSigningPrivateKey
} operation: {
let p = try await savePackage(on: app.db, "https://github.com/foo/1")
do {
let v = try Version(id: UUID(),
package: p,
packageName: "P1-main",
reference: .branch("main"),
toolsVersion: "5.0")
try await v.save(on: app.db)
try await Product(version: v, type: .library(.automatic), name: "P1Lib")
.save(on: app.db)
}
do {
let v = try Version(id: UUID(),
package: p,
latest: .release,
packageName: "P1-tag",
reference: .tag(1, 2, 3),
toolsVersion: "5.1")
try await v.save(on: app.db)
try await Product(version: v, type: .library(.automatic), name: "P1Lib", targets: ["t1"])
.save(on: app.db)
try await Build(version: v,
platform: .iOS,
status: .ok,
swiftVersion: .init(5, 6, 0)).save(on: app.db)
try await Target(version: v, name: "t1").save(on: app.db)
}
try await Repository(package: p,
defaultBranch: "main",
license: .mit,
licenseUrl: "https://foo/mit",
owner: "foo",
summary: "summary 1").create(on: app.db)
let collection = CustomCollection(id: .id2, .init(key: "custom-collection",
name: "Custom Collection",
url: "https://github.com/foo/bar/list.json"))
try await collection.save(on: app.db)
try await collection.$packages.attach(p, on: app.db)
try await withApp { app in
let p = try await savePackage(on: app.db, "https://github.com/foo/1")
do {
let v = try Version(id: UUID(),
package: p,
packageName: "P1-main",
reference: .branch("main"),
toolsVersion: "5.0")
try await v.save(on: app.db)
try await Product(version: v, type: .library(.automatic), name: "P1Lib")
.save(on: app.db)
}
do {
let v = try Version(id: UUID(),
package: p,
latest: .release,
packageName: "P1-tag",
reference: .tag(1, 2, 3),
toolsVersion: "5.1")
try await v.save(on: app.db)
try await Product(version: v, type: .library(.automatic), name: "P1Lib", targets: ["t1"])
.save(on: app.db)
try await Build(version: v,
platform: .iOS,
status: .ok,
swiftVersion: .init(5, 6, 0)).save(on: app.db)
try await Target(version: v, name: "t1").save(on: app.db)
}
try await Repository(package: p,
defaultBranch: "main",
license: .mit,
licenseUrl: "https://foo/mit",
owner: "foo",
summary: "summary 1").create(on: app.db)
let collection = CustomCollection(id: .id2, .init(key: "custom-collection",
name: "Custom Collection",
url: "https://github.com/foo/bar/list.json"))
try await collection.save(on: app.db)
try await collection.$packages.attach(p, on: app.db)

// MUT
let encoder = self.encoder
try await app.test(
.GET,
"collections/custom-collection/collection.json",
afterResponse: { @MainActor res async throws in
// validation
XCTAssertEqual(res.status, .ok)
let json = try res.content.decode(PackageCollection.self)
assertSnapshot(of: json, as: .json(encoder))
})
// MUT
let encoder = self.encoder
try await app.test(
.GET,
"collections/custom-collection/collection.json",
afterResponse: { @MainActor res async throws in
// validation
#expect(res.status == .ok)
let json = try res.content.decode(PackageCollection.self)
assertSnapshot(of: json, as: .json(encoder))
})
}
}
}

func test_nonexisting_404() throws {
@Test func nonexisting_404() async throws {
// Ensure a request for a non-existing collection returns a 404
try withDependencies {
try await withDependencies {
$0.environment.dbId = { nil }
} operation: {
// MUT
try app.test(
.GET,
"foo/collection.json",
afterResponse: { res in
// validation
XCTAssertEqual(res.status, .notFound)
})
try await withApp { app in
// MUT
try await app.test(
.GET,
"foo/collection.json",
afterResponse: { res async in
// validation
#expect(res.status == .notFound)
})
}
}
}

Expand Down
Loading
Loading