Skip to content

Commit

Permalink
test: fix e2e test setup
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathonadams committed Sep 9, 2020
1 parent c5da0dc commit 2218c6e
Show file tree
Hide file tree
Showing 7 changed files with 226 additions and 296 deletions.
2 changes: 1 addition & 1 deletion apps/server/api/src/environments/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import { TestServerConfig } from '@ztp/data';
import { AuthEnv } from '@ztp/server/auth';

const audience = 'http://localhost:3000';
const audience = `http://localhost:${process.env.PORT}`;
const authServerHost = audience;

const privateKey = `-----BEGIN PRIVATE KEY-----
Expand Down
9 changes: 8 additions & 1 deletion apps/server/api/src/server/api/todos/todo.e2e.spec.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
process.env.PORT = '9999';

import { createHash } from 'crypto';
import Koa from 'koa';
import { ITodo } from '@ztp/data';
import { authConfig } from '../../../environments/index';
Expand All @@ -7,6 +10,10 @@ import { createGraphQLSpec } from '@ztp/tests/server';
import { Todo } from './todo.model';
import { User } from '../users';

const keyId = createHash('md5')
.update(authConfig.accessToken.publicKey as string)
.digest('hex');

// Need to import and run the server because
// the server is also our "auth server"
// and the Auth guard needs to be able to retrieve the JWKS
Expand All @@ -23,7 +30,7 @@ const updatedTodo = {

createGraphQLSpec(
schema,
{ ...authConfig.accessToken, keyId: 'keyId' },
{ ...authConfig.accessToken, keyId },
server,
/*userResource */ true
)(Todo, 'Todo', todo, updatedTodo, User);
14 changes: 11 additions & 3 deletions apps/server/api/src/server/api/users/user.e2e.spec.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
process.env.PORT = '9999';

import Koa from 'koa';
import { MongoMemoryServer } from 'mongodb-memory-server';
import { runQuery, setupTestDB, signTestAccessToken } from '@ztp/tests/server';
Expand All @@ -8,6 +10,11 @@ import { IUser } from '@ztp/data';
import ApiServer from '../../server';
import { Server } from 'http';
import { User } from './user.model';
import { createHash } from 'crypto';

const keyId = createHash('md5')
.update(authConfig.accessToken.publicKey as string)
.digest('hex');

// Need to import and run the server because
// the server is also our "auth server"
Expand Down Expand Up @@ -41,10 +48,11 @@ describe(`GraphQL / User`, () => {
testServer = await server.initializeServer(dbUri);

createdUser = await User.create(user);

[createdUser.id, createdUser._id] = [createdUser._id, createdUser.id];
jwt = signTestAccessToken({ ...authConfig.accessToken, keyId: 'id' })(
createdUser
);

const config = { ...authConfig.accessToken, keyId };
jwt = signTestAccessToken(config)(createdUser);
});

afterAll(async () => {
Expand Down
1 change: 0 additions & 1 deletion libs/tests/src/lib/server/graphQLSpec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
/* istanbul ignore file */

import 'jest-extended';
import { GraphQLSchema } from 'graphql';
import { MongoMemoryServer } from 'mongodb-memory-server';
Expand Down
23 changes: 0 additions & 23 deletions libs/tests/src/lib/server/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,6 @@ import mongoose from 'mongoose';
import { MongoMemoryServer } from 'mongodb-memory-server';
import { graphql, GraphQLSchema } from 'graphql';

// export interface RESTTestResource<T> {
// model: any;
// resourceName: string;
// urlString: string;
// resourceToCreate: any;
// resourceToUpdate: any;
// testDependents?: TestDependents[];
// }

// export interface GraphQLTestResource<T> {
// model: any;
// resourceName: string;
// queryName: string;
// resourceToCreate: any;
// resourceToUpdate: any;
// testDependents?: TestDependents[];
// }

// export interface TestDependents {
// model: any;
// resource: any;
// }

/**
* Helper function to generate ObjectID, note it returns the hex string of the ObjectId
*/
Expand Down
Loading

0 comments on commit 2218c6e

Please sign in to comment.