-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #17 from Phoenix-Commerce/e2e-tests
feat: add e2e test
- Loading branch information
Showing
7 changed files
with
176 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import request from 'supertest'; | ||
|
||
export const login = async (app: any) => { | ||
const res = await request(app) | ||
.post('/graphql') | ||
.send({ | ||
query: ` | ||
mutation Login($email: String!, $password: String!) { | ||
login(email: $email, password: $password) | ||
} | ||
`, | ||
variables: { | ||
email: "[email protected]", | ||
password: "superpassword" | ||
}, | ||
}); | ||
|
||
if (!res.body.data || !res.body.data.login) { | ||
throw new Error('Login failed or token not returned'); | ||
} | ||
|
||
return res.body.data.login; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import { MongoMemoryServer } from 'mongodb-memory-server'; | ||
import mongoose from 'mongoose'; | ||
import express from 'express'; | ||
import { ApolloServer } from 'apollo-server-express'; | ||
import jwt from 'jsonwebtoken'; | ||
import PluginLoader from '../src/plugins/plugin-loader'; // Adjust the path as needed | ||
import { setMongoServer } from './teardown'; | ||
|
||
const JWT_SECRET = process.env.JWT_SECRET || 'your_jwt_secret'; | ||
|
||
let server: ApolloServer; | ||
let app: express.Application; | ||
|
||
export const setup = async () => { | ||
const mongoServer = await MongoMemoryServer.create(); | ||
const uri = mongoServer.getUri(); | ||
|
||
await mongoose.connect(uri); | ||
|
||
const pluginLoader = new PluginLoader(); | ||
pluginLoader.loadPlugins(); | ||
pluginLoader.initializePlugins(); | ||
|
||
const schema = await pluginLoader.createSchema(); | ||
pluginLoader.registerModels(); | ||
|
||
app = express(); | ||
|
||
server = new ApolloServer({ | ||
schema, | ||
context: async ({ req }) => { | ||
let user = null; | ||
const authHeader = req.headers.authorization || ''; | ||
const token = authHeader.split(' ')[1]; | ||
|
||
if (token) { | ||
try { | ||
const decoded: any = jwt.verify(token, JWT_SECRET); | ||
const UserModel = mongoose.model('User'); // Access the User model dynamically | ||
user = await UserModel.findById(decoded.id); | ||
} catch (error) { | ||
console.error('Error decoding token:', error); | ||
} | ||
} | ||
|
||
// Mock enforcer for testing purposes | ||
const enforcer = { | ||
enforce: (action: string, resource: string) => { | ||
return true; // Allow all actions for testing | ||
}, | ||
}; | ||
|
||
return { | ||
...pluginLoader.context, | ||
req, | ||
user, | ||
enforcer, | ||
}; | ||
}, | ||
}); | ||
|
||
await server.start(); | ||
server.applyMiddleware({ app, path: '/graphql' }); | ||
|
||
setMongoServer(mongoServer); | ||
|
||
return app; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import mongoose from 'mongoose'; | ||
import { MongoMemoryServer } from 'mongodb-memory-server'; | ||
|
||
let mongoServer: MongoMemoryServer; | ||
|
||
export const setMongoServer = (server: MongoMemoryServer) => { | ||
mongoServer = server; | ||
}; | ||
|
||
export const teardown = async () => { | ||
await mongoose.disconnect(); | ||
if (mongoServer) { | ||
await mongoServer.stop(); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import { describe, it, beforeEach, afterEach } from 'bun:test'; | ||
import { expect } from 'chai'; | ||
import { setup } from '../setup'; | ||
import { teardown } from '../teardown'; | ||
import { initDB, clearDB } from '../utils'; | ||
import { login } from '../login'; | ||
import request from 'supertest'; | ||
|
||
let app: any; | ||
let token: string; | ||
|
||
beforeEach(async () => { | ||
app = await setup(); | ||
await initDB(); | ||
token = await login(app); | ||
}); | ||
|
||
afterEach(async () => { | ||
await clearDB(); | ||
await teardown(); | ||
}); | ||
|
||
describe('User Queries', () => { | ||
it('should return a list of users', async () => { | ||
const res = await request(app) | ||
.post('/graphql') | ||
.set('Authorization', `Bearer ${token}`) | ||
.send({ | ||
query: ` | ||
query { | ||
users { | ||
name | ||
} | ||
} | ||
`, | ||
}) | ||
.expect(200); | ||
expect(res.body.data.users).to.have.lengthOf(3); | ||
expect(res.body.data.users).to.deep.equal([ | ||
{ name: 'John Doe', email: '[email protected]' }, | ||
{ name: 'Jane Doe', email: '[email protected]' }, | ||
{ name: 'Super User', email: '[email protected]' }, | ||
]); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { UserModel } from '../src/plugins/auth-plugin/models/user'; // Adjust the path as needed | ||
import bcrypt from 'bcrypt'; | ||
|
||
export const initDB = async () => { | ||
//we need users to be able to hit auth and non-auth endpoints | ||
const users = [ | ||
{ name: 'John Doe', email: '[email protected]', password: await bcrypt.hash('password123', 10) }, | ||
{ name: 'Jane Doe', email: '[email protected]', password: await bcrypt.hash('password123', 10) }, | ||
{ name: 'Super User', email: '[email protected]', password: await bcrypt.hash('superpassword', 10), role: 'superuser' }, | ||
]; | ||
await UserModel.insertMany(users); | ||
}; | ||
|
||
export const clearDB = async () => { | ||
await UserModel.deleteMany({}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters