Skip to content

Commit

Permalink
Reorganize and decouple tests (WIP), move passport config
Browse files Browse the repository at this point in the history
  • Loading branch information
oddgrd committed Mar 27, 2022
1 parent 7e784cd commit 35b90d9
Show file tree
Hide file tree
Showing 8 changed files with 391 additions and 294 deletions.
51 changes: 51 additions & 0 deletions server/src/config/passport.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import passport from 'passport';
import { Strategy } from 'passport-google-oauth20';
import { __prod__ } from '../constants';
import { User } from '../entities/User';

passport.use(
new Strategy(
{
clientID: process.env.GOOGLE_CLIENT_ID,
clientSecret: process.env.GOOGLE_CLIENT_SECRET,
callbackURL: __prod__
? `https://api.myhomeboard.no/api/auth/google/callback`
: 'http://localhost:4000/api/auth/google/callback',
},
async (_accessToken, _refreshToken, profile: any, done) => {
const user = await User.findOne({ where: { googleId: profile.id } });
if (user) {
await User.update(
{ id: user.id },
{
name: profile.displayName,
avatar: profile._json.picture,
}
);
done(null, user);
} else {
try {
const newUser = await User.create({
name: profile.displayName,
email: profile._json.email,
avatar: profile._json.picture,
googleId: profile.id,
}).save();
done(null, newUser);
} catch (error) {
done(error);
}
}
}
)
);

passport.serializeUser(function (user: any, done) {
done(null, user.id);
});

passport.deserializeUser((id: string, done) => {
User.findOne(id).then((user) => {
done(null, user);
});
});
49 changes: 3 additions & 46 deletions server/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import session from 'express-session';
import { graphqlUploadExpress } from 'graphql-upload';
import Redis from 'ioredis';
import passport from 'passport';
import { Strategy } from 'passport-google-oauth20';
import path from 'path';
import 'reflect-metadata';
import { createConnection } from 'typeorm';
Expand All @@ -22,6 +21,9 @@ import { createLayoutLoader } from './utils/createLayoutLoader';
import { createSchema } from './utils/createSchema';
import { createUserLoader } from './utils/createUserLoader';

// Passport configuration
import "./config/passport";

const main = async () => {
const connection = await createConnection({
applicationName: 'myhomeboard',
Expand Down Expand Up @@ -74,51 +76,6 @@ const main = async () => {
}),
});

passport.use(
new Strategy(
{
clientID: process.env.GOOGLE_CLIENT_ID,
clientSecret: process.env.GOOGLE_CLIENT_SECRET,
callbackURL: __prod__
? `https://api.myhomeboard.no/api/auth/google/callback`
: 'http://localhost:4000/api/auth/google/callback',
},
async (_accessToken, _refreshToken, profile: any, done) => {
const user = await User.findOne({ where: { googleId: profile.id } });
if (user) {
await User.update(
{ id: user.id },
{
name: profile.displayName,
avatar: profile._json.picture,
}
);
done(null, user);
} else {
try {
const newUser = await User.create({
name: profile.displayName,
email: profile._json.email,
avatar: profile._json.picture,
googleId: profile.id,
}).save();
done(null, newUser);
} catch (error) {
done(error);
}
}
}
)
);
passport.serializeUser(function (user: any, done) {
done(null, user.id);
});
passport.deserializeUser((id: string, done) => {
User.findOne(id).then((user) => {
done(null, user);
});
});

