-
Notifications
You must be signed in to change notification settings - Fork 13
/
CamillinkTests.swift
101 lines (82 loc) · 4.22 KB
/
CamillinkTests.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
import ChameleonKit
import ChameleonTestKit
@testable import SlackBotKit
import XCTest
class CamillinkTests: XCTestCase {
func testLinkTracking() throws {
let test = try SlackBot.test()
let storage = MemoryStorage()
var config: SlackBot.Camillink.Config = .default()
var date = Date()
config.dateFactory = { date }
_ = test.bot.enableCamillink(config: config, storage: storage)
// sanity check
try XCTAssertEqual(storage.keys(in: SlackBot.Camillink.Keys.namespace), [])
XCTAssertClear(test)
// messages without links aren't triggering
try test.send(.event(.message([.text("hello world")])))
try XCTAssertEqual(storage.keys(in: SlackBot.Camillink.Keys.namespace), [])
XCTAssertClear(test)
// initial link
try test.enqueue([.permalink(channelId: "C0000000000", url: URL("https://www.slack.com/permalink/channel1/link1"))])
try test.send(.event(.messageWithLink1()))
try XCTAssertEqual(storage.keys(in: SlackBot.Camillink.Keys.namespace), ["https://twitter.com/IanKay"])
XCTAssertClear(test)
try test.send(.event(.unfurlLink1()))
XCTAssertClear(test)
// same link, same channel, 1 second later
date.addTimeInterval(1)
try test.enqueue([.emptyMessage()])
try test.send(.event(.messageWithLink1()))
try XCTAssertEqual(storage.keys(in: SlackBot.Camillink.Keys.namespace), ["https://twitter.com/IanKay"])
XCTAssertClear(test)
// same link, thread
try test.enqueue([.emptyMessage()])
try test.send(.event(.threadedMessageWithLink1()))
XCTAssertClear(test)
// delete link from thread
try test.send(.event(.deleteThreadedMessageWithLink1()))
XCTAssertClear(test)
// same link, different channel, 1 second later
date.addTimeInterval(1)
try test.enqueue([.emptyMessage()])
try test.send(.event(.message([.link(url: URL("https://twitter.com/IanKay"))])))
try XCTAssertEqual(storage.keys(in: SlackBot.Camillink.Keys.namespace), ["https://twitter.com/IanKay"])
XCTAssertClear(test)
// same link, ensuring that an allowlisted domain does not trigger Camille, 1 second later
date.addTimeInterval(1)
try test.send(.event(.message([.link(url: URL("https://apple.com/"))])))
try XCTAssertEqual(storage.keys(in: SlackBot.Camillink.Keys.namespace), ["https://twitter.com/IanKay"])
XCTAssertClear(test)
// same link, ensuring a denylisted query parameter is stripped, 1 second later
date.addTimeInterval(1)
try test.enqueue([.emptyMessage()])
try test.send(.event(.message([.link(url: URL("https://twitter.com/IanKay?s=20"))])))
try XCTAssertEqual(storage.keys(in: SlackBot.Camillink.Keys.namespace), ["https://twitter.com/IanKay"])
XCTAssertClear(test)
// same link, different channel, after expiration
let day = 60 * 60 * 24
date.addTimeInterval(TimeInterval(day * (config.recencyLimitInDays! + 1)))
try test.send(.event(.message([.link(url: URL("https://twitter.com/IanKay"))])))
try XCTAssertEqual(storage.keys(in: SlackBot.Camillink.Keys.namespace), [])
XCTAssertClear(test)
}
func testLinkTracking_EdgeCases_LinksInCodeBlocks() throws {
let test = try SlackBot.test()
let storage = MemoryStorage()
_ = test.bot.enableCamillink(config: .default(), storage: storage)
try test.send(.event(.messageWithPreformattedLink()))
try XCTAssertEqual(storage.keys(in: SlackBot.Camillink.Keys.namespace), [])
XCTAssertClear(test)
}
func testLinkTracking_EdgeCases_DuplicateLink() throws {
let test = try SlackBot.test()
let storage = MemoryStorage()
_ = test.bot.enableCamillink(config: .default(), storage: storage)
// only 1 permalink should be requested
try test.enqueue([.permalink(channelId: "C0000000000", url: URL("https://www.slack.com/permalink/channel1/link1"))])
try test.send(.event(.messageWithDuplicateLink()))
try XCTAssertEqual(storage.keys(in: SlackBot.Camillink.Keys.namespace), ["https://twitter.com/IanKay"])
XCTAssertClear(test)
}
}