Skip to content
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

fix: remove env variable dependencies from tests (bloom-housing#4471) #817

Open
wants to merge 3 commits 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
8 changes: 8 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,21 @@ executors:
TEST_DATABASE_URL: "postgres://bloom-ci@localhost:5432/bloom"
PARTNERS_PORTAL_URL: "http://localhost:3001"
JURISDICTION_NAME: Alameda
BACKEND_API_BASE: "http://localhost:3100"
GOOGLE_API_EMAIL: "secret-key"
GOOGLE_API_ID: "secret-key"
GOOGLE_API_KEY: "secret-key"
GOOGLE_CLOUD_PROJECT_ID: "secret-key"
SHOW_LOTTERY: "TRUE"
THROTTLE_LIMIT: 1000
SHOW_PUBLIC_LOTTERY: "TRUE"
API_PASS_KEY: "some-key-here"
HOUSING_COUNSELOR_SERVICE_URL: "/get-assistance"
# Sample token https://docs.mapbox.com/api/accounts/tokens/#example-token-metadata-object
MAPBOX_TOKEN: "pk.eyJ1Ijoic2NvdGhpcyIsImEiOiJjaWp1Y2ltYmUwMDBicmJrdDQ4ZDBkaGN4In0.sbihZCZJ56-fsFNKHXF8YQ"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ludtkemorgan Is this a private var?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a sample token stolen from the commented link. The API needs a valid looking token (starting with pk. and probably a few other things) but it's not actually a valid token that would work if you tried to use it

CLOUDINARY_KEY: "key"
CLOUDINARY_SIGNED_PRESET: "signedpreset"
CLOUDINARY_CLOUD_NAME: "exygy"

standard-node:
docker:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
-- AlterEnum
-- This migration adds more than one value to an enum.
-- With PostgreSQL versions 11 and earlier, this is not possible
-- in a single migration. This can be worked around by creating
-- multiple migrations, each migration adding only one value to
-- the enum.
ALTER TYPE "languages_enum"
ADD
VALUE 'bn';

ALTER TYPE "languages_enum"
ADD
VALUE 'ar';
2 changes: 2 additions & 0 deletions api/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -975,6 +975,8 @@ enum LanguagesEnum {
vi
zh
tl
bn
ar

@@map("languages_enum")
}
Expand Down
1 change: 1 addition & 0 deletions api/test/integration/api-key-guard.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ describe('API Key Guard Tests', () => {
let cookies = '';

beforeAll(async () => {
process.env.API_PASS_KEY = 'OUR_API_PASS_KEY';
const moduleFixture: TestingModule = await Test.createTestingModule({
imports: [AppModule],
}).compile();
Expand Down
2 changes: 2 additions & 0 deletions api/test/integration/auth.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ describe('Auth Controller Tests', () => {
let smsService: SmsService;

beforeAll(async () => {
process.env.TWILIO_ACCOUNT_SID = 'AC.SID';
process.env.TWILIO_AUTH_TOKEN = 'TOKEN';
const moduleFixture: TestingModule = await Test.createTestingModule({
imports: [AppModule],
}).compile();
Expand Down
5 changes: 1 addition & 4 deletions api/test/integration/lottery.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -702,10 +702,7 @@ describe('Lottery Controller Tests', () => {
it('should only expire listing lotteries that are past due', async () => {
process.env.LOTTERY_DAYS_TILL_EXPIRY = '45';
const expiredClosedListingDate = dayjs(new Date())
.subtract(
Number(process.env.LOTTERY_DAYS_TILL_EXPIRY || 45) + 1,
'days',
)
.subtract(46, 'days')
.toDate();

const expiredListingData = await listingFactory(jurisdictionAId, prisma, {
Expand Down
13 changes: 11 additions & 2 deletions api/test/integration/throttle-guard.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,23 @@ import { INestApplication } from '@nestjs/common';
import request from 'supertest';
import { AppModule } from '../../src/modules/app.module';
import { PrismaService } from '../../src/services/prisma.service';
import { ThrottlerModule } from '@nestjs/throttler';

describe('Throttle Guard Tests', () => {
let app: INestApplication;
let prisma: PrismaService;

beforeAll(async () => {
const moduleFixture: TestingModule = await Test.createTestingModule({
imports: [AppModule],
imports: [
AppModule,
ThrottlerModule.forRoot([
{
ttl: Number('600000'),
limit: Number('10'),
},
]),
],
}).compile();

app = moduleFixture.createNestApplication();
Expand All @@ -26,7 +35,7 @@ describe('Throttle Guard Tests', () => {
it('should succeed until throttle limit is exceeded', async () => {
let res;
// loop as many times as the THROTTLE_LIMIT allows
for (let i = 0; i < Number(process.env.THROTTLE_LIMIT); i++) {
for (let i = 0; i < 10; i++) {
res = await request(app.getHttpServer()).get('/').expect(200);
expect(res.body).toEqual({
success: true,
Expand Down
1 change: 1 addition & 0 deletions api/test/integration/user.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ describe('User Controller Tests', () => {
});

beforeAll(async () => {
process.env.PARTNERS_PORTAL_URL = 'http://localhost:3001/';
const moduleFixture: TestingModule = await Test.createTestingModule({
imports: [AppModule],
})
Expand Down
1 change: 1 addition & 0 deletions api/test/unit/passports/jwt.strategy.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ describe('Testing jwt strategy', () => {
let strategy: JwtStrategy;
let prisma: PrismaService;
beforeAll(async () => {
process.env.APP_SECRET = 'SOME-LONG-SECRET-KEY';
const module: TestingModule = await Test.createTestingModule({
providers: [JwtStrategy, PrismaService],
}).compile();
Expand Down
3 changes: 3 additions & 0 deletions api/test/unit/passports/mfa.strategy.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ describe('Testing mfa strategy', () => {
});

it('should fail because user is locked out', async () => {
process.env.AUTH_LOCK_LOGIN_AFTER_FAILED_ATTEMPTS = '5';
process.env.AUTH_LOCK_LOGIN_COOLDOWN = '1800000';
prisma.userAccounts.findFirst = jest.fn().mockResolvedValue({
id: randomUUID(),
lastLoginAt: new Date(),
Expand Down Expand Up @@ -475,6 +477,7 @@ describe('Testing mfa strategy', () => {
});

it('should fail if no mfaCode is expired', async () => {
process.env.MFA_CODE_VALID = '60000';
const id = randomUUID();
prisma.userAccounts.findFirst = jest.fn().mockResolvedValue({
id: id,
Expand Down
3 changes: 3 additions & 0 deletions api/test/unit/passports/single-use-code.strategy.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ describe('Testing single-use-code strategy', () => {
let strategy: SingleUseCodeStrategy;
let prisma: PrismaService;
beforeAll(async () => {
process.env.MFA_CODE_VALID = '60000';
process.env.AUTH_LOCK_LOGIN_AFTER_FAILED_ATTEMPTS = '5';
process.env.AUTH_LOCK_LOGIN_COOLDOWN = '1800000';
const module: TestingModule = await Test.createTestingModule({
providers: [SingleUseCodeStrategy, PrismaService],
}).compile();
Expand Down
26 changes: 17 additions & 9 deletions api/test/unit/services/auth.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,14 @@ import { PermissionService } from '../../../src/services/permission.service';
import { Jurisdiction } from '../../../src/dtos/jurisdictions/jurisdiction.dto';

describe('Testing auth service', () => {
process.env.APP_SECRET = 'SOME-LONG-SECRET-KEY';
process.env.EMAIL_API_KEY = 'SG.SOME-LONG-SECRET-KEY';
process.env.TWILIO_ACCOUNT_SID = 'AC';
process.env.TWILIO_AUTH_TOKEN = '842';
process.env.MFA_CODE_LENGTH = '5';
process.env.MFA_CODE_VALID = '60000';
process.env.TWILIO_PHONE_NUMBER = '5555555555';
process.env.GOOGLE_API_KEY = 'GOOGLE_API_KEY';
let authService: AuthService;
let smsService: SmsService;
let prisma: PrismaService;
Expand Down Expand Up @@ -96,7 +104,7 @@ describe('Testing auth service', () => {
sub: id,
expiresIn: 86400000 / 24,
},
process.env.APP_SECRET,
'SOME-LONG-SECRET-KEY',
),
);
});
Expand Down Expand Up @@ -128,7 +136,7 @@ describe('Testing auth service', () => {
sub: id,
expiresIn: 86400000,
},
process.env.APP_SECRET,
'SOME-LONG-SECRET-KEY',
),
);
});
Expand Down Expand Up @@ -655,8 +663,8 @@ describe('Testing auth service', () => {
});
expect(sendMfaCodeMock).not.toHaveBeenCalled();
expect(smsService.client.messages.create).toHaveBeenCalledWith({
body: expect.anything(),
from: expect.anything(),
body: 'Your Partners Portal account access token: 00000',
from: '5555555555',
to: '520-781-8711',
});
expect(res).toEqual({
Expand Down Expand Up @@ -727,7 +735,7 @@ describe('Testing auth service', () => {
{
id,
},
process.env.APP_SECRET,
'SOME-LONG-SECRET-KEY',
);
const response = {
cookie: jest.fn(),
Expand Down Expand Up @@ -788,14 +796,14 @@ describe('Testing auth service', () => {
{
id,
},
process.env.APP_SECRET,
'SOME-LONG-SECRET-KEY',
);
const secondId = randomUUID();
const secondToken = sign(
{
id: secondId,
},
process.env.APP_SECRET,
'SOME-LONG-SECRET-KEY',
);

const response = {
Expand Down Expand Up @@ -829,7 +837,7 @@ describe('Testing auth service', () => {
{
id,
},
process.env.APP_SECRET,
'SOME-LONG-SECRET-KEY',
);
prisma.userAccounts.findUnique = jest
.fn()
Expand Down Expand Up @@ -890,7 +898,7 @@ describe('Testing auth service', () => {
id,
email: '[email protected]',
},
process.env.APP_SECRET,
'SOME-LONG-SECRET-KEY',
);
prisma.userAccounts.findUnique = jest
.fn()
Expand Down
1 change: 1 addition & 0 deletions api/test/unit/services/lottery.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -984,6 +984,7 @@ describe('Testing lottery service', () => {

describe('Test expireLotteries endpoint', () => {
it('should call the updateMany', async () => {
process.env.LOTTERY_DAYS_TILL_EXPIRY = '45';
prisma.listings.findMany = jest.fn().mockResolvedValue([
{
id: 'example id1',
Expand Down
5 changes: 4 additions & 1 deletion api/test/unit/services/user.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -304,10 +304,11 @@ describe('Testing user service', () => {

describe('createConfirmationToken', () => {
it('should encode a confirmation token correctly', () => {
process.env.APP_SECRET = 'SOME-LONG-SECRET-KEY';
const id = randomUUID();
const res = service.createConfirmationToken(id, '[email protected]');
expect(res).not.toBeNull();
const decoded = verify(res, process.env.APP_SECRET) as IdDTO;
const decoded = verify(res, 'SOME-LONG-SECRET-KEY') as IdDTO;
expect(decoded.id).toEqual(id);
});
});
Expand Down Expand Up @@ -2107,6 +2108,8 @@ describe('Testing user service', () => {
});

it('should successfully request single use code when previous code is still valid', async () => {
process.env.MFA_CODE_LENGTH = '5';
process.env.MFA_CODE_VALID = '60000';
const id = randomUUID();
emailService.sendSingleUseCode = jest.fn();
prisma.userAccounts.findFirst = jest.fn().mockResolvedValue({
Expand Down
Loading
Loading