app.use(
session({
name: 'mhb',
Expand Down
81 changes: 31 additions & 50 deletions server/src/resolvers/ascent/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,65 +1,35 @@
import faker from 'faker';
import { User } from '../../entities/User';
import { gqlWrapper } from '../../test-utils/gqlWrapper';
import { Board } from '../../entities/Board';
import { Problem } from '../../entities/Problem';
import { Ascent } from '../../entities/Ascent';

const addAscentMutation = `
mutation AddAscent($options: AddAscentInput!) {
addAscent(options: $options)
}
`;
const editAscentMutation = `
mutation EditAscent($options: EditAscentInput!) {
editAscent(options: $options)
}
`;
const deleteAscentMutation = `
mutation DeleteAscent($problemId: String!) {
deleteAscent(problemId: $problemId)
}
`;
import { deleteAscentMutation, editAscentMutation } from '../../test-utils/gqlMutations';
import { addAscent, getDummyData } from '../../test-utils/utils';

describe('Ascent tests', () => {
let userId: string;
let problemId: string;
it('adds ascent', async () => {
// Get user, board and problem ids from dummy data
const user = await User.findOne({ where: { name: 'odd' } });
userId = user!.id;
const board = await Board.findOne({ where: { title: 'test' } });
const problem = await Problem.findOne({ where: { title: 'testproblem' } });
problemId = problem!.id;
const {response, ascentInput } = await addAscent();

const ascentInput = {
problemId,
boardId: board!.id,
comment: faker.lorem.sentence(3),
grade: 10,
rating: 2,
attempts: 6,
};
const response = await gqlWrapper({
source: addAscentMutation,
variableValues: {
options: ascentInput,
},
userId: userId,
});
expect(response).toMatchObject({
data: {
addAscent: true,
},
});

const ascent = await Ascent.findOne({ where: { problemId } });
const ascent = await Ascent.findOne({ where: { problemId: ascentInput.problemId } });
expect(ascent).toBeDefined();
});

it('edits ascent', async () => {
const {user} = await getDummyData();

const {response: addAscentResponse, ascentInput } = await addAscent();
expect(addAscentResponse).toMatchObject({
data: {
addAscent: true,
},
});

const editAscentInput = {
problemId,
problemId: ascentInput.problemId,
comment: faker.lorem.sentence(3),
grade: 5,
rating: 0,
Expand All @@ -70,33 +40,44 @@ describe('Ascent tests', () => {
variableValues: {
options: editAscentInput,
},
userId: userId,
userId: user?.id,
});

expect(response).toMatchObject({
data: {
editAscent: true,
},
});

const ascent = await Ascent.findOne({ where: { problemId } });
const ascent = await Ascent.findOne({ where: { problemId: ascentInput.problemId } });
expect(ascent).toBeDefined();
expect(ascent).toMatchObject(editAscentInput);
});

it('deletes ascent', async () => {
const {user} = await getDummyData();

const {response: addAscentResponse, ascentInput } = await addAscent();
expect(addAscentResponse).toMatchObject({
data: {
addAscent: true,
},
});

const response = await gqlWrapper({
source: deleteAscentMutation,
variableValues: {
problemId,
problemId: ascentInput.problemId,
},
userId: userId,
userId: user?.id,
});
expect(response).toMatchObject({
data: {
deleteAscent: true,
},
});

const ascent = await Ascent.findOne({ where: { problemId } });
const ascent = await Ascent.findOne({ where: { problemId: ascentInput.problemId } });
expect(ascent).toBeUndefined();
});
});
85 changes: 24 additions & 61 deletions server/src/resolvers/board/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,66 +1,13 @@
import faker from 'faker';
import { User } from '../../entities/User';
import { gqlWrapper } from '../../test-utils/gqlWrapper';
import { Board } from '../../entities/Board';

const createBoardMutation = `
mutation CreateBoard($options: BoardInput!) {
createBoard(options: $options) {
board {
id
title
}
errors {
message
field
}
}
}
`;
const editBoardMutation = `
mutation EditBoard($options: EditBoardInput!) {
editBoard(options: $options) {
board {
id
title
angles
adjustable
}
errors {
message
field
}
}
}
`;
const deleteBoardMutation = `
mutation DeleteBoard($boardId: String!) {
deleteBoard(boardId: $boardId)
}
`;
import { deleteBoardMutation, editBoardMutation } from '../../test-utils/gqlMutations';
import { createBoard, getDummyData } from '../../test-utils/utils';

describe('Board tests', () => {
let userId: string;
let boardId: string;
it('should create board', async () => {
// get user from dummy data
const user = await User.findOne({ where: { name: 'odd' } });
userId = user!.id;
const boardInput = {
title: faker.lorem.sentence(2),
description: faker.lorem.sentence(3),
adjustable: true,
angles: [20, 30],
city: faker.address.city(),
country: faker.address.country(),
};
const response = await gqlWrapper({
source: createBoardMutation,
variableValues: {
options: boardInput,
},
userId,
});
const {response, boardInput} = await createBoard();

expect(response).toMatchObject({
data: {
createBoard: {
Expand All @@ -71,12 +18,18 @@ describe('Board tests', () => {
},
},
});
boardId = response.data!.createBoard.board.id;
const board = await Board.findOne(boardId);

const board = await Board.findOne(response.data!.createBoard.board.id);
expect(board).toBeDefined();
});

it('should edit board', async () => {
const {user} = await getDummyData();

const {response: createBoardResponse} = await createBoard();
let boardId = createBoardResponse.data!.createBoard.board.id;
expect(boardId).toBeDefined();

const editBoardInput = {
boardId,
title: faker.lorem.sentence(2),
Expand All @@ -91,8 +44,9 @@ describe('Board tests', () => {
variableValues: {
options: editBoardInput,
},
userId,
userId: user?.id,
});

expect(response).toMatchObject({
data: {
editBoard: {
Expand All @@ -105,20 +59,29 @@ describe('Board tests', () => {
},
},
});

const board = await Board.findOne(boardId);

expect(board).toBeDefined();
expect(board!.adjustable).toBeFalsy();
expect(board!.angles.length).toBe(1);
});

it('should delete board', async () => {
const {user} = await getDummyData();

const {response: createBoardResponse} = await createBoard();
let boardId = createBoardResponse.data?.createBoard.board.id;
expect(boardId).toBeDefined();

const response = await gqlWrapper({
source: deleteBoardMutation,
variableValues: {
boardId,
},
userId: userId,
userId: user?.id,
});

expect(response).toMatchObject({
data: {
deleteBoard: true,
Expand Down
Loading

0 comments on commit 35b90d9

Please sign in to comment.