-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathWebhookTests.swift
169 lines (147 loc) · 6.74 KB
/
WebhookTests.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
// Copyright 2016-2018 Cisco Systems Inc
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
import Foundation
import XCTest
@testable import SparkSDK
class WebhookTests: XCTestCase {
private var fixture: SparkTestFixture! = SparkTestFixture.sharedInstance
private var webhooks: WebhookClient!
private var roomId: String!
private var webhook: Webhook!
private var webhookId: String!
override func setUp() {
super.setUp()
continueAfterFailure = false
XCTAssertNotNil(fixture)
let room = fixture.createRoom(testCase: self, title: "room_for_test")
roomId = room?.id
XCTAssertNotNil(roomId)
webhooks = fixture.spark.webhooks
webhook = createWebhook()
XCTAssertNotNil(webhook)
webhookId = webhook.id
XCTAssertNotNil(webhookId)
}
override func tearDown() {
if let roomId = roomId {
fixture.deleteRoom(testCase: self, roomId: roomId)
}
if let webhook = webhook, let id = webhook.id {
deleteWebhook(id: id)
}
super.tearDown()
}
override static func tearDown() {
Thread.sleep(forTimeInterval: Config.TestcaseInterval)
super.tearDown()
}
func testWhenCreateWebhookThenItExistsAndHasAnId() {
// Tests that basic setup and teardown work without issue.
}
// func testCreateFilterWebhookThenItExistsAndHasAnId() {
// let filterWebhook = createFilterWebhook()
// XCTAssertNotNil(filterWebhook)
//
// let webhooks = listWebhooks()
// XCTAssertEqual(webhooks?.count, 2)
// deleteWebhook(id: filterWebhook!.id!)
// let webhooks1 = listWebhooks()
// XCTAssertEqual(webhooks1?.count, 1)
// }
func testWhenThereIsOneWebhookThenTheListFunctionReturnsOneWebhook() {
let webhooks = listWebhooks()
XCTAssertEqual(webhooks?.count, 1)
}
func testWhenGetWebhookByIDThenReturnedWebhookHasSameID() {
let gottenWebhook = getWebhook(id: webhookId)
XCTAssertEqual(gottenWebhook?.id, webhookId)
}
func testWhenWebhookNameIsUpdatedThenReturnedInfoMatches() {
let updatedName = "updated_test_webhook"
let updatedTargetUrl = "https://example.com/updated_test_webhook"
let updated = updateWebhook(id: webhookId, name: updatedName, targetUrl: updatedTargetUrl)
XCTAssertEqual(updated?.name, updatedName)
XCTAssertEqual(updated?.targetUrl, updatedTargetUrl)
}
func testWhenWebhookIsDeletedThenItCannotBeUpdated() {
deleteWebhook(id: webhookId)
let updatedName = "updated_test_webhook"
let updatedTargetUrl = "https://example.com/updated_test_webhook"
let updated = updateWebhook(id: webhookId, name: updatedName, targetUrl: updatedTargetUrl)
XCTAssertNil(updated)
}
func testWhenWebhookIsDeletedThenItCannotBeGotten() {
deleteWebhook(id: webhookId)
XCTAssertNil(getWebhook(id: webhookId))
}
func testWhenWebhookIsDeletedThenItCannotBeListed() {
deleteWebhook(id: webhookId)
let webHooks = listWebhooks()
XCTAssertEqual(webHooks?.count, 0)
}
// func testCreateSameWebhookThenDeleteIt() {
// let webhook1 = createWebhook()
// XCTAssertNotNil(webhook1)
//
// let webhooks = listWebhooks()
// XCTAssertEqual(webhooks?.count, 2)
// deleteWebhook(id: webhook1!.id!)
// let webhooks1 = listWebhooks()
// XCTAssertEqual(webhooks1?.count, 1)
// XCTAssertEqual(webhooks1?.filter(){$0.id == webhook1!.id!}.count, 0)
//
// }
private func createWebhook() -> Webhook? {
let request = { (completionHandler: @escaping (ServiceResponse<Webhook>) -> Void) in
self.webhooks.create(name: "test_webhook", targetUrl: "https://example.com/test_webhook", resource: "messages", event: "created", filter: nil, completionHandler: completionHandler)
}
return fixture.getResponse(testCase: self, request: request)
}
private func createFilterWebhook() -> Webhook? {
let request = { (completionHandler: @escaping (ServiceResponse<Webhook>) -> Void) in
self.webhooks.create(name: "test_webhook", targetUrl: "https://example.com/test_webhook", resource: "messages", event: "created", filter: "roomId=" + self.roomId, completionHandler: completionHandler)
}
return fixture.getResponse(testCase: self, request: request)
}
private func deleteWebhook(id: String) {
let request = { (completionHandler: @escaping (ServiceResponse<Any>) -> Void) in
self.webhooks.delete(webhookId: id, completionHandler: completionHandler)
}
_ = fixture.getResponse(testCase: self, request: request)
}
private func listWebhooks() -> [Webhook]? {
let request = { (completionHandler: @escaping (ServiceResponse<[Webhook]>) -> Void) in
self.webhooks.list(completionHandler: completionHandler)
}
return fixture.getResponse(testCase: self, request: request)
}
private func getWebhook(id: String) -> Webhook? {
let request = { (completionHandler: @escaping (ServiceResponse<Webhook>) -> Void) in
self.webhooks.get(webhookId: id, completionHandler: completionHandler)
}
return fixture.getResponse(testCase: self, request: request)
}
private func updateWebhook(id: String, name: String, targetUrl: String) -> Webhook? {
let request = { (completionHandler: @escaping (ServiceResponse<Webhook>) -> Void) in
self.webhooks.update(webhookId: id, name: name, targetUrl: targetUrl, completionHandler: completionHandler)
}
return fixture.getResponse(testCase: self, request: request)
}
}