FRAModelsFromDatabaseTest.m revision f877f6ca2428244a6d0954a1dbef471577b32c60
59190ecd61435d19ba3515b876272aee7bd12298vboxsync/*
59190ecd61435d19ba3515b876272aee7bd12298vboxsync * The contents of this file are subject to the terms of the Common Development and
59190ecd61435d19ba3515b876272aee7bd12298vboxsync * Distribution License (the License). You may not use this file except in compliance with the
59190ecd61435d19ba3515b876272aee7bd12298vboxsync * License.
59190ecd61435d19ba3515b876272aee7bd12298vboxsync *
59190ecd61435d19ba3515b876272aee7bd12298vboxsync * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the
772269936494ffaddd0750ba9e28e805ba81398cvboxsync * specific language governing permission and limitations under the License.
59190ecd61435d19ba3515b876272aee7bd12298vboxsync *
c55c68b6a3324172e9dc207926215845880b0f90vboxsync * When distributing Covered Software, include this CDDL Header Notice in each file and include
c55c68b6a3324172e9dc207926215845880b0f90vboxsync * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL
c55c68b6a3324172e9dc207926215845880b0f90vboxsync * Header, with the fields enclosed by brackets [] replaced by your own identifying
c55c68b6a3324172e9dc207926215845880b0f90vboxsync * information: "Portions copyright [year] [name of copyright owner]".
c55c68b6a3324172e9dc207926215845880b0f90vboxsync *
c55c68b6a3324172e9dc207926215845880b0f90vboxsync * Copyright 2016 ForgeRock AS.
c55c68b6a3324172e9dc207926215845880b0f90vboxsync */
59190ecd61435d19ba3515b876272aee7bd12298vboxsync
59190ecd61435d19ba3515b876272aee7bd12298vboxsync#import <OCMock/OCMock.h>
59190ecd61435d19ba3515b876272aee7bd12298vboxsync#import <XCTest/XCTest.h>
772269936494ffaddd0750ba9e28e805ba81398cvboxsync
59190ecd61435d19ba3515b876272aee7bd12298vboxsync#import "FMDatabase.h"
59190ecd61435d19ba3515b876272aee7bd12298vboxsync#import "FRAError.h"
59190ecd61435d19ba3515b876272aee7bd12298vboxsync#import "FRAIdentity.h"
59190ecd61435d19ba3515b876272aee7bd12298vboxsync#import "FRAIdentityDatabase.h"
59190ecd61435d19ba3515b876272aee7bd12298vboxsync#import "FRAIdentityModel.h"
59190ecd61435d19ba3515b876272aee7bd12298vboxsync#import "FRAModelsFromDatabase.h"
59190ecd61435d19ba3515b876272aee7bd12298vboxsync#import "FRANotification.h"
59190ecd61435d19ba3515b876272aee7bd12298vboxsync#import "FRAOathMechanism.h"
78bdce9b23466b28a538c6e2f69c225d1393eb3bvboxsync#import "FRAPushMechanism.h"
78bdce9b23466b28a538c6e2f69c225d1393eb3bvboxsync#import "FRAFMDatabaseConnectionHelper.h"
59190ecd61435d19ba3515b876272aee7bd12298vboxsync
59190ecd61435d19ba3515b876272aee7bd12298vboxsyncstatic NSString * const ReadSchema = @"read_all schema";
59190ecd61435d19ba3515b876272aee7bd12298vboxsyncstatic NSString * const Issuer = @"issuer";
59190ecd61435d19ba3515b876272aee7bd12298vboxsyncstatic NSString * const AccountName = @"account name";
59190ecd61435d19ba3515b876272aee7bd12298vboxsyncstatic NSString * const ImageURL = @"image url";
59190ecd61435d19ba3515b876272aee7bd12298vboxsyncstatic NSString * const BackgroundColour = @"background colour";
59190ecd61435d19ba3515b876272aee7bd12298vboxsyncstatic NSString * const HMACOtpType = @"hotp";
59190ecd61435d19ba3515b876272aee7bd12298vboxsyncstatic NSString * const TimeOtpType = @"totp";
59190ecd61435d19ba3515b876272aee7bd12298vboxsyncstatic NSString * const PushType = @"push";
59190ecd61435d19ba3515b876272aee7bd12298vboxsyncstatic NSString * const UnknownType = @"unknown";
59190ecd61435d19ba3515b876272aee7bd12298vboxsyncstatic NSString * const Version = @"version";
bbc0a3fc49446bf8fa1fcfe669c10875701692bcvboxsyncstatic NSString * const MechanismUID = @"mechanism uid";
bbc0a3fc49446bf8fa1fcfe669c10875701692bcvboxsyncstatic NSString * const Options = @"{}";
59190ecd61435d19ba3515b876272aee7bd12298vboxsyncstatic NSString * const TimeReceived = @"500";
bbc0a3fc49446bf8fa1fcfe669c10875701692bcvboxsyncstatic NSString * const TimeExpired = @"time expired";
59190ecd61435d19ba3515b876272aee7bd12298vboxsyncstatic NSString * const Data = @"{\"message_id\":\"message id\", \"push_challenge\":\"challenge_data\",\"time_to_live\":\"60.0\"}";
59190ecd61435d19ba3515b876272aee7bd12298vboxsync
59190ecd61435d19ba3515b876272aee7bd12298vboxsync@interface FRAModelsFromDatabaseTest : XCTestCase
59190ecd61435d19ba3515b876272aee7bd12298vboxsync
59190ecd61435d19ba3515b876272aee7bd12298vboxsync@end
59190ecd61435d19ba3515b876272aee7bd12298vboxsync
59190ecd61435d19ba3515b876272aee7bd12298vboxsync@implementation FRAModelsFromDatabaseTest {
59190ecd61435d19ba3515b876272aee7bd12298vboxsync id mockSqlDatabase;
59190ecd61435d19ba3515b876272aee7bd12298vboxsync id mockIdentityDatabase;
59190ecd61435d19ba3515b876272aee7bd12298vboxsync id mockDatabase;
59190ecd61435d19ba3515b876272aee7bd12298vboxsync id mockError;
59190ecd61435d19ba3515b876272aee7bd12298vboxsync id mockQueryResults;
59190ecd61435d19ba3515b876272aee7bd12298vboxsync id mockIdentityModel;
59190ecd61435d19ba3515b876272aee7bd12298vboxsync}
59190ecd61435d19ba3515b876272aee7bd12298vboxsync
59190ecd61435d19ba3515b876272aee7bd12298vboxsync- (void)setUp {
a01baba2de7ca040689fb847c5e6b5e282e10fc4vboxsync [super setUp];
59190ecd61435d19ba3515b876272aee7bd12298vboxsync mockSqlDatabase = OCMClassMock([FRAFMDatabaseConnectionHelper class]);
59190ecd61435d19ba3515b876272aee7bd12298vboxsync mockIdentityDatabase = OCMClassMock([FRAIdentityDatabase class]);
59190ecd61435d19ba3515b876272aee7bd12298vboxsync mockDatabase = OCMClassMock([FMDatabase class]);
59190ecd61435d19ba3515b876272aee7bd12298vboxsync mockError = OCMClassMock([FRAError class]);
59190ecd61435d19ba3515b876272aee7bd12298vboxsync mockQueryResults = OCMClassMock([FMResultSet class]);
59190ecd61435d19ba3515b876272aee7bd12298vboxsync mockIdentityModel = OCMClassMock([FRAIdentityModel class]);
59190ecd61435d19ba3515b876272aee7bd12298vboxsync}
59190ecd61435d19ba3515b876272aee7bd12298vboxsync
59190ecd61435d19ba3515b876272aee7bd12298vboxsync- (void)tearDown {
59190ecd61435d19ba3515b876272aee7bd12298vboxsync [mockSqlDatabase stopMocking];
59190ecd61435d19ba3515b876272aee7bd12298vboxsync [mockIdentityDatabase stopMocking];
59190ecd61435d19ba3515b876272aee7bd12298vboxsync [mockDatabase stopMocking];
772269936494ffaddd0750ba9e28e805ba81398cvboxsync [mockError stopMocking];
59190ecd61435d19ba3515b876272aee7bd12298vboxsync [mockIdentityModel stopMocking];
[super tearDown];
}
- (void)testGetAllIdentitiesReturnsNilIfCannotGetReadAllSchema {
OCMStub([mockSqlDatabase readSchema:@"read_all" withError:nil]).andReturn(nil);
NSArray<FRAIdentity*>* identities = [FRAModelsFromDatabase getAllIdentitiesFrom:mockSqlDatabase including:mockIdentityDatabase identityModel:mockIdentityModel catchingErrorsWith:nil];
XCTAssertNil(identities);
}
- (void)testGetAllIdentitiesReturnsNilIfCannotGetDatabaseConnection {
OCMStub([mockSqlDatabase readSchema:@"read_all" withError:nil]).andReturn(ReadSchema);
OCMStub([mockSqlDatabase getConnectionWithError:nil]).andReturn(nil);
NSArray<FRAIdentity*>* identities = [FRAModelsFromDatabase getAllIdentitiesFrom:mockSqlDatabase including:mockIdentityDatabase identityModel:mockIdentityModel catchingErrorsWith:nil];
XCTAssertNil(identities);
}
- (void)testGetAllIdentitiesReturnsNilIfCannotExecuteQueryOnDatabase {
OCMStub([mockSqlDatabase readSchema:@"read_all" withError:nil]).andReturn(ReadSchema);
OCMStub([mockSqlDatabase getConnectionWithError:nil]).andReturn(mockDatabase);
OCMStub([mockDatabase executeQuery:ReadSchema]).andReturn(nil);
NSArray<FRAIdentity*>* identities = [FRAModelsFromDatabase getAllIdentitiesFrom:mockSqlDatabase including:mockIdentityDatabase identityModel:mockIdentityModel catchingErrorsWith:nil];
XCTAssertNil(identities);
OCMVerify([FRAError createErrorForLastFailure:mockDatabase withError:nil]);
}
- (void)testGetAllIdentitiesReturnsIdentityWithHMACOneTimePasswordMechanism {
OCMStub([mockSqlDatabase readSchema:@"read_all" withError:nil]).andReturn(ReadSchema);
OCMStub([mockSqlDatabase getConnectionWithError:nil]).andReturn(mockDatabase);
OCMStub([mockDatabase executeQuery:ReadSchema]).andReturn(mockQueryResults);
[self setUpDummyIdentity:HMACOtpType];
NSArray<FRAIdentity*>* identities = [FRAModelsFromDatabase getAllIdentitiesFrom:mockSqlDatabase including:mockIdentityDatabase identityModel:mockIdentityModel catchingErrorsWith:nil];
XCTAssertEqual(identities.count, 1);
FRAIdentity *identity = [identities objectAtIndex:0];
XCTAssertEqual(identity.issuer, Issuer);
XCTAssertEqual(identity.accountName, AccountName);
XCTAssertEqual(identity.backgroundColor, BackgroundColour);
XCTAssertEqual(identity.mechanisms.count, 1);
FRAOathMechanism *mechanism = (FRAOathMechanism*)[identity.mechanisms objectAtIndex:0];
XCTAssertEqual(mechanism.type, @"hotp");
}
- (void)testGetAllIdentitiesReturnsIdentityWithTimeOneTimePasswordMechanism {
OCMStub([mockSqlDatabase readSchema:@"read_all" withError:nil]).andReturn(ReadSchema);
OCMStub([mockSqlDatabase getConnectionWithError:nil]).andReturn(mockDatabase);
OCMStub([mockDatabase executeQuery:ReadSchema]).andReturn(mockQueryResults);
[self setUpDummyIdentity:TimeOtpType];
NSArray<FRAIdentity*>* identities = [FRAModelsFromDatabase getAllIdentitiesFrom:mockSqlDatabase including:mockIdentityDatabase identityModel:mockIdentityModel catchingErrorsWith:nil];
XCTAssertEqual(identities.count, 1);
FRAIdentity *identity = [identities objectAtIndex:0];
XCTAssertEqual(identity.issuer, Issuer);
XCTAssertEqual(identity.accountName, AccountName);
XCTAssertEqual(identity.backgroundColor, BackgroundColour);
XCTAssertEqual(identity.mechanisms.count, 1);
FRAOathMechanism *mechanism = (FRAOathMechanism*)[identity.mechanisms objectAtIndex:0];
XCTAssertEqual(mechanism.type, @"totp");
}
- (void)testGetAllIdentitiesReturnsIdentityWithPushMechanism {
OCMStub([mockSqlDatabase readSchema:@"read_all" withError:nil]).andReturn(ReadSchema);
OCMStub([mockSqlDatabase getConnectionWithError:nil]).andReturn(mockDatabase);
OCMStub([mockDatabase executeQuery:ReadSchema]).andReturn(mockQueryResults);
[self setUpDummyIdentity:PushType];
NSArray<FRAIdentity*>* identities = [FRAModelsFromDatabase getAllIdentitiesFrom:mockSqlDatabase including:mockIdentityDatabase identityModel:mockIdentityModel catchingErrorsWith:nil];
XCTAssertEqual(identities.count, 1);
FRAIdentity *identity = [identities objectAtIndex:0];
XCTAssertEqual(identity.issuer, Issuer);
XCTAssertEqual(identity.accountName, AccountName);
XCTAssertEqual(identity.backgroundColor, BackgroundColour);
XCTAssertEqual(identity.mechanisms.count, 1);
FRAPushMechanism *mechanism = (FRAPushMechanism *)[identity.mechanisms objectAtIndex:0];
FRANotification *notification = [mechanism.notifications objectAtIndex:0];
XCTAssertEqualObjects(notification.messageId, @"message id");
XCTAssertEqualObjects(notification.challenge, @"challenge_data");
XCTAssertEqualObjects(notification.timeReceived, [NSDate dateWithTimeIntervalSince1970:500]);
XCTAssertEqual(notification.timeToLive, (NSTimeInterval)60.0);
}
- (void)testGetAllIdentitiesThrowsExceptionForUnknownMechanismType {
OCMStub([mockSqlDatabase readSchema:@"read_all" withError:nil]).andReturn(ReadSchema);
OCMStub([mockSqlDatabase getConnectionWithError:nil]).andReturn(mockDatabase);
OCMStub([mockDatabase executeQuery:ReadSchema]).andReturn(mockQueryResults);
[self setUpDummyIdentity:UnknownType];
XCTAssertThrows([FRAModelsFromDatabase getAllIdentitiesFrom:mockSqlDatabase including:mockIdentityDatabase identityModel:mockIdentityModel catchingErrorsWith:nil]);
}
- (void)setUpDummyIdentity:(NSString *)type {
OCMExpect([mockQueryResults next]).andReturn(YES);
OCMStub([mockQueryResults stringForColumn:@"issuer"]).andReturn(Issuer);
OCMStub([mockQueryResults stringForColumn:@"accountName"]).andReturn(AccountName);
OCMStub([mockQueryResults stringForColumn:@"imageURL"]).andReturn(ImageURL);
OCMStub([mockQueryResults stringForColumn:@"bgColor"]).andReturn(BackgroundColour);
OCMStub([mockQueryResults stringForColumn:@"type"]).andReturn(type);
OCMStub([mockQueryResults stringForColumn:@"version"]).andReturn(Version);
OCMStub([mockQueryResults stringForColumn:@"mechanismUID"]).andReturn(MechanismUID);
OCMStub([mockQueryResults stringForColumn:@"options"]).andReturn(Options);
OCMStub([mockQueryResults stringForColumn:@"timeReceived"]).andReturn(TimeReceived);
OCMStub([mockQueryResults stringForColumn:@"timeExpired"]).andReturn(TimeExpired);
OCMStub([mockQueryResults stringForColumn:@"data"]).andReturn(Data);
OCMStub([mockQueryResults intForColumn:@"pending"]).andReturn(-1);
OCMStub([mockQueryResults intForColumn:@"approved"]).andReturn(0);
OCMExpect([mockQueryResults next]).andReturn(NO);
}
@end