Skip to content

Commit

Permalink
Redirect Static Apps to Default File (#163)
Browse files Browse the repository at this point in the history
* Redirect Static Apps to Default File

* Fix tests
  • Loading branch information
huntharo authored Dec 3, 2021
1 parent 80f8272 commit c328480
Show file tree
Hide file tree
Showing 5 changed files with 242 additions and 38 deletions.
4 changes: 2 additions & 2 deletions packages/microapps-datalib/src/models/application.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ describe('application records', () => {
IntegrationID: 'abcd',
SemVer: '3.2.1-beta0',
Status: 'deployed',
Type: 'next.js',
Type: 'lambda',
});
await version.Save(dbManager);

Expand Down Expand Up @@ -176,7 +176,7 @@ describe('application records', () => {
IntegrationID: 'abcd',
SemVer: '3.2.1-beta0',
Status: 'deployed',
Type: 'next.js',
Type: 'lambda',
});
await version.Save(dbManager);

Expand Down
12 changes: 6 additions & 6 deletions packages/microapps-datalib/src/models/version.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ describe('version records', () => {
version.AppName = 'Cat';
version.SemVer = '1.2.3-Beta4';
version.Status = 'pending';
version.Type = 'type';
version.Type = 'lambda';
version.DefaultFile = 'index.html';
version.IntegrationID = 'abcd';

Expand All @@ -45,7 +45,7 @@ describe('version records', () => {
expect(Item?.AppName).toBe('cat');
expect(Item?.SemVer).toBe('1.2.3-Beta4');
expect(Item?.Status).toBe('pending');
expect(Item?.Type).toBe('type');
expect(Item?.Type).toBe('lambda');
expect(Item?.DefaultFile).toBe('index.html');
expect(Item?.IntegrationID).toBe('abcd');
});
Expand All @@ -55,7 +55,7 @@ describe('version records', () => {
version.AppName = 'Dog';
version.SemVer = '1.2.3-Beta5';
version.Status = 'pending';
version.Type = 'type';
version.Type = 'lambda';
version.DefaultFile = 'index.html';
version.IntegrationID = 'abcd';

Expand All @@ -65,7 +65,7 @@ describe('version records', () => {
version.AppName = 'Dog';
version.SemVer = '1.2.3-Beta6';
version.Status = 'pending';
version.Type = 'type';
version.Type = 'lambda';
version.DefaultFile = 'index.html';
version.IntegrationID = 'abcd';

Expand Down Expand Up @@ -98,7 +98,7 @@ describe('version records', () => {
version.AppName = 'Frog';
version.SemVer = '2.2.3-Beta5';
version.Status = 'pending';
version.Type = 'type';
version.Type = 'lambda';
version.DefaultFile = 'index.html';
version.IntegrationID = 'abcd';

Expand All @@ -108,7 +108,7 @@ describe('version records', () => {
version.AppName = 'Frog';
version.SemVer = '2.2.3-Beta6';
version.Status = 'pending';
version.Type = 'type';
version.Type = 'lambda';
version.DefaultFile = 'index.html';
version.IntegrationID = 'abcd';

Expand Down
12 changes: 7 additions & 5 deletions packages/microapps-datalib/src/models/version.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,14 @@ export type VersionStatus =
| 'routed'
| 'deployed';

export type AppTypes = 'static' | 'lambda';

export interface IVersionRecord {
PK: string;
SK: string;
AppName: string;
SemVer: string;
Type: string;
Type: 'lambda' | 'static';
Status: VersionStatus;
DefaultFile: string;
IntegrationID: string;
Expand Down Expand Up @@ -84,7 +86,7 @@ export class Version implements IVersionRecord {
private _keyBy: SaveBy;
private _appName: string | undefined;
private _semVer: string | undefined;
private _type: string | undefined;
private _type: AppTypes | undefined;
private _status: VersionStatus;
private _defaultFile: string;
private _integrationID: string;
Expand Down Expand Up @@ -146,10 +148,10 @@ export class Version implements IVersionRecord {
this._semVer = value;
}

public get Type(): string {
return this._type as string;
public get Type(): AppTypes {
return this._type as AppTypes;
}
public set Type(value: string) {
public set Type(value: AppTypes) {
this._type = value;
}

Expand Down
130 changes: 122 additions & 8 deletions packages/microapps-router/src/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,16 @@ describe('router', () => {
AppName: 'Bat',
DefaultFile: 'bat.html',
IntegrationID: 'abcd',
SemVer: '3.2.1-beta0',
SemVer: '3.2.1-beta.1',
Status: 'deployed',
Type: 'next.js',
Type: 'lambda',
});
await version.Save(dbManager);

const rules = new Rules({
AppName: 'Bat',
Version: 0,
RuleSet: { default: { SemVer: '3.2.1-beta0', AttributeName: '', AttributeValue: '' } },
RuleSet: { default: { SemVer: '3.2.1-beta.1', AttributeName: '', AttributeValue: '' } },
});
await rules.Save(dbManager);

Expand All @@ -61,7 +61,121 @@ describe('router', () => {
expect(response).toBeDefined();
expect(response).toHaveProperty('body');
expect(response.body?.length).toBeGreaterThan(80);
expect(response.body).toContain('<iframe src="/bat/3.2.1-beta0/bat.html" seamless');
expect(response.body).toContain('<iframe src="/bat/3.2.1-beta.1/bat.html" seamless');
});

it('static app - request to app/x.y.z should redirect to defaultFile', async () => {
const app = new Application({
AppName: 'Bat',
DisplayName: 'Bat App',
});
await app.Save(dbManager);

const version = new Version({
AppName: 'Bat',
DefaultFile: 'bat.html',
IntegrationID: 'abcd',
SemVer: '3.2.1-beta.1',
Status: 'deployed',
Type: 'static',
});
await version.Save(dbManager);

const rules = new Rules({
AppName: 'Bat',
Version: 0,
RuleSet: { default: { SemVer: '3.2.1-beta.1', AttributeName: '', AttributeValue: '' } },
});
await rules.Save(dbManager);

// Call the handler
const response = await handler(
{ rawPath: '/bat/3.2.1-beta.1' } as lambda.APIGatewayProxyEventV2,
{} as lambda.Context,
);

expect(response).toHaveProperty('statusCode');
expect(response.statusCode).toBe(302);
expect(response).toBeDefined();
expect(response.headers).toBeDefined();
expect(response.headers).toHaveProperty('Location');
expect(response.headers?.Location).toContain('/bat/3.2.1-beta.1/bat.html');
});

it('static app - request to app/x.y.z/ should redirect to defaultFile', async () => {
const app = new Application({
AppName: 'Bat',
DisplayName: 'Bat App',
});
await app.Save(dbManager);

const version = new Version({
AppName: 'Bat',
DefaultFile: 'bat.html',
IntegrationID: 'abcd',
SemVer: '3.2.1-beta.1',
Status: 'deployed',
Type: 'static',
});
await version.Save(dbManager);

const rules = new Rules({
AppName: 'Bat',
Version: 0,
RuleSet: { default: { SemVer: '3.2.1-beta.1', AttributeName: '', AttributeValue: '' } },
});
await rules.Save(dbManager);

// Call the handler
const response = await handler(
{ rawPath: '/bat/3.2.1-beta.1/' } as lambda.APIGatewayProxyEventV2,
{} as lambda.Context,
);

expect(response).toHaveProperty('statusCode');
expect(response.statusCode).toBe(302);
expect(response).toBeDefined();
expect(response.headers).toBeDefined();
expect(response.headers).toHaveProperty('Location');
expect(response.headers?.Location).toContain('/bat/3.2.1-beta.1/bat.html');
});

it('static app - request to app/notVersion should load app frame with defaultFile', async () => {
const app = new Application({
AppName: 'Bat',
DisplayName: 'Bat App',
});
await app.Save(dbManager);

const version = new Version({
AppName: 'Bat',
DefaultFile: 'bat.html',
IntegrationID: 'abcd',
SemVer: '3.2.1-beta.1',
Status: 'deployed',
Type: 'static',
});
await version.Save(dbManager);

const rules = new Rules({
AppName: 'Bat',
Version: 0,
RuleSet: { default: { SemVer: '3.2.1-beta.1', AttributeName: '', AttributeValue: '' } },
});
await rules.Save(dbManager);

// Call the handler
const response = await handler(
{ rawPath: '/bat/notVersion' } as lambda.APIGatewayProxyEventV2,
{} as lambda.Context,
);

expect(response).toHaveProperty('statusCode');
expect(response.statusCode).toBe(200);
expect(response).toBeDefined();
expect(response).toHaveProperty('body');
expect(response.body?.length).toBeGreaterThan(80);
expect(response.body).toContain('<iframe src="/bat/3.2.1-beta.1/bat.html" seamless');
});

it('should serve appframe with no default file', async () => {
Expand All @@ -77,7 +191,7 @@ describe('router', () => {
IntegrationID: 'abcd',
SemVer: '3.2.1-beta1',
Status: 'deployed',
Type: 'next.js',
Type: 'lambda',
});
await version.Save(dbManager);

Expand Down Expand Up @@ -115,7 +229,7 @@ describe('router', () => {
IntegrationID: 'abcd',
SemVer: '3.2.1-beta2',
Status: 'deployed',
Type: 'next.js',
Type: 'lambda',
});
await version.Save(dbManager);

Expand Down Expand Up @@ -153,7 +267,7 @@ describe('router', () => {
IntegrationID: 'abcd',
SemVer: '3.2.1-beta3',
Status: 'deployed',
Type: 'next.js',
Type: 'lambda',
});
await version.Save(dbManager);

Expand Down Expand Up @@ -191,7 +305,7 @@ describe('router', () => {
IntegrationID: 'abcd',
SemVer: '3.2.1-beta3',
Status: 'deployed',
Type: 'next.js',
Type: 'lambda',
});
await version.Save(dbManager);

Expand Down
Loading

0 comments on commit c328480

Please sign in to comment.