78c07714ec1113f7f21c75b818f2bf6a7021618aDiego Colantoni * The contents of this file are subject to the terms of the Common Development and
78c07714ec1113f7f21c75b818f2bf6a7021618aDiego Colantoni * Distribution License (the License). You may not use this file except in compliance with the
78c07714ec1113f7f21c75b818f2bf6a7021618aDiego Colantoni * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the
78c07714ec1113f7f21c75b818f2bf6a7021618aDiego Colantoni * specific language governing permission and limitations under the License.
78c07714ec1113f7f21c75b818f2bf6a7021618aDiego Colantoni * When distributing Covered Software, include this CDDL Header Notice in each file and include
78c07714ec1113f7f21c75b818f2bf6a7021618aDiego Colantoni * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL
78c07714ec1113f7f21c75b818f2bf6a7021618aDiego Colantoni * Header, with the fields enclosed by brackets [] replaced by your own identifying
78c07714ec1113f7f21c75b818f2bf6a7021618aDiego Colantoni * information: "Portions copyright [year] [name of copyright owner]".
78c07714ec1113f7f21c75b818f2bf6a7021618aDiego Colantoni * Copyright 2016 ForgeRock AS.
78c07714ec1113f7f21c75b818f2bf6a7021618aDiego Colantoni#import "FRAAccountsTableViewController.h"
6a2ae9c7fb4d2c40d75cab0edaf940f22c18224fDiego Colantoni#import "FRAAccountTableViewCell.h"
6a2ae9c7fb4d2c40d75cab0edaf940f22c18224fDiego Colantoni#import "FRAApplicationAssembly.h"
6a2ae9c7fb4d2c40d75cab0edaf940f22c18224fDiego Colantoni#import "FRAHotpOathMechanism.h"
78c07714ec1113f7f21c75b818f2bf6a7021618aDiego Colantoni#import "FRAIdentityModel.h"
6a2ae9c7fb4d2c40d75cab0edaf940f22c18224fDiego Colantoni#import "FRAIdentity.h"
caa9e77dc369fea8df9ae2c598d3c83b7214c1cfDiego Colantoni#import "FRAModelsFromDatabase.h"
78c07714ec1113f7f21c75b818f2bf6a7021618aDiego Colantoni#import "FRAModelUtils.h"
78c07714ec1113f7f21c75b818f2bf6a7021618aDiego Colantoni#import "FRANotification.h"
caa9e77dc369fea8df9ae2c598d3c83b7214c1cfDiego Colantoni#import "FRAPushMechanism.h"
33f578498e5bdaf445a4f509ac419f0a23148f61Diego Colantoni#import "FRATotpOathMechanism.h"
78c07714ec1113f7f21c75b818f2bf6a7021618aDiego Colantoni@interface FRAAccountsTableViewControllerTests : XCTestCase
78c07714ec1113f7f21c75b818f2bf6a7021618aDiego Colantoni@implementation FRAAccountsTableViewControllerTests {
6a2ae9c7fb4d2c40d75cab0edaf940f22c18224fDiego Colantoni FRAAccountsTableViewController *accountsController;
78c07714ec1113f7f21c75b818f2bf6a7021618aDiego Colantoni mockModelsFromDatabase = OCMClassMock([FRAModelsFromDatabase class]);
78c07714ec1113f7f21c75b818f2bf6a7021618aDiego Colantoni OCMStub([mockModelsFromDatabase allIdentitiesWithDatabase:[OCMArg any] identityDatabase:[OCMArg any] identityModel:[OCMArg any] error:[OCMArg anyObjectRef]]).andReturn(@[]);
6a2ae9c7fb4d2c40d75cab0edaf940f22c18224fDiego Colantoni // create a mock instance of the identity database
78c07714ec1113f7f21c75b818f2bf6a7021618aDiego Colantoni mockIdentityModel = OCMClassMock([FRAIdentityModel class]);
33f578498e5bdaf445a4f509ac419f0a23148f61Diego Colantoni // patch Typhoon assembly to mock account controller database dependency
6a2ae9c7fb4d2c40d75cab0edaf940f22c18224fDiego Colantoni FRAApplicationAssembly *assembly = (FRAApplicationAssembly *) [TyphoonComponentFactory defaultFactory];
6a2ae9c7fb4d2c40d75cab0edaf940f22c18224fDiego Colantoni TyphoonPatcher *patcher = [[TyphoonPatcher alloc] init];
6a2ae9c7fb4d2c40d75cab0edaf940f22c18224fDiego Colantoni [patcher patchDefinitionWithSelector:@selector(identityModel) withObject:^id{
33f578498e5bdaf445a4f509ac419f0a23148f61Diego Colantoni [assembly attachDefinitionPostProcessor:patcher];
78c07714ec1113f7f21c75b818f2bf6a7021618aDiego Colantoni // load accounts controller from storyboard
78c07714ec1113f7f21c75b818f2bf6a7021618aDiego Colantoni UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]];
78c07714ec1113f7f21c75b818f2bf6a7021618aDiego Colantoni accountsController = [storyboard instantiateViewControllerWithIdentifier:@"AccountsTableViewController"];
caa9e77dc369fea8df9ae2c598d3c83b7214c1cfDiego Colantoni if (floor(NSFoundationVersionNumber) <= NSFoundationVersionNumber_iOS_8_4) {
6a2ae9c7fb4d2c40d75cab0edaf940f22c18224fDiego Colantoni [accountsController view]; // force IBOutlets etc to be initialized
caa9e77dc369fea8df9ae2c598d3c83b7214c1cfDiego Colantoni [accountsController loadViewIfNeeded]; // force IBOutlets etc to be initialized
a4c96a094a3d715ef793b60cfabfdb2ee088b319Diego Colantoni OCMStub([mockIdentityModel identities]).andReturn(@[]);
a4c96a094a3d715ef793b60cfabfdb2ee088b319Diego Colantoni NSInteger sections = [accountsController numberOfSectionsInTableView:accountsController.tableView];
a4c96a094a3d715ef793b60cfabfdb2ee088b319Diego Colantoni NSInteger rows = [accountsController tableView:accountsController.tableView numberOfRowsInSection:0];
caa9e77dc369fea8df9ae2c598d3c83b7214c1cfDiego Colantoni- (void)testHasOneCellPerDatabaseIdentitySortedByIssuerThenAccountName {
caa9e77dc369fea8df9ae2c598d3c83b7214c1cfDiego Colantoni FRAIdentity *firstIdentity = [FRAIdentity identityWithDatabase:nil identityModel:mockIdentityModel accountName:@"Alice" issuer:@"Issuer_1" image:nil backgroundColor:nil];
caa9e77dc369fea8df9ae2c598d3c83b7214c1cfDiego Colantoni FRAIdentity *secondIdentity = [FRAIdentity identityWithDatabase:nil identityModel:mockIdentityModel accountName:@"Bob" issuer:@"Issuer_1" image:nil backgroundColor:nil];
caa9e77dc369fea8df9ae2c598d3c83b7214c1cfDiego Colantoni FRAIdentity *thirdIdentity = [FRAIdentity identityWithDatabase:nil identityModel:mockIdentityModel accountName:@"Alice" issuer:@"Issuer_2" image:nil backgroundColor:nil];
caa9e77dc369fea8df9ae2c598d3c83b7214c1cfDiego Colantoni NSArray *identities = @[secondIdentity, thirdIdentity, firstIdentity]; // NB. Identities aren't sorted
caa9e77dc369fea8df9ae2c598d3c83b7214c1cfDiego Colantoni OCMStub([mockIdentityModel identities]).andReturn(identities);
caa9e77dc369fea8df9ae2c598d3c83b7214c1cfDiego Colantoni NSInteger sections = [accountsController numberOfSectionsInTableView:accountsController.tableView];
caa9e77dc369fea8df9ae2c598d3c83b7214c1cfDiego Colantoni NSInteger rows = [accountsController tableView:accountsController.tableView numberOfRowsInSection:0];
caa9e77dc369fea8df9ae2c598d3c83b7214c1cfDiego Colantoni [self assertCellForRow:0 isShowingIdentity:firstIdentity];
caa9e77dc369fea8df9ae2c598d3c83b7214c1cfDiego Colantoni [self assertCellForRow:1 isShowingIdentity:secondIdentity];
caa9e77dc369fea8df9ae2c598d3c83b7214c1cfDiego Colantoni [self assertCellForRow:2 isShowingIdentity:thirdIdentity];
caa9e77dc369fea8df9ae2c598d3c83b7214c1cfDiego Colantoni- (void)testShowsNotificationIconWithBadgeForPushMechanism {
caa9e77dc369fea8df9ae2c598d3c83b7214c1cfDiego Colantoni FRAIdentity *identity = [FRAIdentity identityWithDatabase:nil identityModel:nil accountName:@"Alice" issuer:@"Issuer" image:nil backgroundColor:nil];
caa9e77dc369fea8df9ae2c598d3c83b7214c1cfDiego Colantoni FRAPushMechanism *pushMechanism = [[FRAPushMechanism alloc] initWithDatabase:nil identityModel:nil ];
caa9e77dc369fea8df9ae2c598d3c83b7214c1cfDiego Colantoni [pushMechanism addNotification:[self dummyNotification] error:nil];
caa9e77dc369fea8df9ae2c598d3c83b7214c1cfDiego Colantoni [pushMechanism addNotification:[self dummyNotification] error:nil];
caa9e77dc369fea8df9ae2c598d3c83b7214c1cfDiego Colantoni [identity addMechanism:pushMechanism error:nil];
caa9e77dc369fea8df9ae2c598d3c83b7214c1cfDiego Colantoni XCTAssertNotNil([identity mechanismOfClass:[FRAPushMechanism class]]);
caa9e77dc369fea8df9ae2c598d3c83b7214c1cfDiego Colantoni OCMStub([mockIdentityModel identities]).andReturn(identities);
caa9e77dc369fea8df9ae2c598d3c83b7214c1cfDiego Colantoni FRAAccountTableViewCell *cell = [self cellForRow:0];
caa9e77dc369fea8df9ae2c598d3c83b7214c1cfDiego Colantoni XCTAssertFalse(cell.firstMechanismIcon.hidden);
caa9e77dc369fea8df9ae2c598d3c83b7214c1cfDiego Colantoni XCTAssertTrue(cell.secondMechanismIcon.hidden);
caa9e77dc369fea8df9ae2c598d3c83b7214c1cfDiego Colantoni XCTAssertEqualObjects(cell.notificationsBadge.text, @"2");
caa9e77dc369fea8df9ae2c598d3c83b7214c1cfDiego Colantoni- (void)testShowsTokenIconWithoutBadgeForOathMechanism {
caa9e77dc369fea8df9ae2c598d3c83b7214c1cfDiego Colantoni FRAIdentity *identity = [FRAIdentity identityWithDatabase:nil
caa9e77dc369fea8df9ae2c598d3c83b7214c1cfDiego Colantoni FRAHotpOathMechanism *mechanism = [modelUtils demoOathMechanism];
caa9e77dc369fea8df9ae2c598d3c83b7214c1cfDiego Colantoni [identity addMechanism:mechanism error:nil];
caa9e77dc369fea8df9ae2c598d3c83b7214c1cfDiego Colantoni XCTAssertNotNil([identity mechanismOfClass:[FRAHotpOathMechanism class]]);
caa9e77dc369fea8df9ae2c598d3c83b7214c1cfDiego Colantoni OCMStub([mockIdentityModel identities]).andReturn(identities);
caa9e77dc369fea8df9ae2c598d3c83b7214c1cfDiego Colantoni FRAAccountTableViewCell *cell = [self cellForRow:0];
caa9e77dc369fea8df9ae2c598d3c83b7214c1cfDiego Colantoni XCTAssertFalse(cell.firstMechanismIcon.hidden);
caa9e77dc369fea8df9ae2c598d3c83b7214c1cfDiego Colantoni XCTAssertTrue(cell.secondMechanismIcon.hidden);
caa9e77dc369fea8df9ae2c598d3c83b7214c1cfDiego Colantoni XCTAssertEqualObjects(cell.notificationsBadge.text, @"0");
caa9e77dc369fea8df9ae2c598d3c83b7214c1cfDiego Colantoni XCTAssertTrue(cell.notificationsBadge.hidesWhenZero);
78c07714ec1113f7f21c75b818f2bf6a7021618aDiego Colantoni- (void)testCanShowOathMechanismAndPushMechanism {
5d37db6a3aca50ba14cba8909d3ae44f7d43e407Ken Stubbings FRAIdentity *identity = [FRAIdentity identityWithDatabase:nil
78c07714ec1113f7f21c75b818f2bf6a7021618aDiego Colantoni FRAHotpOathMechanism *mechanism = [modelUtils demoOathMechanism];
7c51d54f23c5581d2cf894f9eafb9798e3febd22Diego Colantoni [identity addMechanism:mechanism error:nil];
78c07714ec1113f7f21c75b818f2bf6a7021618aDiego Colantoni FRAPushMechanism *pushMechanism = [[FRAPushMechanism alloc] initWithDatabase:nil identityModel:nil ];
78c07714ec1113f7f21c75b818f2bf6a7021618aDiego Colantoni [pushMechanism addNotification:[self pendingNotification] error:nil];
78c07714ec1113f7f21c75b818f2bf6a7021618aDiego Colantoni [identity addMechanism:pushMechanism error:nil];
78c07714ec1113f7f21c75b818f2bf6a7021618aDiego Colantoni XCTAssertNotNil([identity mechanismOfClass:[FRAHotpOathMechanism class]]);
78c07714ec1113f7f21c75b818f2bf6a7021618aDiego Colantoni XCTAssertNotNil([identity mechanismOfClass:[FRAPushMechanism class]]);
33f578498e5bdaf445a4f509ac419f0a23148f61Diego Colantoni OCMStub([mockIdentityModel identities]).andReturn(identities);
33f578498e5bdaf445a4f509ac419f0a23148f61Diego Colantoni FRAAccountTableViewCell *cell = [self cellForRow:0];
33f578498e5bdaf445a4f509ac419f0a23148f61Diego Colantoni XCTAssertFalse(cell.firstMechanismIcon.hidden);
33f578498e5bdaf445a4f509ac419f0a23148f61Diego Colantoni XCTAssertFalse(cell.secondMechanismIcon.hidden);
33f578498e5bdaf445a4f509ac419f0a23148f61Diego Colantoni XCTAssertEqualObjects(cell.notificationsBadge.text, @"1");
33f578498e5bdaf445a4f509ac419f0a23148f61Diego Colantoni XCTAssertTrue(cell.notificationsBadge.hidesWhenZero);
33f578498e5bdaf445a4f509ac419f0a23148f61Diego Colantoni return [FRANotification notificationWithDatabase:nil
33f578498e5bdaf445a4f509ac419f0a23148f61Diego Colantoni return [FRANotification notificationWithDatabase:nil
33f578498e5bdaf445a4f509ac419f0a23148f61Diego Colantoni- (FRAAccountTableViewCell *)cellForRow:(NSInteger)row {
33f578498e5bdaf445a4f509ac419f0a23148f61Diego Colantoni NSIndexPath *indexPath = [NSIndexPath indexPathForRow:row inSection:0];
2708b42676edf3d2e8f85b3c22b9e3be3cf43eb8Diego Colantoni return (FRAAccountTableViewCell*) [accountsController tableView:accountsController.tableView cellForRowAtIndexPath:indexPath];
2708b42676edf3d2e8f85b3c22b9e3be3cf43eb8Diego Colantoni- (void)assertCellForRow:(NSInteger)row isShowingIdentity:(FRAIdentity*)identity {
2708b42676edf3d2e8f85b3c22b9e3be3cf43eb8Diego Colantoni FRAAccountTableViewCell *cell = [self cellForRow:row];
2708b42676edf3d2e8f85b3c22b9e3be3cf43eb8Diego Colantoni XCTAssertEqualObjects(identity.issuer, cell.issuer.text);