// // APIService+GetDataTests.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 APIServiceGetDataTests: 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! private var data: Data! private var result: Data! // MARK: Setup override func setUp() async throws { service = .init(configuration: sessionConfiguration) } override func tearDown() async throws { service = nil } // MARK: Test cases func test_withExistingFileId_andRightCredentials() async throws { // GIVEN data = "This is just a simple text for testing purposes.".data(using: .utf8) MockURLProtocol.mockData[url.appendingPathComponent(itemId + "/data")] = MockURLResponse( status: .ok, headers: [.Header.Keys.contentType: .Header.Values.contentTypeOctet], data: data ) // WHEN result = try await service.getData( id: itemId, credentials: .init( username: "username", password: "password" ) ) // THEN XCTAssertEqual(result, data) } func test_withExistingFileId_andWrongCredentials() async throws { // GIVEN MockURLProtocol.mockData[url.appendingPathComponent(itemId + "/data")] = MockURLResponse( status: .unauthorized, headers: [:], data: nil ) // WHEN & THEN do { _ = try await service.getData( id: itemId, credentials: .init( username: "usrname", password: "passwrd" ) ) } catch APIClientError.authenticationFailed { XCTAssertTrue(true) } catch { XCTAssertTrue(false) } } func test_withNonExistingFileId_andRightCredentials() async throws { // GIVEN MockURLProtocol.mockData[url.appendingPathComponent(itemId + "/data")] = MockURLResponse( status: .notFound, headers: [:], data: nil ) // WHEN & THEN do { _ = try await service.getData( id: itemId, credentials: .init( username: "username", password: "password" ) ) } catch APIClientError.itemDoesNotExist { XCTAssertTrue(true) } catch { XCTAssertTrue(false) } } func test_withEmptyFileId_andRightCredentials() async throws { // GIVEN MockURLProtocol.mockData[url.appendingPathComponent("/data")] = MockURLResponse( status: .badRequest, headers: [:], data: nil ) // WHEN & THEN do { _ = try await service.getData( id: "", credentials: .init( username: "username", password: "password" ) ) } catch APIClientError.itemIsNotFile { XCTAssertTrue(true) } catch { XCTAssertTrue(false) } } func test_withSomeFolderId_andRightCredentials() async throws { // GIVEN MockURLProtocol.mockData[url.appendingPathComponent(itemId + "/data")] = MockURLResponse( status: .badRequest, headers: [:], data: nil ) // WHEN & THEN do { _ = try await service.getData( id: itemId, credentials: .init( username: "username", password: "password" ) ) } catch APIClientError.itemIsNotFile { XCTAssertTrue(true) } catch { XCTAssertTrue(false) } } }