FRAIdentityTests.m revision 78c07714ec1113f7f21c75b818f2bf6a7021618a
2beebed98b4fc7f018fb224a1e4a3ab6103a4c0bCraig McDonnell * The contents of this file are subject to the terms of the Common Development and
2beebed98b4fc7f018fb224a1e4a3ab6103a4c0bCraig McDonnell * Distribution License (the License). You may not use this file except in compliance with the
2beebed98b4fc7f018fb224a1e4a3ab6103a4c0bCraig McDonnell * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the
2beebed98b4fc7f018fb224a1e4a3ab6103a4c0bCraig McDonnell * specific language governing permission and limitations under the License.
2beebed98b4fc7f018fb224a1e4a3ab6103a4c0bCraig McDonnell * When distributing Covered Software, include this CDDL Header Notice in each file and include
2beebed98b4fc7f018fb224a1e4a3ab6103a4c0bCraig McDonnell * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL
2beebed98b4fc7f018fb224a1e4a3ab6103a4c0bCraig McDonnell * Header, with the fields enclosed by brackets [] replaced by your own identifying
2beebed98b4fc7f018fb224a1e4a3ab6103a4c0bCraig McDonnell * information: "Portions copyright [year] [name of copyright owner]".
2beebed98b4fc7f018fb224a1e4a3ab6103a4c0bCraig McDonnell * Copyright 2016 ForgeRock AS.
78c07714ec1113f7f21c75b818f2bf6a7021618aDiego Colantoni#import "FRAerror.h"
2beebed98b4fc7f018fb224a1e4a3ab6103a4c0bCraig McDonnell#import "FRAIdentity.h"
415243fbc81341293a852ff6aa14e9608d08685cCraig McDonnell#import "FRAIdentityDatabase.h"
415243fbc81341293a852ff6aa14e9608d08685cCraig McDonnell#import "FRAIdentityDatabaseSQLiteOperations.h"
f877f6ca2428244a6d0954a1dbef471577b32c60Diego Colantoni#import "FRAIdentityModel.h"
91f0e3cb60de3eba8cbb70c7e36cc0df22d71f5bRobert Wapshott#import "FRAMechanism.h"
415243fbc81341293a852ff6aa14e9608d08685cCraig McDonnell#import "FRAOathMechanism.h"
415243fbc81341293a852ff6aa14e9608d08685cCraig McDonnell#import "FRAPushMechanism.h"
415243fbc81341293a852ff6aa14e9608d08685cCraig McDonnell mockSqlOperations = OCMClassMock([FRAIdentityDatabaseSQLiteOperations class]);
415243fbc81341293a852ff6aa14e9608d08685cCraig McDonnell database = [[FRAIdentityDatabase alloc] initWithSqlOperations:mockSqlOperations];
415243fbc81341293a852ff6aa14e9608d08685cCraig McDonnell image = [NSURL URLWithString:@"https://forgerock.org/ico/favicon-32x32.png"];
f877f6ca2428244a6d0954a1dbef471577b32c60Diego Colantoni identityModel = [[FRAIdentityModel alloc] initWithDatabase:database sqlDatabase:nil];
f877f6ca2428244a6d0954a1dbef471577b32c60Diego Colantoni identity = [FRAIdentity identityWithDatabase:database identityModel:identityModel accountName:accountName issuer:issuer image:image backgroundColor:nil];
f877f6ca2428244a6d0954a1dbef471577b32c60Diego Colantoni [identityModel addIdentity:identity error:nil];
415243fbc81341293a852ff6aa14e9608d08685cCraig McDonnell- (void)testCanInitIdentityWithLabelIssuerImage {
415243fbc81341293a852ff6aa14e9608d08685cCraig McDonnell XCTAssertEqualObjects(identity.issuer, issuer);
415243fbc81341293a852ff6aa14e9608d08685cCraig McDonnell XCTAssertEqualObjects(identity.accountName, accountName);
415243fbc81341293a852ff6aa14e9608d08685cCraig McDonnell XCTAssertEqualObjects([identity.image absoluteString], [image description]);
f877f6ca2428244a6d0954a1dbef471577b32c60Diego Colantoni FRAPushMechanism *pushMechanism = [[FRAPushMechanism alloc] initWithDatabase:database identityModel:identityModel];
721bb987c406979bcfe705fa1ca8d54497d40fcbRobert Wapshott BOOL mechanismAdded = [identity addMechanism:pushMechanism error:nil];
415243fbc81341293a852ff6aa14e9608d08685cCraig McDonnell XCTAssertEqual(pushMechanism.parent, identity);
415243fbc81341293a852ff6aa14e9608d08685cCraig McDonnell XCTAssertTrue([[identity mechanisms] containsObject:pushMechanism]);
415243fbc81341293a852ff6aa14e9608d08685cCraig McDonnell- (void)testSavedIdentityAutomaticallySavesAddedMechanismToDatabase {
f877f6ca2428244a6d0954a1dbef471577b32c60Diego Colantoni OCMStub([mockSqlOperations insertIdentity:identity error:nil]).andReturn(YES);
721bb987c406979bcfe705fa1ca8d54497d40fcbRobert Wapshott [database insertIdentity:identity error:nil];
f877f6ca2428244a6d0954a1dbef471577b32c60Diego Colantoni FRAPushMechanism *pushMechanism = [[FRAPushMechanism alloc] initWithDatabase:database identityModel:identityModel];
f877f6ca2428244a6d0954a1dbef471577b32c60Diego Colantoni OCMStub([mockSqlOperations insertMechanism:pushMechanism error:nil]).andReturn(YES);
721bb987c406979bcfe705fa1ca8d54497d40fcbRobert Wapshott BOOL mechanismAdded = [identity addMechanism:pushMechanism error:nil];
721bb987c406979bcfe705fa1ca8d54497d40fcbRobert Wapshott OCMVerify([(FRAIdentityDatabaseSQLiteOperations*)mockSqlOperations insertMechanism:pushMechanism error:nil]);
415243fbc81341293a852ff6aa14e9608d08685cCraig McDonnell- (void)testBroadcastsOneChangeNotificationWhenMechanismIsAutomaticallySavedToDatabase {
f877f6ca2428244a6d0954a1dbef471577b32c60Diego Colantoni OCMStub([mockSqlOperations insertIdentity:identity error:nil]).andReturn(YES);
721bb987c406979bcfe705fa1ca8d54497d40fcbRobert Wapshott [database insertIdentity:identity error:nil];
f877f6ca2428244a6d0954a1dbef471577b32c60Diego Colantoni FRAPushMechanism *pushMechanism = [[FRAPushMechanism alloc] initWithDatabase:database identityModel:identityModel];
415243fbc81341293a852ff6aa14e9608d08685cCraig McDonnell [[NSNotificationCenter defaultCenter] addMockObserver:databaseObserverMock name:FRAIdentityDatabaseChangedNotification object:database];
415243fbc81341293a852ff6aa14e9608d08685cCraig McDonnell [[databaseObserverMock expect] notificationWithName:FRAIdentityDatabaseChangedNotification object:database userInfo:[OCMArg any]];
f877f6ca2428244a6d0954a1dbef471577b32c60Diego Colantoni OCMStub([mockSqlOperations insertMechanism:pushMechanism error:nil]).andReturn(YES);
721bb987c406979bcfe705fa1ca8d54497d40fcbRobert Wapshott BOOL mechanismAdded = [identity addMechanism:pushMechanism error:nil];
f877f6ca2428244a6d0954a1dbef471577b32c60Diego Colantoni OCMStub([mockSqlOperations insertIdentity:identity error:nil]).andReturn(YES);
f877f6ca2428244a6d0954a1dbef471577b32c60Diego Colantoni [database insertIdentity:identity error:nil];
f877f6ca2428244a6d0954a1dbef471577b32c60Diego Colantoni FRAPushMechanism *pushMechanism = [[FRAPushMechanism alloc] initWithDatabase:database identityModel:identityModel];
721bb987c406979bcfe705fa1ca8d54497d40fcbRobert Wapshott OCMStub([(FRAIdentityDatabaseSQLiteOperations*)mockSqlOperations insertMechanism:pushMechanism error:nil]).andReturn(YES);
721bb987c406979bcfe705fa1ca8d54497d40fcbRobert Wapshott [identity addMechanism:pushMechanism error:nil];
721bb987c406979bcfe705fa1ca8d54497d40fcbRobert Wapshott OCMStub([(FRAIdentityDatabaseSQLiteOperations*)mockSqlOperations deleteMechanism:pushMechanism error:nil]).andReturn(YES);
f877f6ca2428244a6d0954a1dbef471577b32c60Diego Colantoni OCMStub([(FRAIdentityDatabaseSQLiteOperations*)mockSqlOperations deleteIdentity:identity error:nil]).andReturn(YES);
721bb987c406979bcfe705fa1ca8d54497d40fcbRobert Wapshott BOOL mechanismRemoved = [identity removeMechanism:pushMechanism error:nil];
415243fbc81341293a852ff6aa14e9608d08685cCraig McDonnell XCTAssertFalse([[identity mechanisms] containsObject:pushMechanism]);
415243fbc81341293a852ff6aa14e9608d08685cCraig McDonnell- (void)testSavedIdentityAutomaticallyRemovesMechanismFromDatabase {
f877f6ca2428244a6d0954a1dbef471577b32c60Diego Colantoni OCMStub([mockSqlOperations insertIdentity:identity error:nil]).andReturn(YES);
f877f6ca2428244a6d0954a1dbef471577b32c60Diego Colantoni [database insertIdentity:identity error:nil];
721bb987c406979bcfe705fa1ca8d54497d40fcbRobert Wapshott OCMStub([(FRAIdentityDatabaseSQLiteOperations*)mockSqlOperations insertIdentity:identity error:nil]).andReturn(YES);
721bb987c406979bcfe705fa1ca8d54497d40fcbRobert Wapshott [database insertIdentity:identity error:nil];
f877f6ca2428244a6d0954a1dbef471577b32c60Diego Colantoni FRAPushMechanism *pushMechanism = [[FRAPushMechanism alloc] initWithDatabase:database identityModel:identityModel];
721bb987c406979bcfe705fa1ca8d54497d40fcbRobert Wapshott OCMStub([(FRAIdentityDatabaseSQLiteOperations*)mockSqlOperations insertMechanism:pushMechanism error:nil]).andReturn(YES);
721bb987c406979bcfe705fa1ca8d54497d40fcbRobert Wapshott [identity addMechanism:pushMechanism error:nil];
721bb987c406979bcfe705fa1ca8d54497d40fcbRobert Wapshott OCMStub([(FRAIdentityDatabaseSQLiteOperations*)mockSqlOperations deleteMechanism:pushMechanism error:nil]).andReturn(YES);
f877f6ca2428244a6d0954a1dbef471577b32c60Diego Colantoni OCMStub([(FRAIdentityDatabaseSQLiteOperations*)mockSqlOperations deleteIdentity:identity error:nil]).andReturn(YES);
721bb987c406979bcfe705fa1ca8d54497d40fcbRobert Wapshott BOOL mechanismRemoved = [identity removeMechanism:pushMechanism error:nil];
721bb987c406979bcfe705fa1ca8d54497d40fcbRobert Wapshott OCMVerify([(FRAIdentityDatabaseSQLiteOperations*)mockSqlOperations deleteMechanism:pushMechanism error:nil]);
415243fbc81341293a852ff6aa14e9608d08685cCraig McDonnell- (void)testBroadcastsOneChangeNotificationWhenMechanismIsAutomaticallyRemovedFromDatabase {
721bb987c406979bcfe705fa1ca8d54497d40fcbRobert Wapshott OCMStub([(FRAIdentityDatabaseSQLiteOperations*)mockSqlOperations insertIdentity:identity error:nil]).andReturn(YES);
721bb987c406979bcfe705fa1ca8d54497d40fcbRobert Wapshott [database insertIdentity:identity error:nil];
f877f6ca2428244a6d0954a1dbef471577b32c60Diego Colantoni FRAPushMechanism *pushMechanism = [[FRAPushMechanism alloc] initWithDatabase:database identityModel:identityModel];
721bb987c406979bcfe705fa1ca8d54497d40fcbRobert Wapshott OCMStub([(FRAIdentityDatabaseSQLiteOperations*)mockSqlOperations insertMechanism:pushMechanism error:nil]).andReturn(YES);
721bb987c406979bcfe705fa1ca8d54497d40fcbRobert Wapshott [identity addMechanism:pushMechanism error:nil];
415243fbc81341293a852ff6aa14e9608d08685cCraig McDonnell [[NSNotificationCenter defaultCenter] addMockObserver:databaseObserverMock name:FRAIdentityDatabaseChangedNotification object:database];
415243fbc81341293a852ff6aa14e9608d08685cCraig McDonnell [[databaseObserverMock expect] notificationWithName:FRAIdentityDatabaseChangedNotification object:database userInfo:[OCMArg any]];
721bb987c406979bcfe705fa1ca8d54497d40fcbRobert Wapshott OCMStub([(FRAIdentityDatabaseSQLiteOperations*)mockSqlOperations deleteMechanism:pushMechanism error:nil]).andReturn(YES);
f877f6ca2428244a6d0954a1dbef471577b32c60Diego Colantoni OCMStub([(FRAIdentityDatabaseSQLiteOperations*)mockSqlOperations deleteIdentity:identity error:nil]).andReturn(YES);
721bb987c406979bcfe705fa1ca8d54497d40fcbRobert Wapshott BOOL mechanismRemoved = [identity removeMechanism:pushMechanism error:nil];
f877f6ca2428244a6d0954a1dbef471577b32c60Diego Colantoni- (void)testRemoveMechanisRemovesIdentityFromIdentityModelIfLastMechanism {
f877f6ca2428244a6d0954a1dbef471577b32c60Diego Colantoni FRAPushMechanism *pushMechanism = [[FRAPushMechanism alloc] initWithDatabase:database identityModel:identityModel];
721bb987c406979bcfe705fa1ca8d54497d40fcbRobert Wapshott OCMStub([(FRAIdentityDatabaseSQLiteOperations*)mockSqlOperations insertMechanism:pushMechanism error:nil]).andReturn(YES);
f877f6ca2428244a6d0954a1dbef471577b32c60Diego Colantoni [identity addMechanism:pushMechanism error:nil];
f877f6ca2428244a6d0954a1dbef471577b32c60Diego Colantoni OCMStub([(FRAIdentityDatabaseSQLiteOperations*)mockSqlOperations deleteMechanism:pushMechanism error:nil]).andReturn(YES);
f877f6ca2428244a6d0954a1dbef471577b32c60Diego Colantoni [identity removeMechanism:pushMechanism error:nil];
f877f6ca2428244a6d0954a1dbef471577b32c60Diego Colantoni XCTAssertEqual(identityModel.identities.count, 0);
f877f6ca2428244a6d0954a1dbef471577b32c60Diego Colantoni- (void)testRemoveNonExistingMechanismFromIdentity {
f877f6ca2428244a6d0954a1dbef471577b32c60Diego Colantoni OCMStub([(FRAIdentityDatabaseSQLiteOperations*)mockSqlOperations insertIdentity:identity error:nil]).andReturn(YES);
f877f6ca2428244a6d0954a1dbef471577b32c60Diego Colantoni [database insertIdentity:identity error:nil];
f877f6ca2428244a6d0954a1dbef471577b32c60Diego Colantoni FRAPushMechanism *pushMechanism = [[FRAPushMechanism alloc] initWithDatabase:database identityModel:identityModel];
f877f6ca2428244a6d0954a1dbef471577b32c60Diego Colantoni [pushMechanism setValue:[NSNumber numberWithBool:YES] forKey:@"stored"];
f877f6ca2428244a6d0954a1dbef471577b32c60Diego Colantoni OCMReject([(FRAIdentityDatabaseSQLiteOperations*)mockSqlOperations deleteMechanism:pushMechanism error:[OCMArg anyObjectRef]]);
f877f6ca2428244a6d0954a1dbef471577b32c60Diego Colantoni BOOL mechanismRemoved = [identity removeMechanism:pushMechanism error:&error];
78c07714ec1113f7f21c75b818f2bf6a7021618aDiego Colantoni XCTAssertEqual(error.code, FRAInvalidOperation);
f877f6ca2428244a6d0954a1dbef471577b32c60Diego Colantoni FRAOathMechanism *oathMechanism = [[FRAOathMechanism alloc] initWithDatabase:database identityModel:identityModel];
f877f6ca2428244a6d0954a1dbef471577b32c60Diego Colantoni FRAPushMechanism *pushMechanism = [[FRAPushMechanism alloc] initWithDatabase:database identityModel:identityModel];
f877f6ca2428244a6d0954a1dbef471577b32c60Diego Colantoni OCMStub([mockSqlOperations insertMechanism:oathMechanism error:nil]).andReturn(YES);
f877f6ca2428244a6d0954a1dbef471577b32c60Diego Colantoni OCMStub([mockSqlOperations insertMechanism:pushMechanism error:nil]).andReturn(YES);
721bb987c406979bcfe705fa1ca8d54497d40fcbRobert Wapshott BOOL oathMechanismAdded = [identity addMechanism:oathMechanism error:nil];
721bb987c406979bcfe705fa1ca8d54497d40fcbRobert Wapshott BOOL pushMechanismAdded = [identity addMechanism:pushMechanism error:nil];
415243fbc81341293a852ff6aa14e9608d08685cCraig McDonnell XCTAssertEqualObjects([identity mechanismOfClass:[FRAOathMechanism class]], oathMechanism);
415243fbc81341293a852ff6aa14e9608d08685cCraig McDonnell XCTAssertEqualObjects([identity mechanismOfClass:[FRAPushMechanism class]], pushMechanism);
78c07714ec1113f7f21c75b818f2bf6a7021618aDiego Colantoni- (void)testMechanismIsNotAddedIfIdentityHasSameTypeMechanism {
78c07714ec1113f7f21c75b818f2bf6a7021618aDiego Colantoni FRAPushMechanism *mechanism = [[FRAPushMechanism alloc] initWithDatabase:database identityModel:nil];
78c07714ec1113f7f21c75b818f2bf6a7021618aDiego Colantoni OCMStub([(FRAIdentityDatabaseSQLiteOperations*)mockSqlOperations insertMechanism:mechanism error:nil]).andReturn(YES);
78c07714ec1113f7f21c75b818f2bf6a7021618aDiego Colantoni [identity addMechanism:mechanism error:nil];
78c07714ec1113f7f21c75b818f2bf6a7021618aDiego Colantoni FRAPushMechanism *duplicateMechanism = [[FRAPushMechanism alloc] initWithDatabase:database identityModel:nil];
78c07714ec1113f7f21c75b818f2bf6a7021618aDiego Colantoni OCMStub([(FRAIdentityDatabaseSQLiteOperations*)mockSqlOperations insertMechanism:duplicateMechanism error:nil]).andReturn(YES);
78c07714ec1113f7f21c75b818f2bf6a7021618aDiego Colantoni BOOL duplicateMechanismAdded = [identity addMechanism:duplicateMechanism error:&error];
78c07714ec1113f7f21c75b818f2bf6a7021618aDiego Colantoni XCTAssertEqual(error.code, FRADuplicateMechanism);
78c07714ec1113f7f21c75b818f2bf6a7021618aDiego Colantoni XCTAssertEqual([error.userInfo valueForKey:@"identity"], identity);
78c07714ec1113f7f21c75b818f2bf6a7021618aDiego Colantoni XCTAssertEqual([error.userInfo valueForKey:@"mechanism"], mechanism);
78c07714ec1113f7f21c75b818f2bf6a7021618aDiego Colantoni- (void)testMechanismIsAddedIfIdentityHasDifferentTypeMechanism {
78c07714ec1113f7f21c75b818f2bf6a7021618aDiego Colantoni FRAOathMechanism *mechanism = [[FRAOathMechanism alloc] initWithDatabase:database identityModel:nil];
78c07714ec1113f7f21c75b818f2bf6a7021618aDiego Colantoni OCMStub([(FRAIdentityDatabaseSQLiteOperations*)mockSqlOperations insertMechanism:mechanism error:nil]).andReturn(YES);
78c07714ec1113f7f21c75b818f2bf6a7021618aDiego Colantoni [identity addMechanism:mechanism error:nil];
78c07714ec1113f7f21c75b818f2bf6a7021618aDiego Colantoni FRAPushMechanism *differentMechanism = [[FRAPushMechanism alloc] initWithDatabase:database identityModel:nil];
78c07714ec1113f7f21c75b818f2bf6a7021618aDiego Colantoni OCMStub([(FRAIdentityDatabaseSQLiteOperations*)mockSqlOperations insertMechanism:differentMechanism error:nil]).andReturn(YES);