229 lines
6.7 KiB
Swift
229 lines
6.7 KiB
Swift
//
|
|
// APIService+CreateFolderTests.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 APIServiceCreateFolderTests: XCTestCase {
|
|
|
|
// MARK: Properties
|
|
|
|
private let dateFormatter = DateFormatter.isoZulu
|
|
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: Item!
|
|
|
|
// MARK: Setup
|
|
|
|
override func setUp() async throws {
|
|
service = .init(configuration: sessionConfiguration)
|
|
}
|
|
|
|
override func tearDown() async throws {
|
|
service = nil
|
|
}
|
|
|
|
// MARK: Test cases
|
|
|
|
func test_withExistingParentFolderId_someFolderName_andRightCredentials() async throws {
|
|
// GIVEN
|
|
data = "{\"id\":\"058c53609cac8d8388b96792cbc42bea31c73def\",\"parentId\":\"\(itemId)\",\"name\":\"Some new folder name\",\"isDir\":true,\"modificationDate\":\"2022-12-04T20:54:12.513214855Z\"}".data(using: .utf8)
|
|
|
|
MockURLProtocol.mockData[url.appendingPathComponent(itemId)] = MockURLResponse(
|
|
status: .created,
|
|
headers: [.Header.Keys.contentType: .Header.Values.contentTypeJSON],
|
|
data: data
|
|
)
|
|
|
|
// WHEN
|
|
result = try await service.createFolder(
|
|
id: itemId,
|
|
name: "Some new folder name",
|
|
credentials: .init(
|
|
username: "username",
|
|
password: "password"
|
|
)
|
|
)
|
|
|
|
// THEN
|
|
XCTAssertEqual(result, Item(
|
|
idParent: itemId,
|
|
id: "058c53609cac8d8388b96792cbc42bea31c73def",
|
|
name: "Some new folder name",
|
|
isDirectory: true,
|
|
lastModifiedAt: dateFormatter.date(from: "2022-12-04T20:54:12.513214855Z")!,
|
|
size: nil,
|
|
contentType: nil
|
|
))
|
|
}
|
|
|
|
func test_withExistingParentFolderId_someFolderName_andWrongCredentials() async throws {
|
|
// GIVEN
|
|
MockURLProtocol.mockData[url.appendingPathComponent(itemId)] = MockURLResponse(
|
|
status: .unauthorized,
|
|
headers: [:],
|
|
data: nil
|
|
)
|
|
|
|
// WHEN & THEN
|
|
do {
|
|
_ = try await service.createFolder(
|
|
id: itemId,
|
|
name: "Some new folder name",
|
|
credentials: .init(
|
|
username: "usrname",
|
|
password: "passwrd"
|
|
)
|
|
)
|
|
} catch APIClientError.authenticationFailed {
|
|
XCTAssertTrue(true)
|
|
} catch {
|
|
XCTAssertTrue(false)
|
|
}
|
|
}
|
|
|
|
func test_withNonExistingParentFolderId_someFolderName_andRightCredentials() async throws {
|
|
// GIVEN
|
|
MockURLProtocol.mockData[url.appendingPathComponent(itemId)] = MockURLResponse(
|
|
status: .notFound,
|
|
headers: [:],
|
|
data: nil
|
|
)
|
|
|
|
// WHEN & THEN
|
|
do {
|
|
_ = try await service.createFolder(
|
|
id: itemId,
|
|
name: "Some new folder name",
|
|
credentials: .init(
|
|
username: "username",
|
|
password: "password"
|
|
)
|
|
)
|
|
} catch APIClientError.itemDoesNotExist {
|
|
XCTAssertTrue(true)
|
|
} catch {
|
|
XCTAssertTrue(false)
|
|
}
|
|
}
|
|
|
|
func test_withEmptyParentFolderId_someFolderName_andRightCredentials() async throws {
|
|
// GIVEN
|
|
MockURLProtocol.mockData[url.appendingPathComponent("")] = MockURLResponse(
|
|
status: .notFound,
|
|
headers: [:],
|
|
data: nil
|
|
)
|
|
|
|
// WHEN & THEN
|
|
do {
|
|
_ = try await service.createFolder(
|
|
id: "",
|
|
name: "Some new folder name",
|
|
credentials: .init(
|
|
username: "username",
|
|
password: "password"
|
|
)
|
|
)
|
|
} catch APIClientError.itemDoesNotExist {
|
|
XCTAssertTrue(true)
|
|
} catch {
|
|
XCTAssertTrue(false)
|
|
}
|
|
}
|
|
|
|
func test_withExistingParentFolderId_someInvalidFolderName_andRightCredentials() async throws {
|
|
// GIVEN
|
|
MockURLProtocol.mockData[url.appendingPathComponent(itemId)] = MockURLResponse(
|
|
status: .badRequest,
|
|
headers: [:],
|
|
data: nil
|
|
)
|
|
|
|
// WHEN & THEN
|
|
do {
|
|
_ = try await service.createFolder(
|
|
id: itemId,
|
|
name: "./Some:new:folder:name\\",
|
|
credentials: .init(
|
|
username: "username",
|
|
password: "password"
|
|
)
|
|
)
|
|
} catch APIClientError.itemNameInvalidOrDefined {
|
|
XCTAssertTrue(true)
|
|
} catch {
|
|
XCTAssertTrue(false)
|
|
}
|
|
}
|
|
|
|
func test_withExistingParentFolderId_someExistingFolderName_andRightCredentials() async throws {
|
|
// GIVEN
|
|
MockURLProtocol.mockData[url.appendingPathComponent(itemId)] = MockURLResponse(
|
|
status: .badRequest,
|
|
headers: [:],
|
|
data: nil
|
|
)
|
|
|
|
// WHEN & THEN
|
|
do {
|
|
_ = try await service.createFolder(
|
|
id: itemId,
|
|
name: "Some new folder name",
|
|
credentials: .init(
|
|
username: "username",
|
|
password: "password"
|
|
)
|
|
)
|
|
} catch APIClientError.itemNameInvalidOrDefined {
|
|
XCTAssertTrue(true)
|
|
} catch {
|
|
XCTAssertTrue(false)
|
|
}
|
|
}
|
|
|
|
func test_withExistingParentFolderId_someEmptyFolderName_andRightCredentials() async throws {
|
|
// GIVEN
|
|
MockURLProtocol.mockData[url.appendingPathComponent(itemId)] = MockURLResponse(
|
|
status: .badRequest,
|
|
headers: [:],
|
|
data: nil
|
|
)
|
|
|
|
// WHEN & THEN
|
|
do {
|
|
_ = try await service.createFolder(
|
|
id: itemId,
|
|
name: "",
|
|
credentials: .init(
|
|
username: "username",
|
|
password: "password"
|
|
)
|
|
)
|
|
} catch APIClientError.itemNameInvalidOrDefined {
|
|
XCTAssertTrue(true)
|
|
} catch {
|
|
XCTAssertTrue(false)
|
|
}
|
|
}
|
|
|
|
}
|