FRANotificationTest.m revision cc2ae888007384c94072d7864e53548dd2840d33
2407N/A/*
2407N/A * The contents of this file are subject to the terms of the Common Development and
2407N/A * Distribution License (the License). You may not use this file except in compliance with the
2407N/A * License.
2407N/A *
2407N/A * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the
2407N/A * specific language governing permission and limitations under the License.
2407N/A *
2407N/A * When distributing Covered Software, include this CDDL Header Notice in each file and include
2407N/A * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL
2407N/A * Header, with the fields enclosed by brackets [] replaced by your own identifying
2407N/A * information: "Portions copyright [year] [name of copyright owner]".
2407N/A *
2407N/A * Copyright 2016 ForgeRock AS.
2407N/A */
2407N/A
2407N/A
2407N/A#import <OCMock/OCMock.h>
2407N/A#import <XCTest/XCTest.h>
2407N/A
2407N/A#import "FRAIdentityDatabase.h"
2407N/A#import "FRAIdentityDatabaseSQLiteOperations.h"
2407N/A#import "FRAIdentityModel.h"
2407N/A#import "FRAMessageUtils.h"
2407N/A#import "FRANotification.h"
2407N/A#import "FRAPushMechanism.h"
2407N/A
2407N/A@interface FRANotificationTest : XCTestCase
2407N/A
2407N/A@end
2407N/A
2412N/A@implementation FRANotificationTest {
2412N/A id mockSqlOperations;
2407N/A id mockIdentityModel;
2407N/A id databaseObserverMock;
2407N/A FRAIdentityDatabase *database;
2407N/A FRANotification* notification;
2407N/A id messageUtilsMock;
2407N/A FRAPushMechanism *mechanism;
2407N/A}
2407N/A
2407N/A- (void)setUp {
2407N/A [super setUp];
2500N/A
2500N/A mockSqlOperations = OCMClassMock([FRAIdentityDatabaseSQLiteOperations class]);
2500N/A mockIdentityModel = OCMClassMock([FRAIdentityModel class]);
2466N/A database = [[FRAIdentityDatabase alloc] initWithSqlOperations:mockSqlOperations];
2407N/A NSTimeInterval timeToLive = 120.0;
2407N/A mechanism = [[FRAPushMechanism alloc] initWithDatabase:database identityModel:mockIdentityModel authEndpoint:@"http://service.endpoint" secret:@"secret"];
2407N/A notification = [[FRANotification alloc] initWithDatabase:database identityModel:mockIdentityModel messageId:@"messageId" challenge:@"challenge" timeReceived:[NSDate date] timeToLive:timeToLive];
2407N/A OCMStub([(FRAIdentityDatabaseSQLiteOperations *)mockSqlOperations insertNotification:notification error:nil]).andReturn(YES);
2500N/A OCMStub([(FRAIdentityDatabaseSQLiteOperations *)mockSqlOperations updateNotification:notification error:nil]).andReturn(YES);
2500N/A databaseObserverMock = OCMObserverMock();
2500N/A messageUtilsMock = OCMClassMock([FRAMessageUtils class]);
2500N/A [notification setParent:mechanism];
2500N/A}
2500N/A
2407N/A- (void)tearDown {
2407N/A [mockSqlOperations stopMocking];
2407N/A [messageUtilsMock stopMocking];
2407N/A [super tearDown];
2407N/A}
2407N/A
2407N/A- (void)testInitialStateOfNotification {
2407N/A // Given
2407N/A
2407N/A // When
2407N/A
2407N/A // Then
2407N/A XCTAssertEqual([notification isPending], YES);
2407N/A XCTAssertEqual([notification isApproved], NO);
2407N/A XCTAssertEqual([notification isDenied], NO);
2407N/A XCTAssertEqual([notification isExpired], NO);
2407N/A}
2407N/A
2407N/A- (void)testShouldSetTimeExpired {
2407N/A // Given
2407N/A NSDate *timeReceived = [NSDate date];
2407N/A NSTimeInterval timeToLive = 120.0;
2407N/A FRANotification *expiringNotification = [[FRANotification alloc] initWithDatabase:database identityModel:mockIdentityModel messageId:@"messageId" challenge:@"challenge" timeReceived:timeReceived timeToLive:timeToLive];
2407N/A
2407N/A // When
2407N/A
2407N/A // Then
2407N/A XCTAssertEqualObjects([expiringNotification timeExpired], [timeReceived dateByAddingTimeInterval:timeToLive]);
2407N/A}
2407N/A
2501N/A- (void)testShouldApproveNotification {
2501N/A // Given
2407N/A
2407N/A // When
2407N/A [notification approveWithError:nil];
2407N/A
2407N/A // Then
2407N/A XCTAssertEqual([notification isPending], NO);
2407N/A XCTAssertEqual([notification isApproved], YES);
2407N/A XCTAssertEqual([notification isDenied], NO);
2407N/A}
2407N/A
2407N/A- (void)testApproveNotificationSendsMessageToService {
2407N/A // Given
2407N/A [database insertNotification:notification error:nil];
2407N/A OCMExpect([messageUtilsMock respondWithEndpoint:@"http://service.endpoint"
2412N/A base64Secret:@"secret"
2412N/A messageId:@"messageId"
2412N/A data:@{@"response":[FRAMessageUtils generateChallengeResponse:@"challenge"
2412N/A secret:@"secret"]}
2412N/A handler:[OCMArg any]]);
2412N/A
2412N/A // When
2412N/A [notification approveWithError:nil];
2407N/A
2500N/A // Then
2500N/A OCMVerifyAll(messageUtilsMock);
2500N/A}
2500N/A
2500N/A- (void)testShouldDenyNotification {
2500N/A // Given
2500N/A
2500N/A // When
2500N/A [notification denyWithError:nil];
2500N/A
2500N/A // Then
2500N/A XCTAssertEqual([notification isPending], NO);
2500N/A XCTAssertEqual([notification isApproved], NO);
2500N/A XCTAssertEqual([notification isDenied], YES);
2500N/A}
2500N/A
2500N/A- (void)testDenyNotificationSendsMessageToService {
2500N/A // Given
2500N/A [database insertNotification:notification error:nil];
2500N/A NSDictionary *expectedData = @{
2500N/A @"response":[FRAMessageUtils generateChallengeResponse:@"challenge" secret:@"secret"],
2500N/A @"deny": @YES
2500N/A };
2500N/A OCMExpect([messageUtilsMock respondWithEndpoint:@"http://service.endpoint"
2500N/A base64Secret:@"secret"
2500N/A messageId:@"messageId"
2500N/A data:expectedData
2500N/A handler:[OCMArg any]]);
2500N/A
2500N/A // When
2500N/A [notification denyWithError:nil];
2500N/A
2500N/A // Then
2500N/A OCMVerifyAll(messageUtilsMock);
2500N/A}
2500N/A
2500N/A- (void)testSavedNotificationAutomaticallySavesItselfToDatabaseWhenApproved {
2500N/A // Given
2500N/A OCMStub([(FRAIdentityDatabaseSQLiteOperations*)mockSqlOperations insertNotification:notification error:nil]).andReturn(YES);
2500N/A [database insertNotification:notification error:nil];
2500N/A OCMStub([(FRAIdentityDatabaseSQLiteOperations*)mockSqlOperations updateNotification:notification error:nil]).andReturn(YES);
2500N/A OCMStub([messageUtilsMock respondWithEndpoint:@"http://service.endpoint"
2500N/A base64Secret:@"secret"
2500N/A messageId:@"messageId"
2500N/A data:@{@"response":[FRAMessageUtils generateChallengeResponse:@"challenge"
2500N/A secret:@"secret"]}
2500N/A handler:[OCMArg any]]);
2500N/A
2500N/A
2500N/A // When
2500N/A BOOL notificationApproved = [notification approveWithError:nil];
2500N/A
2500N/A // Then
2500N/A XCTAssertTrue(notificationApproved);
2500N/A}
2500N/A
2500N/A- (void)testSavedNotificationAutomaticallySavesItselfToDatabaseWhenDenied {
2500N/A // Given
2500N/A OCMStub([(FRAIdentityDatabaseSQLiteOperations*)mockSqlOperations insertNotification:notification error:nil]).andReturn(YES);
2500N/A [database insertNotification:notification error:nil];
2500N/A OCMStub([(FRAIdentityDatabaseSQLiteOperations*)mockSqlOperations updateNotification:notification error:nil]).andReturn(YES);
2500N/A
2500N/A // When
2500N/A BOOL notificationDenied = [notification denyWithError:nil];
2500N/A
2500N/A // Then
2500N/A XCTAssertTrue(notificationDenied);
2500N/A}
2500N/A
2500N/A- (void)testBroadcastsOneChangeNotificationWhenNotificationUpdateIsAutomaticallySavedToDatabase {
2500N/A // Given
2500N/A OCMStub([(FRAIdentityDatabaseSQLiteOperations*)mockSqlOperations insertNotification:notification error:nil]).andReturn(YES);
2500N/A [database insertNotification:notification error:nil];
2500N/A OCMStub([(FRAIdentityDatabaseSQLiteOperations*)mockSqlOperations updateNotification:notification error:nil]).andReturn(YES);
2500N/A [[NSNotificationCenter defaultCenter] addMockObserver:databaseObserverMock name:FRAIdentityDatabaseChangedNotification object:database];
2500N/A [[databaseObserverMock expect] notificationWithName:FRAIdentityDatabaseChangedNotification object:database userInfo:[OCMArg any]];
2500N/A OCMStub([messageUtilsMock respondWithEndpoint:@"http://service.endpoint"
2500N/A base64Secret:@"secret"
2500N/A messageId:@"messageId"
2500N/A data:@{@"response":[FRAMessageUtils generateChallengeResponse:@"challenge"
2500N/A secret:@"secret"]}
2500N/A handler:[OCMArg any]]);
2500N/A
2500N/A // When
2500N/A BOOL notificationApproved = [notification approveWithError:nil];
2500N/A
2500N/A // Then
2500N/A XCTAssertTrue(notificationApproved);
2500N/A OCMVerifyAll(databaseObserverMock);
2500N/A}
2500N/A
2500N/A- (void)testIsExpiredReturnsYesIfNotificationHasExpired {
2500N/A // Given
2500N/A FRANotification *expiredNotification = [[FRANotification alloc] initWithDatabase:database identityModel:mockIdentityModel messageId:@"messageId" challenge:@"challenge" timeReceived:[NSDate date] timeToLive:-10.0];
2500N/A
2500N/A // When
2500N/A
2500N/A // Then
2500N/A XCTAssertEqual([expiredNotification isExpired], YES);
2500N/A XCTAssertEqual([expiredNotification isPending], NO);
2500N/A XCTAssertEqual([expiredNotification isApproved], NO);
2500N/A XCTAssertEqual([expiredNotification isDenied], NO);
2500N/A}
2500N/A
2500N/A- (void)testIsExpiredReturnsNoIfNotificationHasNotExpired {
2500N/A // Given
2500N/A FRANotification *expiredNotification = [[FRANotification alloc] initWithDatabase:database identityModel:mockIdentityModel messageId:@"messageId" challenge:@"challenge" timeReceived:[NSDate date] timeToLive:120.0];
2500N/A
2500N/A // When
2500N/A
2500N/A // Then
2500N/A XCTAssertEqual([expiredNotification isExpired], NO);
2500N/A XCTAssertEqual([expiredNotification isPending], YES);
2500N/A XCTAssertEqual([expiredNotification isApproved], NO);
2500N/A XCTAssertEqual([expiredNotification isDenied], NO);
2407N/A}
2407N/A
2407N/A@end
2407N/A
2407N/A