aboutsummaryrefslogtreecommitdiffhomepage
path: root/Example/Messaging/Tests/FIRMessagingRegistrarTest.m
blob: f3e66fdd10874163d6900b2d55ef8aaa7f69ea6d (plain)
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
/*
 * Copyright 2017 Google
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#import <UIKit/UIKit.h>
#import <XCTest/XCTest.h>

#import <OCMock/OCMock.h>

#import "FIRMessagingCheckinService.h"
#import "FIRMessagingPubSubRegistrar.h"
#import "FIRMessagingRegistrar.h"
#import "FIRMessagingUtilities.h"
#import "NSError+FIRMessaging.h"

static NSString *const kFIRMessagingUserDefaultsSuite =
    @"FIRMessagingRegistrarTestUserDefaultsSuite";

static NSString *const kDeviceAuthId = @"12345";
static NSString *const kSecretToken = @"45657809";
static NSString *const kVersionInfo = @"1.0";
static NSString *const kTopicToSubscribeTo = @"/topics/xyz/hello-world";
static NSString *const kFIRMessagingAppIDToken = @"abcdefgh1234lmno";
static NSString *const kSubscriptionID = @"sample-subscription-id-xyz";

@interface FIRMessagingRegistrar ()

@property(nonatomic, readwrite, strong) FIRMessagingPubSubRegistrar *pubsubRegistrar;
@property(nonatomic, readwrite, strong) FIRMessagingCheckinService *checkinService;

@end

@interface FIRMessagingRegistrarTest : XCTestCase

@property(nonatomic, readwrite, strong) FIRMessagingRegistrar *registrar;
@property(nonatomic, readwrite, strong) id mockRegistrar;
@property(nonatomic, readwrite, strong) id mockCheckin;
@property(nonatomic, readwrite, strong) id mockPubsubRegistrar;

@end

@implementation FIRMessagingRegistrarTest

- (void)setUp {
  [super setUp];
  _registrar = [[FIRMessagingRegistrar alloc] init];
  _mockRegistrar = OCMPartialMock(_registrar);
  _mockCheckin = OCMPartialMock(_registrar.checkinService);
  _registrar.checkinService = _mockCheckin;
  _registrar.pubsubRegistrar = OCMClassMock([FIRMessagingPubSubRegistrar class]);
  _mockPubsubRegistrar = _registrar.pubsubRegistrar;
}

- (void)testUpdateSubscriptionWithValidCheckinData {
  [self stubCheckinService];

  [self.registrar updateSubscriptionToTopic:kTopicToSubscribeTo
                                  withToken:kFIRMessagingAppIDToken
                                    options:nil
                               shouldDelete:NO
                                    handler:^(NSError *error){
                                    }];

  OCMVerify([self.mockPubsubRegistrar updateSubscriptionToTopic:[OCMArg isEqual:kTopicToSubscribeTo]
                                                      withToken:[OCMArg isEqual:kFIRMessagingAppIDToken]
                                                        options:nil
                                                   shouldDelete:NO
                                                        handler:OCMOCK_ANY]);
}

- (void)testUpdateSubscription {
  [self stubCheckinService];

  __block FIRMessagingTopicOperationCompletion pubsubCompletion;
  [[[self.mockPubsubRegistrar stub] andDo:^(NSInvocation *invocation) {
    pubsubCompletion(nil);
  }] updateSubscriptionToTopic:kTopicToSubscribeTo
                     withToken:kFIRMessagingAppIDToken
                       options:nil
                  shouldDelete:NO
                       handler:[OCMArg checkWithBlock:^BOOL(id obj) {
                         return (pubsubCompletion = obj) != nil;
                       }]];

  [self.registrar updateSubscriptionToTopic:kTopicToSubscribeTo
                                  withToken:kFIRMessagingAppIDToken
                                    options:nil
                               shouldDelete:NO
                                    handler:^(NSError *error) {
                                      XCTAssertNil(error);
                                    }];
}

- (void)testFailedUpdateSubscriptionWithNoCheckin {
  // Mock checkin service to always return NO for hasValidCheckinInfo
  [[[self.mockCheckin stub] andReturnValue:@NO] hasValidCheckinInfo];
  // This should not create a network request since we don't have checkin info
  [self.registrar updateSubscriptionToTopic:kTopicToSubscribeTo
                                  withToken:kFIRMessagingAppIDToken
                                    options:nil
                               shouldDelete:NO
                                    handler:^(NSError *error) {
                                      XCTAssertNotNil(error);
                                    }];
}

#pragma mark - Private Helpers

- (void)stubCheckinService {
  [[[self.mockCheckin stub] andReturn:kDeviceAuthId] deviceAuthID];
  [[[self.mockCheckin stub] andReturn:kSecretToken] secretToken];
  [[[self.mockCheckin stub] andReturnValue:@YES] hasValidCheckinInfo];
}

@end