const app = require('./app.js') const request = require('supertest') const qs = require('querystring') require('dotenv').config() describe('app', function () { it('should exist', function (done) { if (!app?.mountpath) throw new Error('app doesnt exist'); done() }) describe('/health', function () { it('should be publicly readable', function (done) { request(app) .get('/health') .set('Accept', 'text/html') .expect('Content-Type', /text/) .expect(/piss/) .expect(200, done) }) }) describe('/task', function () { describe('POST', function () { it('should create a task', function (done) { request(app) .post('/task') .set('Authorization', `Bearer ${process.env.API_KEY}`) .set('Accept', 'application/json') .send({ url: 'https://futureporn-b2.b-cdn.net/projekt-melody.jpg' }) .expect('Content-Type', /json/) .expect((res) => { if (!res.body?.data) throw new Error('response body was missing data') if (!res.body?.data?.id) throw new Error('response body was missing id') return true }) .expect(200, done) }) }) describe('GET', function () { it('should show all tasks specifications', async function () { await request(app).post('/task').set('Authorization', `Bearer ${process.env.API_KEY}`).send({ url: 'https://example.com/my.jpg' }) await request(app).post('/task').set('Authorization', `Bearer ${process.env.API_KEY}`).send({ url: 'https://example.com/your.png' }) return request(app) .get(`/task`) .set('Authorization', `Bearer ${process.env.API_KEY}`) .set('Accept', 'application/json') .expect('Content-Type', /json/) .expect((res) => { if (!res?.body?.data) throw new Error('there was no data in response') }) .expect(200) }) it('should accept task id as query param and return task specification', function (done) { const seed = request(app).post('/task').set('Authorization', `Bearer ${process.env.API_KEY}`).send({ url: 'https://example.com/z.jpg' }) seed.then((res) => { const query = qs.stringify({ id: res.body.data.id }) request(app) .get(`/task?${query}`) .set('Accept', 'application/json') .expect('Content-Type', /json/) .expect((res) => { if (res?.body?.error) throw new Error('there was an error in the response: '+res.body?.message) if (!res?.body?.data?.url) throw new Error('data.url was missing') if (!res?.body?.data?.createdAt) throw new Error('data.createdAt was missing') return true }) .expect(200, done) }) }) it('should show all tasks by default', function (done) { request(app) .get('/task') .set('Accept', 'application/json') .expect('Content-Type', /json/) .expect((res) => { if (res.body?.error) throw new Error('there was an error in the response'+res.error) if (!res.body?.data) throw new Error('data was missing') return true }) .expect(200, done) }) }) describe('DELETE', function () { const query = qs.stringify({ id: 'awejf9wiejf9we' }) it('should delete a single task', function (done) { request(app) .delete(`/task?${query}`) .set('Authorization', `Bearer ${process.env.API_KEY}`) .set('Accept', 'application/json') .expect('Content-Type', /json/) .expect(200, done); }); }) }) })