my-files-sample/Libraries/Tests/APIServiceTests/Cases/Actors/APIService+DeleteItemTests.swift

137 lines
3.4 KiB
Swift
Raw Normal View History

2022-12-04 23:04:22 +01:00
//
// APIService+DeleteItemTests.swift
// APIServiceTests
//
// Created by Javier Cicchelli on 04/12/2022.
// Copyright © 2022 Röck+Cöde. All rights reserved.
//
import Foundation
import XCTest
@testable import APIService
final class APIServiceDeleteItemTests: XCTestCase {
// MARK: Properties
private let sessionConfiguration = {
let configuration = URLSessionConfiguration.default
configuration.protocolClasses = [MockURLProtocol.self]
return configuration
}()
private let itemId = UUID().uuidString
private let url = URL(string: "http://163.172.147.216:8080/items/")!
private var service: APIService!
// MARK: Setup
override func setUp() async throws {
service = .init(configuration: sessionConfiguration)
}
override func tearDown() async throws {
service = nil
}
// MARK: Test cases
func test_withExistingId_andRightCredentials() async throws {
// GIVEN
MockURLProtocol.mockData[url.appendingPathComponent(itemId)] = MockURLResponse(
status: .noContent,
headers: [:],
data: nil
)
// WHEN
try await service.deleteItem(
id: itemId,
credentials: .init(
username: "username",
password: "password"
)
)
// THEN
XCTAssertTrue(true)
}
func test_withExistingId_andWrongCredentials() async throws {
// GIVEN
MockURLProtocol.mockData[url.appendingPathComponent(itemId)] = MockURLResponse(
status: .unauthorized,
headers: [:],
data: nil
)
// WHEN & THEN
do {
try await service.deleteItem(
id: itemId,
credentials: .init(
username: "usrname",
password: "passwrd"
)
)
} catch APIClientError.authenticationFailed {
XCTAssertTrue(true)
} catch {
XCTAssertTrue(false)
}
}
func test_withNonExistingId_andRightCredentials() async throws {
// GIVEN
MockURLProtocol.mockData[url.appendingPathComponent(itemId)] = MockURLResponse(
status: .notFound,
headers: [:],
data: nil
)
// WHEN & THEN
do {
try await service.deleteItem(
id: itemId,
credentials: .init(
username: "username",
password: "password"
)
)
} catch APIClientError.itemDoesNotExist {
XCTAssertTrue(true)
} catch {
XCTAssertTrue(false)
}
}
func test_withEmptyId_andRightCredentials() async throws {
// GIVEN
MockURLProtocol.mockData[url.appendingPathComponent("")] = MockURLResponse(
status: .notFound,
headers: [:],
data: nil
)
// WHEN & THEN
do {
try await service.deleteItem(
id: "",
credentials: .init(
username: "username",
password: "password"
)
)
} catch APIClientError.itemDoesNotExist {
XCTAssertTrue(true)
} catch {
XCTAssertTrue(false)
}
}
}