-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBopplServerTest.m
76 lines (59 loc) · 2.62 KB
/
BopplServerTest.m
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
//
// BopplServerTest.m
// BopplExample
//
// Created by Davide De Franceschi on 21/08/14.
// Copyright (c) 2014 Davide De Franceschi. All rights reserved.
//
#import <XCTest/XCTest.h>
#import "BopplServer.h"
#define TEST_TIMEOUT_TIME_S 10.0
#define TEST_TIMEOUT_TIME_NS ((int64_t)(TEST_TIMEOUT_TIME_S * NSEC_PER_SEC))
#pragma mark -
@interface BopplServerTest : XCTestCase
@property (strong, nonatomic) BasicHTTPAuthAccount *exampleAccount;
@property (strong, nonatomic) BasicHTTPAuthAccount *exampleInvalidAccount;
@property (strong, nonatomic) BopplServer *exampleServer;
@end
@implementation BopplServerTest
#pragma mark XCTestCase
- (void)setUp
{
[super setUp];
self.exampleAccount = [BasicHTTPAuthAccount accountWithUsername:@"[email protected]" andPassword:@"password123"];
self.exampleInvalidAccount = [BasicHTTPAuthAccount accountWithUsername:@"[email protected]" andPassword:@"password"];
self.exampleServer = [BopplFakeServer new];
self.exampleServer.account = self.exampleAccount;
}
#pragma mark BopplServerTest
- (void)testAuthenticationWithValidAccount
{
dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
[self.exampleServer authenticateAccountWithCompletion:^(BOOL authenticated, NSHTTPURLResponse *response, NSError *error) {
XCTAssertTrue(authenticated, @"Should have authenticated succesfully with a valid account.");
dispatch_semaphore_signal(semaphore);
}];
if (dispatch_semaphore_wait(semaphore, dispatch_time(DISPATCH_TIME_NOW, TEST_TIMEOUT_TIME_NS))) {
XCTFail(@"Asynchronous test %s did not finish within the timeout of %f seconds.", __PRETTY_FUNCTION__, TEST_TIMEOUT_TIME_S);
}
}
- (void)testAuthenticationWithInvalidAccount
{
dispatch_group_t semaphores = dispatch_group_create();
BopplServer *testServer = [BopplFakeServer new];
dispatch_group_enter(semaphores);
[testServer authenticateAccountWithCompletion:^(BOOL authenticated, NSHTTPURLResponse *response, NSError *error) {
XCTAssertFalse(authenticated, @"Should not have authenticated succesfully without an account.");
dispatch_group_leave(semaphores);
}];
dispatch_group_enter(semaphores);
testServer.account = self.exampleInvalidAccount;
[testServer authenticateAccountWithCompletion:^(BOOL authenticated, NSHTTPURLResponse *response, NSError *error) {
XCTAssertFalse(authenticated, @"Should not have authenticated succesfully with an invalid account.");
dispatch_group_leave(semaphores);
}];
if (dispatch_group_wait(semaphores, dispatch_time(DISPATCH_TIME_NOW, TEST_TIMEOUT_TIME_NS))) {
XCTFail(@"Asynchronous test %s did not finish within the timeout of %f seconds.", __PRETTY_FUNCTION__, TEST_TIMEOUT_TIME_S);
}
}
@end