-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Copy pathPushAuthenticationManagerTests.swift
134 lines (101 loc) · 6.52 KB
/
PushAuthenticationManagerTests.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
import Foundation
import XCTest
@testable import WordPress
import WordPressShared
class PushAuthenticationManagerTests: XCTestCase {
class MockUIAlertControllerProxy: UIAlertControllerProxy {
var titlePassedIn: String?
var messagePassedIn: String?
var cancelButtonTitlePassedIn: String?
var otherButtonTitlesPassedIn: [AnyObject]?
var tapBlockPassedIn: UIAlertControllerCompletionBlock?
var showWithTitleCalled = false
override func show(withTitle title: String?, message: String?, cancelButtonTitle: String?, otherButtonTitles: [Any]?, tap tapBlock: UIAlertControllerCompletionBlock?) -> UIAlertController {
showWithTitleCalled = true
titlePassedIn = title
messagePassedIn = message
cancelButtonTitlePassedIn = cancelButtonTitle
otherButtonTitlesPassedIn = otherButtonTitles as [AnyObject]?
tapBlockPassedIn = tapBlock
return UIAlertController()
}
}
class MockPushAuthenticationService: PushAuthenticationService {
var tokenPassedIn: String?
var completionBlockPassedIn: ((Bool) -> ())?
var authorizedLoginCalled = false
var numberOfTimesAuthorizedLoginCalled = 0
override func authorizeLogin(_ token: String, completion: @escaping ((Bool) -> ())) {
authorizedLoginCalled = true
numberOfTimesAuthorizedLoginCalled += 1
tokenPassedIn = token
completionBlockPassedIn = completion
}
}
var mockPushAuthenticationService = MockPushAuthenticationService(managedObjectContext: TestContextManager().mainContext)
var mockAlertControllerProxy = MockUIAlertControllerProxy()
var pushAuthenticationManager: PushAuthenticationManager?
var approvalAlertController: UIAlertController!
override func setUp() {
super.setUp()
pushAuthenticationManager = PushAuthenticationManager(pushAuthenticationService: mockPushAuthenticationService)
pushAuthenticationManager?.alertControllerProxy = mockAlertControllerProxy
approvalAlertController = UIAlertController()
approvalAlertController.addCancelActionWithTitle("Ignore", handler: nil)
approvalAlertController.addDefaultActionWithTitle("Approve", handler: nil)
}
func testIsPushAuthenticationNotificationReturnsTrueWhenPassedTheCorrectPushAuthenticationNoteType() {
let result = pushAuthenticationManager!.isAuthenticationNotification(["type": "push_auth"])
XCTAssertTrue(result, "Should be true when the type is 'push_auth'")
}
func testIsPushAuthenticationNotificationReturnsFalseWhenPassedIncorrectPushAuthenticationNoteType() {
let result = pushAuthenticationManager!.isAuthenticationNotification(["type": "not_push"])
XCTAssertFalse(result, "Should be false when the type is not 'push_auth'")
}
var expiredAuthenticationDictionary: NSDictionary {
return ["expires": TimeInterval(3)]
}
var validAuthenticationDictionary: NSMutableDictionary {
return ["push_auth_token": "token", "aps": [ "alert": "an alert"]]
}
func testHandlePushAuthenticationNotificationShowsTheLoginExpiredAlertIfNotificationHasExpired() {
pushAuthenticationManager!.handleAuthenticationNotification(expiredAuthenticationDictionary)
XCTAssertTrue(mockAlertControllerProxy.showWithTitleCalled, "Should show the login expired alert if the notification has expired")
XCTAssertEqual(mockAlertControllerProxy.titlePassedIn, NSLocalizedString("Login Request Expired", comment: "Error message shown when a user is trying to login."), "")
}
func testHandlePushAuthenticationNotificationDoesNotShowTheLoginExpiredAlertIfNotificationHasNotExpired() {
pushAuthenticationManager!.handleAuthenticationNotification([:])
XCTAssertFalse(mockAlertControllerProxy.showWithTitleCalled, "Should not show the login expired alert if the notification hasn't expired")
}
func testHandlePushAuthenticationNotificationWithBlankTokenDoesNotShowLoginVerificationAlert() {
let pushNotificationDictionary = validAuthenticationDictionary
pushNotificationDictionary.removeObject(forKey: "push_auth_token")
pushAuthenticationManager!.handleAuthenticationNotification(pushNotificationDictionary)
XCTAssertFalse(mockAlertControllerProxy.showWithTitleCalled, "Should not show the login verification")
}
func testHandlePushAuthenticationNotificationWithBlankMessageDoesNotShowLoginVerificationAlert() {
pushAuthenticationManager!.handleAuthenticationNotification(["push_auth_token": "token"])
XCTAssertFalse(mockAlertControllerProxy.showWithTitleCalled, "Should not show the login verification")
}
func testHandlePushAuthenticationNotificationWithValidDataShouldShowLoginVerification() {
pushAuthenticationManager!.handleAuthenticationNotification(validAuthenticationDictionary)
XCTAssertTrue(mockAlertControllerProxy.showWithTitleCalled, "Should show the login verification")
XCTAssertEqual(mockAlertControllerProxy.titlePassedIn, NSLocalizedString("Verify Log In", comment: "Title of a prompt. A user must verify their login attempt."), "")
}
func testHandlePushAuthenticationNotificationShouldAttemptToAuthorizeTheLoginIfTheUserIndicatesTheyWantTo() {
pushAuthenticationManager!.handleAuthenticationNotification(validAuthenticationDictionary)
mockAlertControllerProxy.tapBlockPassedIn?(approvalAlertController, 1)
XCTAssertTrue(mockPushAuthenticationService.authorizedLoginCalled, "Should have attempted to authorize the login")
}
func testHandlePushAuthenticationNotificationWhenAttemptingToLoginShouldAttemptToRetryTheLoginIfItFailed() {
pushAuthenticationManager!.handleAuthenticationNotification(validAuthenticationDictionary)
mockAlertControllerProxy.tapBlockPassedIn?(approvalAlertController, 1)
mockPushAuthenticationService.completionBlockPassedIn?(false)
XCTAssertEqual(mockPushAuthenticationService.numberOfTimesAuthorizedLoginCalled, 2, "Should have attempted to retry a failed login")
}
func testHandlePushAuthenticationNotificationShouldNotAttemptToAuthorizeTheLoginIfTheUserIndicatesTheyDontWantTo() {
pushAuthenticationManager!.handleAuthenticationNotification(validAuthenticationDictionary)
mockAlertControllerProxy.tapBlockPassedIn?(approvalAlertController, 0)
XCTAssertFalse(mockPushAuthenticationService.authorizedLoginCalled, "Should not have attempted to authorize the login")
}
}