Skip to content

PER-10096-archive-invitations #577

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
116 changes: 116 additions & 0 deletions src/app/auth/guards/auth.guard.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
import { TestBed } from '@angular/core/testing';
import {
Router,
UrlTree,
ActivatedRouteSnapshot,
RouterStateSnapshot,
} from '@angular/router';
import { AccountService } from '@shared/services/account/account.service';
import { AccountVO } from '@models/account-vo';
import { AuthGuard } from './auth.guard';

describe('AuthGuard', () => {
let guard: AuthGuard;
let accountServiceSpy: jasmine.SpyObj<AccountService>;
let routerSpy: jasmine.SpyObj<Router>;

beforeEach(() => {
accountServiceSpy = jasmine.createSpyObj('AccountService', [
'getAccount',
'hasOwnArchives',
]);

routerSpy = jasmine.createSpyObj('Router', ['createUrlTree', 'parseUrl']);

TestBed.configureTestingModule({
providers: [
AuthGuard,
{ provide: AccountService, useValue: accountServiceSpy },
{ provide: Router, useValue: routerSpy },
],
});

guard = TestBed.inject(AuthGuard);
});

function createMockRouteSnapshot(
query: Record<string, string> = {},
): ActivatedRouteSnapshot {
return {
queryParamMap: {
get: (key: string) => query[key] ?? null,
},
} as any;
}

it('should allow access if no account is found', () => {
accountServiceSpy.getAccount.and.returnValue(null);
const result = guard.canActivate(createMockRouteSnapshot(), {
url: '/signup',
} as RouterStateSnapshot);

expect(result).toBeTrue();
});

it('should redirect to /app/dialog:archives/pending if on /signup with invite params', () => {
accountServiceSpy.getAccount.and.returnValue(
new AccountVO({ accountId: 1 }),
);

const query = {
inviteCode: 'xyz',
fullName: 'Test User',
primaryEmail: '[email protected]',
};

const expectedTree = {} as UrlTree;
routerSpy.createUrlTree.and.returnValue(expectedTree);

const result = guard.canActivate(createMockRouteSnapshot(query), {
url: '/signup',
} as RouterStateSnapshot);

expect(routerSpy.createUrlTree).toHaveBeenCalledWith([
'/app',
{ outlets: { primary: 'private', dialog: 'archives/pending' } },
]);

expect(result).toBe(expectedTree);
});

it('should redirect to /app/private if account has own archives', async () => {
accountServiceSpy.getAccount.and.returnValue(
new AccountVO({ accountId: 1 }),
);
accountServiceSpy.hasOwnArchives.and.returnValue(Promise.resolve(true));

const expectedUrl = {} as UrlTree;
routerSpy.parseUrl.and.returnValue(expectedUrl);

const result = await guard.canActivate(createMockRouteSnapshot(), {
url: '/app/anything',
} as RouterStateSnapshot);

expect(accountServiceSpy.hasOwnArchives).toHaveBeenCalled();
expect(result).toBe(expectedUrl);
expect(routerSpy.parseUrl).toHaveBeenCalledWith('/app/private');
});

it('should redirect to /app/onboarding if account has no own archives', async () => {
accountServiceSpy.getAccount.and.returnValue(
new AccountVO({ accountId: 1 }),
);
accountServiceSpy.hasOwnArchives.and.returnValue(Promise.resolve(false));

const expectedUrl = {} as UrlTree;
routerSpy.parseUrl.and.returnValue(expectedUrl);

const result = await guard.canActivate(createMockRouteSnapshot(), {
url: '/app/anything',
} as RouterStateSnapshot);

expect(accountServiceSpy.hasOwnArchives).toHaveBeenCalled();
expect(result).toBe(expectedUrl);
expect(routerSpy.parseUrl).toHaveBeenCalledWith('/app/onboarding');
});
});
14 changes: 14 additions & 0 deletions src/app/auth/guards/auth.guard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,21 @@ export class AuthGuard {
| Promise<boolean | UrlTree>
| boolean
| UrlTree {
const account = this.account.getAccount();
const queryParams = route.queryParamMap;

const inviteCode = queryParams.get('inviteCode');
const fullName = queryParams.get('fullName');
const primaryEmail = queryParams.get('primaryEmail');
const isInviteSignup = inviteCode && fullName && primaryEmail;

if (this.account.getAccount()?.accountId) {
if (state.url.includes('/signup') && isInviteSignup) {
return this.router.createUrlTree([
'/app',
{ outlets: { primary: 'private', dialog: 'archives/pending' } },
]);
}
return this.account.hasOwnArchives().then((hasArchives) => {
if (hasArchives) {
return this.router.parseUrl('/app/private');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
<div class="panel" #panel>
<ng-container [ngSwitch]="activeTab">
<ng-container *ngSwitchCase="'switch'">
<div class="panel-title">({{ archives.length }}) Archives</div>
<div class="panel-title">({{ archives?.length }}) Archives</div>
<pr-archive-small
*ngFor="let archive of archives"
(archiveClick)="onArchiveClick(archive)"
Expand All @@ -45,7 +45,7 @@
archive.accessRole.includes('owner') &&
currentArchive.archiveId !== archive.archiveId &&
account.defaultArchiveId !== archive.archiveId &&
archives.length > 1
archives?.length > 1
"
removeText="Delete Archive"
(removeClick)="onArchiveDeleteClick(archive)"
Expand All @@ -58,9 +58,9 @@
</ng-container>
<ng-container *ngSwitchCase="'pending'">
<div class="panel-title">
({{ pendingArchives.length }}) Pending Archives
({{ pendingArchives?.length }}) Pending Archives
</div>
<div *ngIf="!pendingArchives.length">No pending archives</div>
<div *ngIf="!pendingArchives?.length">No pending archives</div>
<pr-archive-small
*ngFor="let archive of pendingArchives"
[largeOnDesktop]="true"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
import {
ComponentFixture,
TestBed,
fakeAsync,
tick,
} from '@angular/core/testing';
import { ArchiveVO, AccountVO } from '@models';
import { AccountService } from '@shared/services/account/account.service';
import { ApiService } from '@shared/services/api/api.service';
import { MessageService } from '@shared/services/message/message.service';
import { PromptService } from '@shared/services/prompt/prompt.service';
import { UntypedFormBuilder } from '@angular/forms';
import { Router, ActivatedRoute } from '@angular/router';
import { DIALOG_DATA, DialogRef } from '@angular/cdk/dialog';
import { MyArchivesDialogComponent } from './my-archives-dialog.component';

describe('MyArchivesDialogComponent', () => {
let component: MyArchivesDialogComponent;
let fixture: ComponentFixture<MyArchivesDialogComponent>;
let accountServiceSpy: jasmine.SpyObj<AccountService>;
let apiServiceSpy: jasmine.SpyObj<ApiService>;
let promptServiceSpy: jasmine.SpyObj<PromptService>;
let messageServiceSpy: jasmine.SpyObj<MessageService>;
let dialogRefSpy: jasmine.SpyObj<DialogRef>;
let dialogData;

beforeEach(async () => {
accountServiceSpy = jasmine.createSpyObj('AccountService', [
'refreshArchives',
'getAccount',
'getArchive',
'getArchives',
'changeArchive',
'updateAccount',
]);

apiServiceSpy = jasmine.createSpyObj('ApiService', ['archive'], {
archive: jasmine.createSpyObj('archive', ['delete', 'accept', 'decline']),
});

promptServiceSpy = jasmine.createSpyObj('PromptService', ['confirm']);
messageServiceSpy = jasmine.createSpyObj('MessageService', ['showError']);
dialogRefSpy = jasmine.createSpyObj('DialogRef', ['close']);

await TestBed.configureTestingModule({
declarations: [MyArchivesDialogComponent],
providers: [
{ provide: AccountService, useValue: accountServiceSpy },
{ provide: ApiService, useValue: apiServiceSpy },
{ provide: PromptService, useValue: promptServiceSpy },
{ provide: MessageService, useValue: messageServiceSpy },
{ provide: DialogRef, useValue: dialogRefSpy },
{
provide: ActivatedRoute,
useValue: { snapshot: { root: { params: {}, children: [] } } },
},
{
provide: Router,
useValue: { routerState: { snapshot: { root: {} } } },
},
{
provide: DIALOG_DATA,
useValue: {},
},

UntypedFormBuilder,
],
}).compileComponents();

fixture = TestBed.createComponent(MyArchivesDialogComponent);
component = fixture.componentInstance;

const mockAccount: AccountVO = new AccountVO({ defaultArchiveId: '123' });
const mockCurrentArchive: ArchiveVO = new ArchiveVO({
archiveId: '123',
fullName: 'Test Archive',
accessRole: 'access.role.owner',
});

accountServiceSpy.getAccount.and.returnValue(mockAccount);
accountServiceSpy.getArchive.and.returnValue(mockCurrentArchive);
accountServiceSpy.getArchives.and.returnValue([
new ArchiveVO({
archiveId: '123',
fullName: 'Test Archive',
status: 'status.generic.ok',
accessRole: 'access.role.owner',
}),
]);
accountServiceSpy.refreshArchives.and.returnValue(
Promise.resolve([new ArchiveVO({ archiveId: 1 })]),
);

fixture.detectChanges();
});

it('should initialize and categorize archives correctly', async () => {
await component.ngOnInit();

expect(component.archives?.length).toBe(1);
expect(component.pendingArchives?.length).toBe(0);
});

it('should switch tabs when setTab is called', () => {
component.setTab('pending');

expect(component.activeTab).toBe('pending');
});

it('should close dialog onDoneClick', () => {
component.onDoneClick();

expect(dialogRefSpy.close).toHaveBeenCalled();
});

it('should initialize and separate pending/regular archives correctly', async () => {
const mockAccount = new AccountVO({ defaultArchiveId: '111' });
const mockCurrentArchive = new ArchiveVO({
archiveId: '111',
fullName: 'Current Archive',
});

const pending = new ArchiveVO({
archiveId: '222',
fullName: 'Pending Archive',
status: 'status.generic.pending',
});

const active = new ArchiveVO({
archiveId: '333',
fullName: 'Active Archive',
status: 'status.generic.ok',
});

accountServiceSpy.refreshArchives.and.returnValue(
Promise.resolve([new ArchiveVO({})]),
);
accountServiceSpy.getAccount.and.returnValue(mockAccount);
accountServiceSpy.getArchive.and.returnValue(mockCurrentArchive);
accountServiceSpy.getArchives.and.returnValue([pending, active]);

await component.ngOnInit();

expect(accountServiceSpy.refreshArchives).toHaveBeenCalled();
expect(component.account).toEqual(mockAccount);
expect(component.currentArchive).toEqual(mockCurrentArchive);
expect(component.pendingArchives.length).toBe(1);
expect(component.archives.length).toBe(1);
expect(component.pendingArchives[0].archiveId).toBe('222');
expect(component.archives[0].archiveId).toBe('333');
});
});
Loading