FRANotificationTest.m revision 6a2ae9c7fb4d2c40d75cab0edaf940f22c18224f
2cacab200fb387e290386593d9b2fca2e7beef33csovant * The contents of this file are subject to the terms of the Common Development and
2cacab200fb387e290386593d9b2fca2e7beef33csovant * Distribution License (the License). You may not use this file except in compliance with the
2cacab200fb387e290386593d9b2fca2e7beef33csovant * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the
2cacab200fb387e290386593d9b2fca2e7beef33csovant * specific language governing permission and limitations under the License.
2cacab200fb387e290386593d9b2fca2e7beef33csovant * When distributing Covered Software, include this CDDL Header Notice in each file and include
2cacab200fb387e290386593d9b2fca2e7beef33csovant * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL
2cacab200fb387e290386593d9b2fca2e7beef33csovant * Header, with the fields enclosed by brackets [] replaced by your own identifying
2cacab200fb387e290386593d9b2fca2e7beef33csovant * information: "Portions copyright [year] [name of copyright owner]".
2cacab200fb387e290386593d9b2fca2e7beef33csovant * Copyright 2016 ForgeRock AS.
2cacab200fb387e290386593d9b2fca2e7beef33csovant#import "FRAIdentityDatabase.h"
2cacab200fb387e290386593d9b2fca2e7beef33csovant#import "FRAIdentityDatabaseSQLiteOperations.h"
2cacab200fb387e290386593d9b2fca2e7beef33csovant#import "FRAIdentityModel.h"
2cacab200fb387e290386593d9b2fca2e7beef33csovant#import "FRAMessageUtils.h"
23f633e1b8cc53fdac740eca4ab7e0341890bba2csovant#import "FRANotification.h"
2cacab200fb387e290386593d9b2fca2e7beef33csovant#import "FRAPushMechanism.h"
23f633e1b8cc53fdac740eca4ab7e0341890bba2csovant mockSqlOperations = OCMClassMock([FRAIdentityDatabaseSQLiteOperations class]);
2cacab200fb387e290386593d9b2fca2e7beef33csovant mockIdentityModel = OCMClassMock([FRAIdentityModel class]);
23f633e1b8cc53fdac740eca4ab7e0341890bba2csovant database = [[FRAIdentityDatabase alloc] initWithSqlOperations:mockSqlOperations];
2cacab200fb387e290386593d9b2fca2e7beef33csovant mechanism = [[FRAPushMechanism alloc] initWithDatabase:database identityModel:mockIdentityModel authEndpoint:@"http://service.endpoint" secret:@"secret"];
23f633e1b8cc53fdac740eca4ab7e0341890bba2csovant notification = [[FRANotification alloc] initWithDatabase:database identityModel:mockIdentityModel messageId:@"messageId" challenge:@"challenge" timeReceived:[NSDate date] timeToLive:timeToLive loadBalancerCookieData:@"amlbcookie=03"];
2cacab200fb387e290386593d9b2fca2e7beef33csovant OCMStub([(FRAIdentityDatabaseSQLiteOperations *)mockSqlOperations insertNotification:notification error:nil]).andReturn(YES);
2cacab200fb387e290386593d9b2fca2e7beef33csovant OCMStub([(FRAIdentityDatabaseSQLiteOperations *)mockSqlOperations updateNotification:notification error:nil]).andReturn(YES);
23f633e1b8cc53fdac740eca4ab7e0341890bba2csovant messageUtilsMock = OCMClassMock([FRAMessageUtils class]);
2cacab200fb387e290386593d9b2fca2e7beef33csovant FRANotification *expiringNotification = [[FRANotification alloc] initWithDatabase:database identityModel:mockIdentityModel messageId:@"messageId" challenge:@"challenge" timeReceived:timeReceived timeToLive:timeToLive loadBalancerCookieData:@"amlbcookie=03"];
2cacab200fb387e290386593d9b2fca2e7beef33csovant XCTAssertEqualObjects([expiringNotification timeExpired], [timeReceived dateByAddingTimeInterval:timeToLive]);
23f633e1b8cc53fdac740eca4ab7e0341890bba2csovant- (void)testApproveNotificationSendsMessageToService {
23f633e1b8cc53fdac740eca4ab7e0341890bba2csovant [database insertNotification:notification error:nil];
23f633e1b8cc53fdac740eca4ab7e0341890bba2csovant OCMExpect([messageUtilsMock respondWithEndpoint:@"http://service.endpoint"
23f633e1b8cc53fdac740eca4ab7e0341890bba2csovant data:@{@"response":[FRAMessageUtils generateChallengeResponse:@"challenge"
23f633e1b8cc53fdac740eca4ab7e0341890bba2csovant [database insertNotification:notification error:nil];
23f633e1b8cc53fdac740eca4ab7e0341890bba2csovant @"response":[FRAMessageUtils generateChallengeResponse:@"challenge" secret:@"secret"],
23f633e1b8cc53fdac740eca4ab7e0341890bba2csovant OCMExpect([messageUtilsMock respondWithEndpoint:@"http://service.endpoint"
62ecec3a82a8b838ee76c1f6610902d8fd7015cbmatthew_swift- (void)testSavedNotificationAutomaticallySavesItselfToDatabaseWhenApproved {
62ecec3a82a8b838ee76c1f6610902d8fd7015cbmatthew_swift OCMStub([(FRAIdentityDatabaseSQLiteOperations*)mockSqlOperations insertNotification:notification error:nil]).andReturn(YES);
62ecec3a82a8b838ee76c1f6610902d8fd7015cbmatthew_swift [database insertNotification:notification error:nil];
62ecec3a82a8b838ee76c1f6610902d8fd7015cbmatthew_swift OCMStub([(FRAIdentityDatabaseSQLiteOperations*)mockSqlOperations updateNotification:notification error:nil]).andReturn(YES);
62ecec3a82a8b838ee76c1f6610902d8fd7015cbmatthew_swift OCMStub([messageUtilsMock respondWithEndpoint:@"http://service.endpoint"
23f633e1b8cc53fdac740eca4ab7e0341890bba2csovant data:@{@"response":[FRAMessageUtils generateChallengeResponse:@"challenge"
23f633e1b8cc53fdac740eca4ab7e0341890bba2csovant BOOL notificationApproved = [notification approveWithHandler:nil error:nil];
23f633e1b8cc53fdac740eca4ab7e0341890bba2csovant- (void)testSavedNotificationAutomaticallySavesItselfToDatabaseWhenDenied {
23f633e1b8cc53fdac740eca4ab7e0341890bba2csovant OCMStub([(FRAIdentityDatabaseSQLiteOperations*)mockSqlOperations insertNotification:notification error:nil]).andReturn(YES);
2cacab200fb387e290386593d9b2fca2e7beef33csovant [database insertNotification:notification error:nil];
23f633e1b8cc53fdac740eca4ab7e0341890bba2csovant OCMStub([(FRAIdentityDatabaseSQLiteOperations*)mockSqlOperations updateNotification:notification error:nil]).andReturn(YES);
2cacab200fb387e290386593d9b2fca2e7beef33csovant BOOL notificationDenied = [notification denyWithHandler:nil error:nil];
2cacab200fb387e290386593d9b2fca2e7beef33csovant- (void)testBroadcastsOneChangeNotificationWhenNotificationUpdateIsAutomaticallySavedToDatabase {
2cacab200fb387e290386593d9b2fca2e7beef33csovant OCMStub([(FRAIdentityDatabaseSQLiteOperations*)mockSqlOperations insertNotification:notification error:nil]).andReturn(YES);
23f633e1b8cc53fdac740eca4ab7e0341890bba2csovant [database insertNotification:notification error:nil];
23f633e1b8cc53fdac740eca4ab7e0341890bba2csovant OCMStub([(FRAIdentityDatabaseSQLiteOperations*)mockSqlOperations updateNotification:notification error:nil]).andReturn(YES);
23f633e1b8cc53fdac740eca4ab7e0341890bba2csovant [[NSNotificationCenter defaultCenter] addMockObserver:databaseObserverMock name:FRAIdentityDatabaseChangedNotification object:database];
23f633e1b8cc53fdac740eca4ab7e0341890bba2csovant [[databaseObserverMock expect] notificationWithName:FRAIdentityDatabaseChangedNotification object:database userInfo:[OCMArg any]];
23f633e1b8cc53fdac740eca4ab7e0341890bba2csovant OCMStub([messageUtilsMock respondWithEndpoint:@"http://service.endpoint"
23f633e1b8cc53fdac740eca4ab7e0341890bba2csovant data:@{@"response":[FRAMessageUtils generateChallengeResponse:@"challenge"
62ecec3a82a8b838ee76c1f6610902d8fd7015cbmatthew_swift BOOL notificationApproved = [notification approveWithHandler:nil error:nil];
23f633e1b8cc53fdac740eca4ab7e0341890bba2csovant- (void)testIsExpiredReturnsYesIfNotificationHasExpired {
23f633e1b8cc53fdac740eca4ab7e0341890bba2csovant FRANotification *expiredNotification = [[FRANotification alloc] initWithDatabase:database identityModel:mockIdentityModel messageId:@"messageId" challenge:@"challenge" timeReceived:[NSDate date] timeToLive:-10.0 loadBalancerCookieData:@"amlbcookie=03"];
23f633e1b8cc53fdac740eca4ab7e0341890bba2csovant XCTAssertEqual([expiredNotification isExpired], YES);
23f633e1b8cc53fdac740eca4ab7e0341890bba2csovant XCTAssertEqual([expiredNotification isPending], NO);
23f633e1b8cc53fdac740eca4ab7e0341890bba2csovant XCTAssertEqual([expiredNotification isApproved], NO);
23f633e1b8cc53fdac740eca4ab7e0341890bba2csovant- (void)testIsExpiredReturnsNoIfNotificationHasNotExpired {
23f633e1b8cc53fdac740eca4ab7e0341890bba2csovant FRANotification *expiredNotification = [[FRANotification alloc] initWithDatabase:database identityModel:mockIdentityModel messageId:@"messageId" challenge:@"challenge" timeReceived:[NSDate date] timeToLive:120.0 loadBalancerCookieData:@"amlbcookie=03"];
23f633e1b8cc53fdac740eca4ab7e0341890bba2csovant XCTAssertEqual([expiredNotification isExpired], NO);
23f633e1b8cc53fdac740eca4ab7e0341890bba2csovant XCTAssertEqual([expiredNotification isPending], YES);