FRAIdentityTests.m revision 78c07714ec1113f7f21c75b818f2bf6a7021618a
2beebed98b4fc7f018fb224a1e4a3ab6103a4c0bCraig McDonnell/*
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 * License.
2beebed98b4fc7f018fb224a1e4a3ab6103a4c0bCraig McDonnell *
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 *
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 *
2beebed98b4fc7f018fb224a1e4a3ab6103a4c0bCraig McDonnell * Copyright 2016 ForgeRock AS.
2beebed98b4fc7f018fb224a1e4a3ab6103a4c0bCraig McDonnell */
2beebed98b4fc7f018fb224a1e4a3ab6103a4c0bCraig McDonnell
415243fbc81341293a852ff6aa14e9608d08685cCraig McDonnell#import <OCMock/OCMock.h>
2beebed98b4fc7f018fb224a1e4a3ab6103a4c0bCraig McDonnell#import <XCTest/XCTest.h>
415243fbc81341293a852ff6aa14e9608d08685cCraig McDonnell
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"
2beebed98b4fc7f018fb224a1e4a3ab6103a4c0bCraig McDonnell
2beebed98b4fc7f018fb224a1e4a3ab6103a4c0bCraig McDonnell@interface FRAIdentityTests : XCTestCase
2beebed98b4fc7f018fb224a1e4a3ab6103a4c0bCraig McDonnell
2beebed98b4fc7f018fb224a1e4a3ab6103a4c0bCraig McDonnell@end
2beebed98b4fc7f018fb224a1e4a3ab6103a4c0bCraig McDonnell
91f0e3cb60de3eba8cbb70c7e36cc0df22d71f5bRobert Wapshott@implementation FRAIdentityTests {
721bb987c406979bcfe705fa1ca8d54497d40fcbRobert Wapshott id mockSqlOperations;
721bb987c406979bcfe705fa1ca8d54497d40fcbRobert Wapshott id databaseObserverMock;
415243fbc81341293a852ff6aa14e9608d08685cCraig McDonnell FRAIdentityDatabase *database;
f877f6ca2428244a6d0954a1dbef471577b32c60Diego Colantoni FRAIdentity *identity;
f877f6ca2428244a6d0954a1dbef471577b32c60Diego Colantoni FRAIdentityModel *identityModel;
f877f6ca2428244a6d0954a1dbef471577b32c60Diego Colantoni NSString *issuer;
f877f6ca2428244a6d0954a1dbef471577b32c60Diego Colantoni NSString *accountName;
f877f6ca2428244a6d0954a1dbef471577b32c60Diego Colantoni NSURL *image;
91f0e3cb60de3eba8cbb70c7e36cc0df22d71f5bRobert Wapshott}
2beebed98b4fc7f018fb224a1e4a3ab6103a4c0bCraig McDonnell
2beebed98b4fc7f018fb224a1e4a3ab6103a4c0bCraig McDonnell- (void)setUp {
2beebed98b4fc7f018fb224a1e4a3ab6103a4c0bCraig McDonnell [super setUp];
415243fbc81341293a852ff6aa14e9608d08685cCraig McDonnell mockSqlOperations = OCMClassMock([FRAIdentityDatabaseSQLiteOperations class]);
415243fbc81341293a852ff6aa14e9608d08685cCraig McDonnell database = [[FRAIdentityDatabase alloc] initWithSqlOperations:mockSqlOperations];
415243fbc81341293a852ff6aa14e9608d08685cCraig McDonnell issuer = @"ForgeRock";
415243fbc81341293a852ff6aa14e9608d08685cCraig McDonnell accountName = @"joe.bloggs";
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 databaseObserverMock = OCMObserverMock();
2beebed98b4fc7f018fb224a1e4a3ab6103a4c0bCraig McDonnell}
2beebed98b4fc7f018fb224a1e4a3ab6103a4c0bCraig McDonnell
2beebed98b4fc7f018fb224a1e4a3ab6103a4c0bCraig McDonnell- (void)tearDown {
721bb987c406979bcfe705fa1ca8d54497d40fcbRobert Wapshott [mockSqlOperations stopMocking];
2beebed98b4fc7f018fb224a1e4a3ab6103a4c0bCraig McDonnell [super tearDown];
2beebed98b4fc7f018fb224a1e4a3ab6103a4c0bCraig McDonnell}
2beebed98b4fc7f018fb224a1e4a3ab6103a4c0bCraig McDonnell
415243fbc81341293a852ff6aa14e9608d08685cCraig McDonnell- (void)testCanInitIdentityWithLabelIssuerImage {
415243fbc81341293a852ff6aa14e9608d08685cCraig McDonnell XCTAssertEqualObjects(identity.issuer, issuer);
415243fbc81341293a852ff6aa14e9608d08685cCraig McDonnell XCTAssertEqualObjects(identity.accountName, accountName);
415243fbc81341293a852ff6aa14e9608d08685cCraig McDonnell XCTAssertEqualObjects([identity.image absoluteString], [image description]);
415243fbc81341293a852ff6aa14e9608d08685cCraig McDonnell}
415243fbc81341293a852ff6aa14e9608d08685cCraig McDonnell
415243fbc81341293a852ff6aa14e9608d08685cCraig McDonnell- (void)testCanAddMechanism {
415243fbc81341293a852ff6aa14e9608d08685cCraig McDonnell // Given
f877f6ca2428244a6d0954a1dbef471577b32c60Diego Colantoni FRAPushMechanism *pushMechanism = [[FRAPushMechanism alloc] initWithDatabase:database identityModel:identityModel];
415243fbc81341293a852ff6aa14e9608d08685cCraig McDonnell
415243fbc81341293a852ff6aa14e9608d08685cCraig McDonnell // When
721bb987c406979bcfe705fa1ca8d54497d40fcbRobert Wapshott BOOL mechanismAdded = [identity addMechanism:pushMechanism error:nil];
415243fbc81341293a852ff6aa14e9608d08685cCraig McDonnell
415243fbc81341293a852ff6aa14e9608d08685cCraig McDonnell // Then
721bb987c406979bcfe705fa1ca8d54497d40fcbRobert Wapshott XCTAssertTrue(mechanismAdded);
415243fbc81341293a852ff6aa14e9608d08685cCraig McDonnell XCTAssertEqual(pushMechanism.parent, identity);
415243fbc81341293a852ff6aa14e9608d08685cCraig McDonnell XCTAssertTrue([[identity mechanisms] containsObject:pushMechanism]);
415243fbc81341293a852ff6aa14e9608d08685cCraig McDonnell}
415243fbc81341293a852ff6aa14e9608d08685cCraig McDonnell
415243fbc81341293a852ff6aa14e9608d08685cCraig McDonnell- (void)testSavedIdentityAutomaticallySavesAddedMechanismToDatabase {
415243fbc81341293a852ff6aa14e9608d08685cCraig McDonnell // Given
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);
415243fbc81341293a852ff6aa14e9608d08685cCraig McDonnell
415243fbc81341293a852ff6aa14e9608d08685cCraig McDonnell // When
721bb987c406979bcfe705fa1ca8d54497d40fcbRobert Wapshott BOOL mechanismAdded = [identity addMechanism:pushMechanism error:nil];
415243fbc81341293a852ff6aa14e9608d08685cCraig McDonnell
415243fbc81341293a852ff6aa14e9608d08685cCraig McDonnell // Then
721bb987c406979bcfe705fa1ca8d54497d40fcbRobert Wapshott XCTAssertTrue(mechanismAdded);
415243fbc81341293a852ff6aa14e9608d08685cCraig McDonnell XCTAssertTrue([pushMechanism isStored]);
721bb987c406979bcfe705fa1ca8d54497d40fcbRobert Wapshott OCMVerify([(FRAIdentityDatabaseSQLiteOperations*)mockSqlOperations insertMechanism:pushMechanism error:nil]);
415243fbc81341293a852ff6aa14e9608d08685cCraig McDonnell}
415243fbc81341293a852ff6aa14e9608d08685cCraig McDonnell
415243fbc81341293a852ff6aa14e9608d08685cCraig McDonnell- (void)testBroadcastsOneChangeNotificationWhenMechanismIsAutomaticallySavedToDatabase {
415243fbc81341293a852ff6aa14e9608d08685cCraig McDonnell // Given
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);
415243fbc81341293a852ff6aa14e9608d08685cCraig McDonnell
415243fbc81341293a852ff6aa14e9608d08685cCraig McDonnell // When
721bb987c406979bcfe705fa1ca8d54497d40fcbRobert Wapshott BOOL mechanismAdded = [identity addMechanism:pushMechanism error:nil];
415243fbc81341293a852ff6aa14e9608d08685cCraig McDonnell
415243fbc81341293a852ff6aa14e9608d08685cCraig McDonnell // Then
721bb987c406979bcfe705fa1ca8d54497d40fcbRobert Wapshott XCTAssertTrue(mechanismAdded);
415243fbc81341293a852ff6aa14e9608d08685cCraig McDonnell OCMVerifyAll(databaseObserverMock);
415243fbc81341293a852ff6aa14e9608d08685cCraig McDonnell}
415243fbc81341293a852ff6aa14e9608d08685cCraig McDonnell
415243fbc81341293a852ff6aa14e9608d08685cCraig McDonnell- (void)testCanRemoveMechanism {
415243fbc81341293a852ff6aa14e9608d08685cCraig McDonnell // Given
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);
415243fbc81341293a852ff6aa14e9608d08685cCraig McDonnell
415243fbc81341293a852ff6aa14e9608d08685cCraig McDonnell // When
721bb987c406979bcfe705fa1ca8d54497d40fcbRobert Wapshott BOOL mechanismRemoved = [identity removeMechanism:pushMechanism error:nil];
415243fbc81341293a852ff6aa14e9608d08685cCraig McDonnell
415243fbc81341293a852ff6aa14e9608d08685cCraig McDonnell // Then
721bb987c406979bcfe705fa1ca8d54497d40fcbRobert Wapshott XCTAssertTrue(mechanismRemoved);
415243fbc81341293a852ff6aa14e9608d08685cCraig McDonnell XCTAssertEqual(pushMechanism.parent, nil);
415243fbc81341293a852ff6aa14e9608d08685cCraig McDonnell XCTAssertFalse([[identity mechanisms] containsObject:pushMechanism]);
415243fbc81341293a852ff6aa14e9608d08685cCraig McDonnell}
415243fbc81341293a852ff6aa14e9608d08685cCraig McDonnell
415243fbc81341293a852ff6aa14e9608d08685cCraig McDonnell- (void)testSavedIdentityAutomaticallyRemovesMechanismFromDatabase {
2beebed98b4fc7f018fb224a1e4a3ab6103a4c0bCraig McDonnell // Given
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);
2beebed98b4fc7f018fb224a1e4a3ab6103a4c0bCraig McDonnell
2beebed98b4fc7f018fb224a1e4a3ab6103a4c0bCraig McDonnell // When
721bb987c406979bcfe705fa1ca8d54497d40fcbRobert Wapshott BOOL mechanismRemoved = [identity removeMechanism:pushMechanism error:nil];
91f0e3cb60de3eba8cbb70c7e36cc0df22d71f5bRobert Wapshott
91f0e3cb60de3eba8cbb70c7e36cc0df22d71f5bRobert Wapshott // Then
721bb987c406979bcfe705fa1ca8d54497d40fcbRobert Wapshott XCTAssertTrue(mechanismRemoved);
415243fbc81341293a852ff6aa14e9608d08685cCraig McDonnell XCTAssertFalse([pushMechanism isStored]);
721bb987c406979bcfe705fa1ca8d54497d40fcbRobert Wapshott OCMVerify([(FRAIdentityDatabaseSQLiteOperations*)mockSqlOperations deleteMechanism:pushMechanism error:nil]);
91f0e3cb60de3eba8cbb70c7e36cc0df22d71f5bRobert Wapshott}
91f0e3cb60de3eba8cbb70c7e36cc0df22d71f5bRobert Wapshott
415243fbc81341293a852ff6aa14e9608d08685cCraig McDonnell- (void)testBroadcastsOneChangeNotificationWhenMechanismIsAutomaticallyRemovedFromDatabase {
91f0e3cb60de3eba8cbb70c7e36cc0df22d71f5bRobert Wapshott // Given
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);
91f0e3cb60de3eba8cbb70c7e36cc0df22d71f5bRobert Wapshott
91f0e3cb60de3eba8cbb70c7e36cc0df22d71f5bRobert Wapshott // When
721bb987c406979bcfe705fa1ca8d54497d40fcbRobert Wapshott BOOL mechanismRemoved = [identity removeMechanism:pushMechanism error:nil];
91f0e3cb60de3eba8cbb70c7e36cc0df22d71f5bRobert Wapshott
91f0e3cb60de3eba8cbb70c7e36cc0df22d71f5bRobert Wapshott // Then
721bb987c406979bcfe705fa1ca8d54497d40fcbRobert Wapshott XCTAssertTrue(mechanismRemoved);
415243fbc81341293a852ff6aa14e9608d08685cCraig McDonnell OCMVerifyAll(databaseObserverMock);
91f0e3cb60de3eba8cbb70c7e36cc0df22d71f5bRobert Wapshott}
91f0e3cb60de3eba8cbb70c7e36cc0df22d71f5bRobert Wapshott
f877f6ca2428244a6d0954a1dbef471577b32c60Diego Colantoni- (void)testRemoveMechanisRemovesIdentityFromIdentityModelIfLastMechanism {
91f0e3cb60de3eba8cbb70c7e36cc0df22d71f5bRobert Wapshott // Given
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
f877f6ca2428244a6d0954a1dbef471577b32c60Diego Colantoni // When
f877f6ca2428244a6d0954a1dbef471577b32c60Diego Colantoni [identity removeMechanism:pushMechanism error:nil];
f877f6ca2428244a6d0954a1dbef471577b32c60Diego Colantoni
f877f6ca2428244a6d0954a1dbef471577b32c60Diego Colantoni // Then
f877f6ca2428244a6d0954a1dbef471577b32c60Diego Colantoni XCTAssertEqual(identityModel.identities.count, 0);
f877f6ca2428244a6d0954a1dbef471577b32c60Diego Colantoni}
f877f6ca2428244a6d0954a1dbef471577b32c60Diego Colantoni
f877f6ca2428244a6d0954a1dbef471577b32c60Diego Colantoni- (void)testRemoveNonExistingMechanismFromIdentity {
f877f6ca2428244a6d0954a1dbef471577b32c60Diego Colantoni // Given
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 NSError *error;
f877f6ca2428244a6d0954a1dbef471577b32c60Diego Colantoni
f877f6ca2428244a6d0954a1dbef471577b32c60Diego Colantoni // When
f877f6ca2428244a6d0954a1dbef471577b32c60Diego Colantoni BOOL mechanismRemoved = [identity removeMechanism:pushMechanism error:&error];
f877f6ca2428244a6d0954a1dbef471577b32c60Diego Colantoni
f877f6ca2428244a6d0954a1dbef471577b32c60Diego Colantoni // Then
f877f6ca2428244a6d0954a1dbef471577b32c60Diego Colantoni XCTAssertFalse(mechanismRemoved);
f877f6ca2428244a6d0954a1dbef471577b32c60Diego Colantoni XCTAssertNotNil(error);
78c07714ec1113f7f21c75b818f2bf6a7021618aDiego Colantoni XCTAssertEqual(error.code, FRAInvalidOperation);
f877f6ca2428244a6d0954a1dbef471577b32c60Diego Colantoni}
f877f6ca2428244a6d0954a1dbef471577b32c60Diego Colantoni
f877f6ca2428244a6d0954a1dbef471577b32c60Diego Colantoni- (void)testCanQueryForMechanismByType {
f877f6ca2428244a6d0954a1dbef471577b32c60Diego Colantoni // Given
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);
91f0e3cb60de3eba8cbb70c7e36cc0df22d71f5bRobert Wapshott
91f0e3cb60de3eba8cbb70c7e36cc0df22d71f5bRobert Wapshott // When
721bb987c406979bcfe705fa1ca8d54497d40fcbRobert Wapshott BOOL oathMechanismAdded = [identity addMechanism:oathMechanism error:nil];
721bb987c406979bcfe705fa1ca8d54497d40fcbRobert Wapshott BOOL pushMechanismAdded = [identity addMechanism:pushMechanism error:nil];
2beebed98b4fc7f018fb224a1e4a3ab6103a4c0bCraig McDonnell
2beebed98b4fc7f018fb224a1e4a3ab6103a4c0bCraig McDonnell // Then
721bb987c406979bcfe705fa1ca8d54497d40fcbRobert Wapshott XCTAssertTrue(oathMechanismAdded);
721bb987c406979bcfe705fa1ca8d54497d40fcbRobert Wapshott XCTAssertTrue(pushMechanismAdded);
415243fbc81341293a852ff6aa14e9608d08685cCraig McDonnell XCTAssertEqualObjects([identity mechanismOfClass:[FRAOathMechanism class]], oathMechanism);
415243fbc81341293a852ff6aa14e9608d08685cCraig McDonnell XCTAssertEqualObjects([identity mechanismOfClass:[FRAPushMechanism class]], pushMechanism);
2beebed98b4fc7f018fb224a1e4a3ab6103a4c0bCraig McDonnell}
2beebed98b4fc7f018fb224a1e4a3ab6103a4c0bCraig McDonnell
78c07714ec1113f7f21c75b818f2bf6a7021618aDiego Colantoni- (void)testMechanismIsNotAddedIfIdentityHasSameTypeMechanism {
78c07714ec1113f7f21c75b818f2bf6a7021618aDiego Colantoni // Given
78c07714ec1113f7f21c75b818f2bf6a7021618aDiego Colantoni NSError * error;
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
78c07714ec1113f7f21c75b818f2bf6a7021618aDiego Colantoni // When
78c07714ec1113f7f21c75b818f2bf6a7021618aDiego Colantoni BOOL duplicateMechanismAdded = [identity addMechanism:duplicateMechanism error:&error];
78c07714ec1113f7f21c75b818f2bf6a7021618aDiego Colantoni
78c07714ec1113f7f21c75b818f2bf6a7021618aDiego Colantoni // Then
78c07714ec1113f7f21c75b818f2bf6a7021618aDiego Colantoni XCTAssertFalse(duplicateMechanismAdded);
78c07714ec1113f7f21c75b818f2bf6a7021618aDiego Colantoni XCTAssertNotNil(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}
78c07714ec1113f7f21c75b818f2bf6a7021618aDiego Colantoni
78c07714ec1113f7f21c75b818f2bf6a7021618aDiego Colantoni- (void)testMechanismIsAddedIfIdentityHasDifferentTypeMechanism {
78c07714ec1113f7f21c75b818f2bf6a7021618aDiego Colantoni // Given
78c07714ec1113f7f21c75b818f2bf6a7021618aDiego Colantoni NSError * error;
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);
78c07714ec1113f7f21c75b818f2bf6a7021618aDiego Colantoni
78c07714ec1113f7f21c75b818f2bf6a7021618aDiego Colantoni // When
78c07714ec1113f7f21c75b818f2bf6a7021618aDiego Colantoni BOOL mechanismAdded = [identity addMechanism:differentMechanism error:&error];
78c07714ec1113f7f21c75b818f2bf6a7021618aDiego Colantoni
78c07714ec1113f7f21c75b818f2bf6a7021618aDiego Colantoni // Then
78c07714ec1113f7f21c75b818f2bf6a7021618aDiego Colantoni XCTAssertTrue(mechanismAdded);
78c07714ec1113f7f21c75b818f2bf6a7021618aDiego Colantoni XCTAssertNil(error);
78c07714ec1113f7f21c75b818f2bf6a7021618aDiego Colantoni}
78c07714ec1113f7f21c75b818f2bf6a7021618aDiego Colantoni
2beebed98b4fc7f018fb224a1e4a3ab6103a4c0bCraig McDonnell@end