more work on testing

This commit is contained in:
Mikayla Dobson
2022-12-17 12:22:03 -06:00
parent a38bc2793f
commit 27e6b4aa1f
14 changed files with 96 additions and 62 deletions

View File

@@ -1,19 +1,8 @@
import request from 'supertest';
const agent = request('localhost:8080');
export default async function loginUser(auth: { token: any }) {
const onResponse = (err: any, res: any) => {
if (err) throw err;
auth.token = res.body.token;
}
agent.post('/auth/login')
.send({
email: "verifieduser@test.com",
password: "verifieduser"
})
.end(onResponse);
return auth;
export default function loginUser(server: any) {
server.post('/auth/login')
.send({ email: 'verifieduser@test.com', password: 'coolpassword' })
.end((err: any, res: Response) => {
if (err) throw err;
expect(res.status).toBe(200);
})
}

View File

@@ -0,0 +1,3 @@
export default function logoutUser(server: any) {
server.delete('/auth/logout');
}

View File

@@ -0,0 +1,23 @@
import dotenv from 'dotenv';
import supertest from "supertest";
import loginUser from '../../helpers/loginUser';
dotenv.config();
const APISTRING = process.env.APISTRING || 'localhost:8080';
const server = supertest.agent(APISTRING);
describe('/auth', () => {
// beforeAll(() => {
// loginUser(server);
// })
// it('receives a token', () => {
// })
// test('allows access to protected resources', async () => {
// const data = await supertest(APISTRING).get('/recipe');
// console.log(data.body);
// expect(data.statusCode).toBe(200);
// // expect(data.body.name).toBe("Green beans");
// })
})

View File

@@ -1,23 +1,27 @@
import request from 'supertest'
import supertest from 'supertest'
import loginUser from '../../helpers/loginUser';
import { IRecipe } from '../../../schemas'
const server = request.agent('localhost:8080');
import dotenv from 'dotenv';
import app from '../../..';
dotenv.config();
const APISTRING = process.env.APISTRING || 'localhost:8080';
const server = supertest.agent(app);
describe('/recipe', () => {
beforeAll(async () => {
// to do: create session user,
// use it to log in on this test,
// use the authenticated session to view recipes
// await server.post('/auth/login')
// .body()
beforeAll(() => {
server.post('/auth/login')
.send({ email: 'verifieduser@test.com', password: 'coolpassword' });
})
describe('GET /', () => {
test('gets an array of recipes', async () => {
const result = await request('localhost:8080').get('/recipe');
const data = JSON.parse(result.text);
expect(data.length).toBeGreaterThan(0);
})
})
})
it('gets an array of recipes', () => {
server.get('/recipe').end((err, res) => {
if (err) throw err;
console.log(res.body);
expect(res.statusCode).toBe(200);
expect(res.body.ok).toBeTruthy();
});
});
});
});

View File

@@ -1,13 +1,19 @@
import loginUser from "../../helpers/loginUser"
import supertest from "supertest";
import loginUser from '../../helpers/loginUser';
import dotenv from 'dotenv';
dotenv.config();
const APISTRING = process.env.APISTRING || 'localhost:8080';
const server = supertest.agent(APISTRING);
describe('login user', () => {
let auth = { token: undefined }
beforeAll(async () => {
auth = await loginUser(auth);
beforeAll(() => {
loginUser(server);
})
it('authenticates a hard-coded user', () => {
console.log(auth);
expect(auth.token).toBeDefined();
it('allows access to protected resources', () => {
server.get('/recipe').end((err, res) => {
if (err) throw err;
expect(res.statusCode).toBe(200);
})
})
})