Skip to content

Commit 9d14c77

Browse files
committed
add API tests
1 parent 07b2013 commit 9d14c77

File tree

5 files changed

+507
-0
lines changed

5 files changed

+507
-0
lines changed
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
//
2+
// APIKeysTests.swift
3+
// AnthropicSwiftSDK
4+
//
5+
// Created by 伊藤史 on 2024/12/07.
6+
//
7+
8+
import XCTest
9+
import AnthropicSwiftSDK_TestUtils
10+
@testable import AnthropicSwiftSDK
11+
12+
final class APIKeysTests: XCTestCase {
13+
var session: URLSession!
14+
var apiKeys: APIKeys!
15+
16+
override func setUp() {
17+
super.setUp()
18+
let configuration = URLSessionConfiguration.default
19+
configuration.protocolClasses = [HTTPMock.self]
20+
session = URLSession(configuration: configuration)
21+
22+
let apiKey = "test-api-key"
23+
apiKeys = APIKeys(adminAPIKey: apiKey, session: session)
24+
}
25+
26+
func testGetAPIKey() async throws {
27+
let expectation = XCTestExpectation(description: "Get API Key response")
28+
29+
HTTPMock.inspectType = .response("""
30+
{
31+
"id": "apikey_01Rj2N8SVvo6BePZj99NhmiT",
32+
"type": "api_key",
33+
"name": "Developer Key",
34+
"workspace_id": "wrkspc_01JwQvzr7rXLA5AGx3HKfFUJ",
35+
"created_at": "2024-10-30T23:58:27.427722Z",
36+
"created_by": {
37+
"id": "user_01WCz1FkmYMm4gnmykNKUu3Q",
38+
"type": "user"
39+
},
40+
"partial_key_hint": "sk-ant-api03-R2D...igAA",
41+
"status": "active"
42+
}
43+
""")
44+
45+
let response = try await apiKeys.get(apiKeyId: "test-key-id")
46+
XCTAssertEqual(response.id, "apikey_01Rj2N8SVvo6BePZj99NhmiT")
47+
XCTAssertEqual(response.type, .apiKey)
48+
XCTAssertEqual(response.name, "Developer Key")
49+
XCTAssertEqual(response.workspaceId, "wrkspc_01JwQvzr7rXLA5AGx3HKfFUJ")
50+
XCTAssertEqual(response.createdAt, "2024-10-30T23:58:27.427722Z")
51+
XCTAssertEqual(response.createdBy.id, "user_01WCz1FkmYMm4gnmykNKUu3Q")
52+
XCTAssertEqual(response.createdBy.type, "user")
53+
XCTAssertEqual(response.partialKeyHint, "sk-ant-api03-R2D...igAA")
54+
XCTAssertEqual(response.status, .active)
55+
56+
expectation.fulfill()
57+
await fulfillment(of: [expectation], timeout: 1.0)
58+
}
59+
60+
func testUpdateAPIKey() async throws {
61+
let expectation = XCTestExpectation(description: "Update API Key response")
62+
63+
HTTPMock.inspectType = .response("""
64+
{
65+
"id": "apikey_01Rj2N8SVvo6BePZj99NhmiT",
66+
"type": "api_key",
67+
"name": "Updated API Key",
68+
"workspace_id": "wrkspc_01JwQvzr7rXLA5AGx3HKfFUJ",
69+
"created_at": "2024-10-30T23:58:27.427722Z",
70+
"created_by": {
71+
"id": "user_01WCz1FkmYMm4gnmykNKUu3Q",
72+
"type": "user"
73+
},
74+
"partial_key_hint": "sk-ant-api03-R2D...igAA",
75+
"status": "inactive"
76+
}
77+
""")
78+
79+
let updatedResponse = try await apiKeys.update(apiKeyId: "test-key-id", name: "Updated API Key", status: .inactive)
80+
XCTAssertEqual(updatedResponse.id, "apikey_01Rj2N8SVvo6BePZj99NhmiT")
81+
XCTAssertEqual(updatedResponse.type, .apiKey)
82+
XCTAssertEqual(updatedResponse.name, "Updated API Key")
83+
XCTAssertEqual(updatedResponse.workspaceId, "wrkspc_01JwQvzr7rXLA5AGx3HKfFUJ")
84+
XCTAssertEqual(updatedResponse.createdAt, "2024-10-30T23:58:27.427722Z")
85+
XCTAssertEqual(updatedResponse.createdBy.id, "user_01WCz1FkmYMm4gnmykNKUu3Q")
86+
XCTAssertEqual(updatedResponse.createdBy.type, "user")
87+
XCTAssertEqual(updatedResponse.partialKeyHint, "sk-ant-api03-R2D...igAA")
88+
XCTAssertEqual(updatedResponse.status, .inactive)
89+
90+
91+
expectation.fulfill()
92+
await fulfillment(of: [expectation], timeout: 1.0)
93+
}
94+
}
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
//
2+
// OrganizationInvitesTests.swift
3+
// AnthropicSwiftSDK
4+
//
5+
// Created by 伊藤史 on 2024/12/07.
6+
//
7+
8+
import XCTest
9+
import AnthropicSwiftSDK_TestUtils
10+
@testable import AnthropicSwiftSDK
11+
12+
final class OrganizationInvitesTests: XCTestCase {
13+
var session: URLSession!
14+
var organizationInvites: OrganizationInvites!
15+
16+
override func setUp() {
17+
super.setUp()
18+
let configuration = URLSessionConfiguration.default
19+
configuration.protocolClasses = [HTTPMock.self]
20+
session = URLSession(configuration: configuration)
21+
22+
let apiKey = "test-api-key"
23+
organizationInvites = OrganizationInvites(adminAPIKey: apiKey, session: session)
24+
}
25+
26+
func testGetOrganizationInvite() async throws {
27+
let expectation = XCTestExpectation(description: "Get Organization Invite response")
28+
29+
HTTPMock.inspectType = .response("""
30+
{
31+
"id": "invite_015gWxCN9Hfg2QhZwTK7Mdeu",
32+
"type": "invite",
33+
"email": "user@emaildomain.com",
34+
"role": "user",
35+
"invited_at": "2024-10-30T23:58:27.427722Z",
36+
"expires_at": "2024-11-20T23:58:27.427722Z",
37+
"status": "pending"
38+
}
39+
""")
40+
41+
let response = try await organizationInvites.get(invitationId: "invite_015gWxCN9Hfg2QhZwTK7Mdeu")
42+
XCTAssertEqual(response.id, "invite_015gWxCN9Hfg2QhZwTK7Mdeu")
43+
XCTAssertEqual(response.type, .invite)
44+
XCTAssertEqual(response.email, "user@emaildomain.com")
45+
XCTAssertEqual(response.role, .user)
46+
XCTAssertEqual(response.invitedAt, "2024-10-30T23:58:27.427722Z")
47+
XCTAssertEqual(response.expiresAt, "2024-11-20T23:58:27.427722Z")
48+
XCTAssertEqual(response.status, .pending)
49+
50+
expectation.fulfill()
51+
await fulfillment(of: [expectation], timeout: 1.0)
52+
}
53+
54+
func testRemoveOrganizationInvite() async throws {
55+
let expectation = XCTestExpectation(description: "Remove Organization Invite response")
56+
57+
HTTPMock.inspectType = .response("""
58+
{
59+
"id": "invite_015gWxCN9Hfg2QhZwTK7Mdeu",
60+
"type": "invite_deleted"
61+
}
62+
""")
63+
64+
let response = try await organizationInvites.remove(invitationId: "invite_015gWxCN9Hfg2QhZwTK7Mdeu")
65+
XCTAssertEqual(response.id, "invite_015gWxCN9Hfg2QhZwTK7Mdeu")
66+
XCTAssertEqual(response.type, .deleted)
67+
68+
expectation.fulfill()
69+
await fulfillment(of: [expectation], timeout: 1.0)
70+
}
71+
72+
func testSendOrganizationInvite() async throws {
73+
let expectation = XCTestExpectation(description: "Send Organization Invite response")
74+
75+
HTTPMock.inspectType = .response("""
76+
{
77+
"id": "invite_015gWxCN9Hfg2QhZwTK7Mdeu",
78+
"type": "invite",
79+
"email": "user@emaildomain.com",
80+
"role": "user",
81+
"invited_at": "2024-10-30T23:58:27.427722Z",
82+
"expires_at": "2024-11-20T23:58:27.427722Z",
83+
"status": "pending"
84+
}
85+
""")
86+
87+
let invite = Invitation(email: "user@emaildomain.com", role: .user)
88+
let response = try await organizationInvites.send(invitation: invite)
89+
XCTAssertEqual(response.id, "invite_015gWxCN9Hfg2QhZwTK7Mdeu")
90+
XCTAssertEqual(response.type, .invite)
91+
XCTAssertEqual(response.email, "user@emaildomain.com")
92+
XCTAssertEqual(response.role, .user)
93+
XCTAssertEqual(response.invitedAt, "2024-10-30T23:58:27.427722Z")
94+
XCTAssertEqual(response.expiresAt, "2024-11-20T23:58:27.427722Z")
95+
XCTAssertEqual(response.status, .pending)
96+
97+
expectation.fulfill()
98+
await fulfillment(of: [expectation], timeout: 1.0)
99+
}
100+
}
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
//
2+
// OrganizationMembersTests.swift
3+
// AnthropicSwiftSDK
4+
//
5+
// Created by 伊藤史 on 2024/12/07.
6+
//
7+
8+
import XCTest
9+
import AnthropicSwiftSDK_TestUtils
10+
@testable import AnthropicSwiftSDK
11+
12+
final class OrganizationMembersTests: XCTestCase {
13+
var session: URLSession!
14+
var organizationMembers: OrganizationMembers!
15+
16+
override func setUp() {
17+
super.setUp()
18+
let configuration = URLSessionConfiguration.default
19+
configuration.protocolClasses = [HTTPMock.self]
20+
session = URLSession(configuration: configuration)
21+
22+
let apiKey = "test-api-key"
23+
organizationMembers = OrganizationMembers(adminAPIKey: apiKey, session: session)
24+
}
25+
26+
func testGetOrganizationMember() async throws {
27+
let expectation = XCTestExpectation(description: "Get Organization Member response")
28+
29+
HTTPMock.inspectType = .response("""
30+
{
31+
"id": "user_01WCz1FkmYMm4gnmykNKUu3Q",
32+
"type": "user",
33+
"email": "user@emaildomain.com",
34+
"name": "Jane Doe",
35+
"role": "user",
36+
"added_at": "2024-10-30T23:58:27.427722Z"
37+
}
38+
""")
39+
40+
let response = try await organizationMembers.get(userId: "user_01WCz1FkmYMm4gnmykNKUu3Q")
41+
XCTAssertEqual(response.id, "user_01WCz1FkmYMm4gnmykNKUu3Q")
42+
XCTAssertEqual(response.type, .user)
43+
XCTAssertEqual(response.email, "user@emaildomain.com")
44+
XCTAssertEqual(response.name, "Jane Doe")
45+
XCTAssertEqual(response.role, .user)
46+
XCTAssertEqual(response.addedAt, "2024-10-30T23:58:27.427722Z")
47+
48+
expectation.fulfill()
49+
await fulfillment(of: [expectation], timeout: 1.0)
50+
}
51+
52+
func testRemoveOrganizationMember() async throws {
53+
let expectation = XCTestExpectation(description: "Remove Organization Member response")
54+
55+
HTTPMock.inspectType = .response("""
56+
{
57+
"id": "user_01WCz1FkmYMm4gnmykNKUu3Q",
58+
"type": "user_deleted"
59+
}
60+
""")
61+
62+
let response = try await organizationMembers.remove(userId: "user_01WCz1FkmYMm4gnmykNKUu3Q")
63+
XCTAssertEqual(response.id, "user_01WCz1FkmYMm4gnmykNKUu3Q")
64+
XCTAssertEqual(response.type, .deleted)
65+
66+
expectation.fulfill()
67+
await fulfillment(of: [expectation], timeout: 1.0)
68+
}
69+
70+
func testUpdateOrganizationMember() async throws {
71+
let expectation = XCTestExpectation(description: "Update Organization Member response")
72+
73+
HTTPMock.inspectType = .response("""
74+
{
75+
"id": "user_01WCz1FkmYMm4gnmykNKUu3Q",
76+
"type": "user",
77+
"email": "updated_user@emaildomain.com",
78+
"name": "Updated Jane Doe",
79+
"role": "admin",
80+
"added_at": "2024-10-30T23:58:27.427722Z"
81+
}
82+
""")
83+
84+
let response = try await organizationMembers.update(userId: "user_01WCz1FkmYMm4gnmykNKUu3Q", role: .admin)
85+
XCTAssertEqual(response.id, "user_01WCz1FkmYMm4gnmykNKUu3Q")
86+
XCTAssertEqual(response.type, .user)
87+
XCTAssertEqual(response.email, "updated_user@emaildomain.com")
88+
XCTAssertEqual(response.name, "Updated Jane Doe")
89+
XCTAssertEqual(response.role, .admin)
90+
XCTAssertEqual(response.addedAt, "2024-10-30T23:58:27.427722Z")
91+
92+
expectation.fulfill()
93+
await fulfillment(of: [expectation], timeout: 1.0)
94+
}
95+
}
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
//
2+
// WorkspaceMembersTests.swift
3+
// AnthropicSwiftSDK
4+
//
5+
// Created by 伊藤史 on 2024/12/07.
6+
//
7+
8+
import XCTest
9+
import AnthropicSwiftSDK_TestUtils
10+
@testable import AnthropicSwiftSDK
11+
12+
final class WorkspaceMembersTests: XCTestCase {
13+
var session: URLSession!
14+
var workspaceMembers: WorkspaceMembers!
15+
16+
override func setUp() {
17+
super.setUp()
18+
let configuration = URLSessionConfiguration.default
19+
configuration.protocolClasses = [HTTPMock.self]
20+
session = URLSession(configuration: configuration)
21+
22+
let apiKey = "test-api-key"
23+
workspaceMembers = WorkspaceMembers(adminAPIKey: apiKey, session: session)
24+
}
25+
26+
func testGetWorkspaceMember() async throws {
27+
let expectation = XCTestExpectation(description: "Get Workspace Member response")
28+
29+
HTTPMock.inspectType = .response("""
30+
{
31+
"type": "workspace_member",
32+
"user_id": "user_01WCz1FkmYMm4gnmykNKUu3Q",
33+
"workspace_id": "wrkspc_01JwQvzr7rXLA5AGx3HKfFUJ",
34+
"workspace_role": "workspace_user"
35+
}
36+
""")
37+
38+
let response = try await workspaceMembers.get(userId: "user_01WCz1FkmYMm4gnmykNKUu3Q", workspaceId: "wrkspc_01JwQvzr7rXLA5AGx3HKfFUJ")
39+
XCTAssertEqual(response.userId, "user_01WCz1FkmYMm4gnmykNKUu3Q")
40+
XCTAssertEqual(response.workspaceId, "wrkspc_01JwQvzr7rXLA5AGx3HKfFUJ")
41+
XCTAssertEqual(response.workspaceRole, .user)
42+
XCTAssertEqual(response.type, .workspaceMember)
43+
44+
expectation.fulfill()
45+
await fulfillment(of: [expectation], timeout: 1.0)
46+
}
47+
48+
func testRemoveWorkspaceMember() async throws {
49+
let expectation = XCTestExpectation(description: "Remove Workspace Member response")
50+
51+
HTTPMock.inspectType = .response("""
52+
{
53+
"user_id": "user_01WCz1FkmYMm4gnmykNKUu3Q",
54+
"workspace_id": "wrkspc_01JwQvzr7rXLA5AGx3HKfFUJ",
55+
"type": "workspace_member_deleted"
56+
}
57+
""")
58+
59+
let response = try await workspaceMembers.remove(userId: "user_01WCz1FkmYMm4gnmykNKUu3Q", workspaceId: "wrkspc_01JwQvzr7rXLA5AGx3HKfFUJ")
60+
XCTAssertEqual(response.userId, "user_01WCz1FkmYMm4gnmykNKUu3Q")
61+
XCTAssertEqual(response.workspaceId, "wrkspc_01JwQvzr7rXLA5AGx3HKfFUJ")
62+
XCTAssertEqual(response.type, .deleted)
63+
64+
expectation.fulfill()
65+
await fulfillment(of: [expectation], timeout: 1.0)
66+
}
67+
68+
func testUpdateWorkspaceMember() async throws {
69+
let expectation = XCTestExpectation(description: "Update Workspace Member response")
70+
71+
HTTPMock.inspectType = .response("""
72+
{
73+
"type": "workspace_member",
74+
"user_id": "user_01WCz1FkmYMm4gnmykNKUu3Q",
75+
"workspace_id": "wrkspc_01JwQvzr7rXLA5AGx3HKfFUJ",
76+
"workspace_role": "workspace_admin"
77+
}
78+
""")
79+
80+
let response = try await workspaceMembers.update(userId: "user_01WCz1FkmYMm4gnmykNKUu3Q", workspaceId: "wrkspc_01JwQvzr7rXLA5AGx3HKfFUJ", role: .admin)
81+
XCTAssertEqual(response.userId, "user_01WCz1FkmYMm4gnmykNKUu3Q")
82+
XCTAssertEqual(response.workspaceId, "wrkspc_01JwQvzr7rXLA5AGx3HKfFUJ")
83+
XCTAssertEqual(response.workspaceRole, .admin)
84+
XCTAssertEqual(response.type, .workspaceMember)
85+
86+
expectation.fulfill()
87+
await fulfillment(of: [expectation], timeout: 1.0)
88+
}
89+
}

0 commit comments

Comments
 (0)