FRAModelsFromDatabaseTest.m revision f877f6ca2428244a6d0954a1dbef471577b32c60
1685N/A/*
2362N/A * The contents of this file are subject to the terms of the Common Development and
1685N/A * Distribution License (the License). You may not use this file except in compliance with the
1685N/A * License.
1685N/A *
1685N/A * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the
1685N/A * specific language governing permission and limitations under the License.
1685N/A *
1685N/A * When distributing Covered Software, include this CDDL Header Notice in each file and include
1685N/A * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL
1685N/A * Header, with the fields enclosed by brackets [] replaced by your own identifying
1685N/A * information: "Portions copyright [year] [name of copyright owner]".
1685N/A *
1685N/A * Copyright 2016 ForgeRock AS.
1685N/A */
1685N/A
1685N/A#import <OCMock/OCMock.h>
1685N/A#import <XCTest/XCTest.h>
2362N/A
2362N/A#import "FMDatabase.h"
2362N/A#import "FRAError.h"
1685N/A#import "FRAIdentity.h"
1685N/A#import "FRAIdentityDatabase.h"
1685N/A#import "FRAIdentityModel.h"
1685N/A#import "FRAModelsFromDatabase.h"
1685N/A#import "FRANotification.h"
1685N/A#import "FRAOathMechanism.h"
1685N/A#import "FRAPushMechanism.h"
1685N/A#import "FRAFMDatabaseConnectionHelper.h"
1685N/A
1685N/Astatic NSString * const ReadSchema = @"read_all schema";
1685N/Astatic NSString * const Issuer = @"issuer";
1685N/Astatic NSString * const AccountName = @"account name";
1685N/Astatic NSString * const ImageURL = @"image url";
1685N/Astatic NSString * const BackgroundColour = @"background colour";
1685N/Astatic NSString * const HMACOtpType = @"hotp";
1685N/Astatic NSString * const TimeOtpType = @"totp";
1685N/Astatic NSString * const PushType = @"push";
1685N/Astatic NSString * const UnknownType = @"unknown";
1685N/Astatic NSString * const Version = @"version";
1685N/Astatic NSString * const MechanismUID = @"mechanism uid";
1685N/Astatic NSString * const Options = @"{}";
1685N/Astatic NSString * const TimeReceived = @"500";
1685N/Astatic NSString * const TimeExpired = @"time expired";
1685N/Astatic NSString * const Data = @"{\"message_id\":\"message id\", \"push_challenge\":\"challenge_data\",\"time_to_live\":\"60.0\"}";
1685N/A
1685N/A@interface FRAModelsFromDatabaseTest : XCTestCase
1685N/A
1685N/A@end
1685N/A
1685N/A@implementation FRAModelsFromDatabaseTest {
1685N/A id mockSqlDatabase;
1685N/A id mockIdentityDatabase;
1685N/A id mockDatabase;
1685N/A id mockError;
1685N/A id mockQueryResults;
1685N/A id mockIdentityModel;
1685N/A}
1685N/A
1685N/A- (void)setUp {
1685N/A [super setUp];
1685N/A mockSqlDatabase = OCMClassMock([FRAFMDatabaseConnectionHelper class]);
1685N/A mockIdentityDatabase = OCMClassMock([FRAIdentityDatabase class]);
mockDatabase = OCMClassMock([FMDatabase class]);
mockError = OCMClassMock([FRAError class]);
mockQueryResults = OCMClassMock([FMResultSet class]);
mockIdentityModel = OCMClassMock([FRAIdentityModel class]);
}
- (void)tearDown {
[mockSqlDatabase stopMocking];
[mockIdentityDatabase stopMocking];
[mockDatabase stopMocking];
[mockError stopMocking];
[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