Skip to content

Commit

Permalink
Some minor refctoring and add tests fro ssrGetDeploymentINfo
Browse files Browse the repository at this point in the history
  • Loading branch information
cosa65 committed Jul 18, 2022
1 parent 483b740 commit 3b257e3
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 13 deletions.
74 changes: 73 additions & 1 deletion src/__test__/utils/deploymentInfo.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {
DomainName, privacyPolicyIsNotAccepted,
ssrGetDeploymentInfo, DomainName, privacyPolicyIsNotAccepted, Environment,
} from 'utils/deploymentInfo';

describe('deploymentInfo', () => {
Expand All @@ -24,5 +24,77 @@ describe('deploymentInfo', () => {

expect(privacyPolicyIsNotAccepted(user, domainName)).toEqual(true);
});

it('Returns true for users that still need to accept terms in Biomage staging', () => {
const user = { attributes: {} };
const domainName = DomainName.BIOMAGE_STAGING;

expect(privacyPolicyIsNotAccepted(user, domainName)).toEqual(true);
});
});

describe('ssrGetDeploymentInfo', () => {
let originalEnv;

// We are going to mess with the process env so save the original to avoid leak into other tests
beforeAll(() => {
originalEnv = { ...process.env };
});

afterAll(() => {
process.env = originalEnv;
});

it('Throws if not called in server side', () => {
process.env = undefined;

expect(ssrGetDeploymentInfo).toThrowError(
'ssrGetDeploymentInfo must be called on the server side. Refer to `store.networkResources.environment` for the actual environment.',
);
});

it('Works with test node env', () => {
process.env = { NODE_ENV: 'test' };

expect(ssrGetDeploymentInfo()).toEqual({
environment: Environment.DEVELOPMENT,
domainName: DomainName.BIOMAGE,
});
});

it('Works with prod k8s env in biomage domain', () => {
process.env = {
NODE_ENV: Environment.PRODUCTION,
K8S_ENV: Environment.PRODUCTION,
DOMAIN_NAME: DomainName.BIOMAGE,
};

expect(ssrGetDeploymentInfo()).toEqual({
environment: Environment.PRODUCTION,
domainName: DomainName.BIOMAGE,
});
});

it('Works with staging k8s env in biomage staging domain', () => {
process.env = {
NODE_ENV: Environment.PRODUCTION,
K8S_ENV: Environment.STAGING,
DOMAIN_NAME: DomainName.BIOMAGE_STAGING,
};

expect(ssrGetDeploymentInfo()).toEqual({
environment: Environment.STAGING,
domainName: DomainName.BIOMAGE_STAGING,
});
});

it('Works in development', () => {
process.env = { NODE_ENV: Environment.DEVELOPMENT };

expect(ssrGetDeploymentInfo()).toEqual({
environment: Environment.DEVELOPMENT,
domainName: DomainName.BIOMAGE,
});
});
});
});
23 changes: 11 additions & 12 deletions src/utils/deploymentInfo.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
const isBrowser = typeof window !== 'undefined' && typeof window.document !== 'undefined';

const privacyPolicyIsNotAccepted = (user, domainName) => user?.attributes['custom:agreed_terms'] !== 'true' && domainName === DomainName.BIOMAGE;
const privacyPolicyIsNotAccepted = (user, domainName) => (
user?.attributes['custom:agreed_terms'] !== 'true'
&& (domainName === DomainName.BIOMAGE || domainName === DomainName.BIOMAGE_STAGING)
);

const Environment = {
DEVELOPMENT: 'development',
Expand All @@ -16,14 +19,14 @@ const DomainName = {
const ssrGetDeploymentInfo = () => {
let currentEnvironment = null;

if (process.env.NODE_ENV === 'test') {
return Environment.DEVELOPMENT;
}

if (!process.env) {
throw new Error('ssrGetDeploymentInfo must be called on the server side. Refer to `store.networkResources.environment` for the actual environment.');
}

if (process.env.NODE_ENV === 'test') {
return { environment: Environment.DEVELOPMENT, domainName: DomainName.BIOMAGE };
}

switch (process.env.K8S_ENV) {
case 'production':
currentEnvironment = Environment.PRODUCTION;
Expand All @@ -36,13 +39,9 @@ const ssrGetDeploymentInfo = () => {
break;
}

let domainName;
if (
[DomainName.BIOMAGE, DomainName.BIOMAGE_STAGING].includes(process.env.DOMAIN_NAME)
|| currentEnvironment === Environment.DEVELOPMENT
) {
domainName = DomainName.BIOMAGE;
}
const domainName = currentEnvironment !== Environment.DEVELOPMENT
? process.env.DOMAIN_NAME
: DomainName.BIOMAGE;

return { environment: currentEnvironment, domainName };
};
Expand Down

0 comments on commit 3b257e3

Please sign in to comment.