|
| 1 | +import NIO |
| 2 | +import NIOHTTP1 |
| 3 | +import XCTest |
| 4 | + |
| 5 | +@testable import TestRailKit |
| 6 | + |
| 7 | +class UserTests: XCTestCase { |
| 8 | + |
| 9 | + static var utilities = UserUtilities() |
| 10 | + |
| 11 | + override class func tearDown() { |
| 12 | + //XCTAssertNoThrow(try Self.utilities.testServer.stop()) //this is a nio problem and should remain. Omitting for GH Actions only |
| 13 | + XCTAssertNoThrow(try Self.utilities.httpClient.syncShutdown()) |
| 14 | + XCTAssertNoThrow(try Self.utilities.group.syncShutdownGracefully()) |
| 15 | + } |
| 16 | + |
| 17 | + func testGetUser() { |
| 18 | + var requestComplete: EventLoopFuture<User>! |
| 19 | + XCTAssertNoThrow(requestComplete = try Self.utilities.client.action(resource: UserResource.get(type: .one(userId: 1)))) |
| 20 | + |
| 21 | + XCTAssertNoThrow( |
| 22 | + XCTAssertEqual( |
| 23 | + .head( |
| 24 | + .init( |
| 25 | + version: .init(major: 1, minor: 1), |
| 26 | + method: .GET, |
| 27 | + uri: "/index.php?/api/v2/get_user/1", |
| 28 | + headers: .init([ |
| 29 | + ("authorization", "Basic dXNlckB0ZXN0cmFpbC5pbzoxMjM0YWJjZA=="), |
| 30 | + ("content-type", "application/json; charset=utf-8"), |
| 31 | + ("Host", "127.0.0.1:\(Self.utilities.testServer.serverPort)"), |
| 32 | + ("Content-Length", "0"), |
| 33 | + ]))), |
| 34 | + try Self.utilities.testServer.readInbound())) |
| 35 | + |
| 36 | + XCTAssertEqual(try Self.utilities.testServer.readInbound(), .end(nil)) |
| 37 | + |
| 38 | + var responseBuffer = Self.utilities.allocator.buffer(capacity: 0) |
| 39 | + responseBuffer.writeString(Self.utilities.userResponse) |
| 40 | + |
| 41 | + XCTAssertNoThrow( |
| 42 | + try Self.utilities.testServer.writeOutbound(.head(.init(version: .init(major: 1, minor: 1), status: .ok)))) |
| 43 | + XCTAssertNoThrow(try Self.utilities.testServer.writeOutbound(.body(.byteBuffer(responseBuffer)))) |
| 44 | + XCTAssertNoThrow(try Self.utilities.testServer.writeOutbound(.end(nil))) |
| 45 | + |
| 46 | + let response = try! requestComplete.wait() |
| 47 | + XCTAssertEqual(response .email , "[email protected]") |
| 48 | + } |
| 49 | + |
| 50 | + func testGetCurrentUser() { |
| 51 | + var requestComplete: EventLoopFuture<User>! |
| 52 | + XCTAssertNoThrow(requestComplete = try Self.utilities.client.action(resource: UserResource.get(type: .current(userId: 1)))) |
| 53 | + |
| 54 | + XCTAssertNoThrow( |
| 55 | + XCTAssertEqual( |
| 56 | + .head( |
| 57 | + .init( |
| 58 | + version: .init(major: 1, minor: 1), |
| 59 | + method: .GET, |
| 60 | + uri: "/index.php?/api/v2/get_current_user/1", |
| 61 | + headers: .init([ |
| 62 | + ("authorization", "Basic dXNlckB0ZXN0cmFpbC5pbzoxMjM0YWJjZA=="), |
| 63 | + ("content-type", "application/json; charset=utf-8"), |
| 64 | + ("Host", "127.0.0.1:\(Self.utilities.testServer.serverPort)"), |
| 65 | + ("Content-Length", "0"), |
| 66 | + ]))), |
| 67 | + try Self.utilities.testServer.readInbound())) |
| 68 | + |
| 69 | + XCTAssertEqual(try Self.utilities.testServer.readInbound(), .end(nil)) |
| 70 | + |
| 71 | + var responseBuffer = Self.utilities.allocator.buffer(capacity: 0) |
| 72 | + responseBuffer.writeString(Self.utilities.userResponse) |
| 73 | + |
| 74 | + XCTAssertNoThrow( |
| 75 | + try Self.utilities.testServer.writeOutbound(.head(.init(version: .init(major: 1, minor: 1), status: .ok)))) |
| 76 | + XCTAssertNoThrow(try Self.utilities.testServer.writeOutbound(.body(.byteBuffer(responseBuffer)))) |
| 77 | + XCTAssertNoThrow(try Self.utilities.testServer.writeOutbound(.end(nil))) |
| 78 | + |
| 79 | + let response = try! requestComplete.wait() |
| 80 | + XCTAssertEqual(response .email , "[email protected]") |
| 81 | + } |
| 82 | + |
| 83 | + func testGetUserByEmail() { |
| 84 | + var requestComplete: EventLoopFuture<User>! |
| 85 | + XCTAssertNoThrow( |
| 86 | + requestComplete = try Self.utilities .client .action(resource : UserResource.get(type : .email (email : "[email protected]"))) |
| 87 | + ) |
| 88 | + |
| 89 | + XCTAssertNoThrow( |
| 90 | + XCTAssertEqual( |
| 91 | + .head( |
| 92 | + .init( |
| 93 | + version: .init(major: 1, minor: 1), |
| 94 | + method: .GET, |
| 95 | + uri : "/index.php?/api/v2/get_user_by_email&[email protected]", |
| 96 | + headers: .init([ |
| 97 | + ("authorization", "Basic dXNlckB0ZXN0cmFpbC5pbzoxMjM0YWJjZA=="), |
| 98 | + ("content-type", "application/json; charset=utf-8"), |
| 99 | + ("Host", "127.0.0.1:\(Self.utilities.testServer.serverPort)"), |
| 100 | + ("Content-Length", "0"), |
| 101 | + ]))), |
| 102 | + try Self.utilities.testServer.readInbound())) |
| 103 | + |
| 104 | + XCTAssertEqual(try Self.utilities.testServer.readInbound(), .end(nil)) |
| 105 | + |
| 106 | + var responseBuffer = Self.utilities.allocator.buffer(capacity: 0) |
| 107 | + responseBuffer.writeString(Self.utilities.userResponse) |
| 108 | + |
| 109 | + XCTAssertNoThrow( |
| 110 | + try Self.utilities.testServer.writeOutbound(.head(.init(version: .init(major: 1, minor: 1), status: .ok)))) |
| 111 | + XCTAssertNoThrow(try Self.utilities.testServer.writeOutbound(.body(.byteBuffer(responseBuffer)))) |
| 112 | + XCTAssertNoThrow(try Self.utilities.testServer.writeOutbound(.end(nil))) |
| 113 | + |
| 114 | + let response = try! requestComplete.wait() |
| 115 | + XCTAssertEqual(response .email , "[email protected]") |
| 116 | + } |
| 117 | + |
| 118 | + func testGetUsers() { |
| 119 | + var requestComplete: EventLoopFuture<[User]>! |
| 120 | + XCTAssertNoThrow( |
| 121 | + requestComplete = try Self.utilities.client.action(resource: UserResource.get(type: .all(projectId: nil))) |
| 122 | + ) |
| 123 | + |
| 124 | + XCTAssertNoThrow( |
| 125 | + XCTAssertEqual( |
| 126 | + .head( |
| 127 | + .init( |
| 128 | + version: .init(major: 1, minor: 1), |
| 129 | + method: .GET, |
| 130 | + uri: "/index.php?/api/v2/get_users", |
| 131 | + headers: .init([ |
| 132 | + ("authorization", "Basic dXNlckB0ZXN0cmFpbC5pbzoxMjM0YWJjZA=="), |
| 133 | + ("content-type", "application/json; charset=utf-8"), |
| 134 | + ("Host", "127.0.0.1:\(Self.utilities.testServer.serverPort)"), |
| 135 | + ("Content-Length", "0"), |
| 136 | + ]))), |
| 137 | + try Self.utilities.testServer.readInbound())) |
| 138 | + |
| 139 | + XCTAssertEqual(try Self.utilities.testServer.readInbound(), .end(nil)) |
| 140 | + |
| 141 | + var responseBuffer = Self.utilities.allocator.buffer(capacity: 0) |
| 142 | + responseBuffer.writeString(Self.utilities.usersResponse) |
| 143 | + |
| 144 | + XCTAssertNoThrow( |
| 145 | + try Self.utilities.testServer.writeOutbound(.head(.init(version: .init(major: 1, minor: 1), status: .ok)))) |
| 146 | + XCTAssertNoThrow(try Self.utilities.testServer.writeOutbound(.body(.byteBuffer(responseBuffer)))) |
| 147 | + XCTAssertNoThrow(try Self.utilities.testServer.writeOutbound(.end(nil))) |
| 148 | + |
| 149 | + let response = try! requestComplete.wait() |
| 150 | + XCTAssertEqual(response .first ?.email , "[email protected]") |
| 151 | + } |
| 152 | + |
| 153 | + func testGetUsersNonAdmin() { |
| 154 | + var requestComplete: EventLoopFuture<[User]>! |
| 155 | + XCTAssertNoThrow( |
| 156 | + requestComplete = try Self.utilities.client.action(resource: UserResource.get(type: .all(projectId: 1))) |
| 157 | + ) |
| 158 | + |
| 159 | + XCTAssertNoThrow( |
| 160 | + XCTAssertEqual( |
| 161 | + .head( |
| 162 | + .init( |
| 163 | + version: .init(major: 1, minor: 1), |
| 164 | + method: .GET, |
| 165 | + uri: "/index.php?/api/v2/get_users&project_id=1", |
| 166 | + headers: .init([ |
| 167 | + ("authorization", "Basic dXNlckB0ZXN0cmFpbC5pbzoxMjM0YWJjZA=="), |
| 168 | + ("content-type", "application/json; charset=utf-8"), |
| 169 | + ("Host", "127.0.0.1:\(Self.utilities.testServer.serverPort)"), |
| 170 | + ("Content-Length", "0"), |
| 171 | + ]))), |
| 172 | + try Self.utilities.testServer.readInbound())) |
| 173 | + |
| 174 | + XCTAssertEqual(try Self.utilities.testServer.readInbound(), .end(nil)) |
| 175 | + |
| 176 | + var responseBuffer = Self.utilities.allocator.buffer(capacity: 0) |
| 177 | + responseBuffer.writeString(Self.utilities.usersResponse) |
| 178 | + |
| 179 | + XCTAssertNoThrow( |
| 180 | + try Self.utilities.testServer.writeOutbound(.head(.init(version: .init(major: 1, minor: 1), status: .ok)))) |
| 181 | + XCTAssertNoThrow(try Self.utilities.testServer.writeOutbound(.body(.byteBuffer(responseBuffer)))) |
| 182 | + XCTAssertNoThrow(try Self.utilities.testServer.writeOutbound(.end(nil))) |
| 183 | + |
| 184 | + let response = try! requestComplete.wait() |
| 185 | + XCTAssertEqual(response .first ?.email , "[email protected]") |
| 186 | + } |
| 187 | +} |
0 commit comments