From 9e01a7014e7d7b2698432b34b76bcf7d3e133431 Mon Sep 17 00:00:00 2001 From: Alex Hinds Date: Thu, 11 Apr 2019 15:40:06 +1000 Subject: [PATCH 1/3] moved some of config to docker-compose --- Dockerfile | 8 +++++++- docker-compose.yml | 14 ++++++++++++++ docker.sh | 21 ++++++++------------- src/models/db/index.js | 2 +- 4 files changed, 30 insertions(+), 15 deletions(-) create mode 100644 docker-compose.yml diff --git a/Dockerfile b/Dockerfile index ffc4ea0..2119d9a 100644 --- a/Dockerfile +++ b/Dockerfile @@ -7,4 +7,10 @@ RUN apt-get update RUN echo yes | apt-get install -y msodbcsql RUN echo yes | apt-get install -y mssql-tools RUN locale-gen en_US en_US.UTF-8 -RUN dpkg-reconfigure -f noninteractive locales \ No newline at end of file +RUN dpkg-reconfigure -f noninteractive locales + +ENV PATH="/opt/mssql-tools/bin:${PATH}" +ENV ACCEPT_EULA="Y" +ARG SA_PASSWORD + +EXPOSE 1433 \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..b258a89 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,14 @@ +version: '3.1' +services: + smartcourse-testing: + container_name: 'smartcourse-testing' + build: + context: . + ports: + - "1433:1433" + environment: + SA_PASSWORD: ${LOCAL_SQL_PASSWORD} + ACCEPT_EULA: "Y" + healthcheck: + test: sqlcmd -S localhost,1433 -U SA -P "$SA_PASSWORD" -Q 'select * from smartcourse-testing.testdb.tables' + diff --git a/docker.sh b/docker.sh index 4aa137a..f95937c 100755 --- a/docker.sh +++ b/docker.sh @@ -5,20 +5,15 @@ DOCKER_NAME='smartcourse-testing' docker kill $DOCKER_NAME docker rm $DOCKER_NAME +# bail out if any errors set -ex -# docker build --tag $DOCKER_NAME . -docker run \ - --name $DOCKER_NAME \ - --env 'ACCEPT_EULA=Y' \ - --env "SA_PASSWORD=$LOCAL_SQL_PASSWORD" \ - -p 1433:1433 \ - -d microsoft/mssql-server-linux:2017-latest +# start the container using compose config +SA_PASSWORD="$LOCAL_SQL_PASSWORD" docker-compose up -d -sleep 10 - -docker exec -it $DOCKER_NAME /opt/mssql-tools/bin/sqlcmd \ - -S localhost,1433 -U SA -P "$LOCAL_SQL_PASSWORD" \ +# initdb +docker exec -it $DOCKER_NAME sqlcmd \ + -S localhost,1433 \ + -U SA \ + -P "$LOCAL_SQL_PASSWORD" \ -Q 'CREATE DATABASE testdb' - -# really this should be a dockerfile as with all db config \_(* *)_/ diff --git a/src/models/db/index.js b/src/models/db/index.js index 2b1ce38..9c5e7d0 100644 --- a/src/models/db/index.js +++ b/src/models/db/index.js @@ -13,7 +13,7 @@ class DB extends EventEmitter { super() console.log('Connecting to database') - + this.connections = [] this.ready = false From 47c70c387fef9185e92a5cb68ef3861a77c65873 Mon Sep 17 00:00:00 2001 From: Alex Hinds Date: Thu, 11 Apr 2019 16:35:06 +1000 Subject: [PATCH 2/3] failing build --- src/controllers/report.js | 10 ++++++++ src/models/report.js | 14 +++++++++++ src/routes/index.js | 4 +-- src/routes/uni.js | 3 +++ tests/routes/uni.js | 51 ++++++++++++++++++++++++++++++--------- 5 files changed, 69 insertions(+), 13 deletions(-) diff --git a/src/controllers/report.js b/src/controllers/report.js index 8282dea..fe94fba 100644 --- a/src/controllers/report.js +++ b/src/controllers/report.js @@ -60,3 +60,13 @@ exports.getReportSummary = function ({ user, query }, res, next) { .then(getResponseHandler(res)) .catch(next) } + +exports.dismissReport = function ({ user, params }, res, next) { + if (user.permissions < PERMISSIONS_MOD) { + throw new APIError(ERRORS.MISC.AUTHORIZATION) + } + + reportModel.dismissReport(params.id) + .then(getResponseHandler(res)) + .catch(next) +} diff --git a/src/models/report.js b/src/models/report.js index 0e71fdf..26cc164 100644 --- a/src/models/report.js +++ b/src/models/report.js @@ -42,6 +42,20 @@ class Report { .then(([{ id }]) => id) } + /** + * Report Id + * @param {string} reportId + */ + dismissReport(reportId) { + return this.db + .run( + `DELETE ${REPORTS} WHERE id=@id;`, + { + [REPORTS]: { id: reportId } + } + ) + } + /** * Get all of dem reports for a given post * @param {object} queryObject diff --git a/src/routes/index.js b/src/routes/index.js index 8e44506..fa15de3 100644 --- a/src/routes/index.js +++ b/src/routes/index.js @@ -13,8 +13,8 @@ api.use('/course', courseRouter) api.use('/subject', subjectRouter) /* Root API for debugging */ -api.get('/', function (req, res) { - res.send('

Welcome to the API

') +api.get('/', function (_, res) { + res.sendStatus(200) }) api.use('*', function (_, res) { diff --git a/src/routes/uni.js b/src/routes/uni.js index 35ee078..d95da3a 100644 --- a/src/routes/uni.js +++ b/src/routes/uni.js @@ -23,4 +23,7 @@ uni.get('/sessions', uniController.getSessions) /* Return all posts with reports, sorted by number of reports */ uni.get('/reports', isModOrHigher, reportController.getReportSummary) +/* Delete the relevant report */ +uni.delete('/report/:id', isModOrHigher, reportController.dismissReport) + module.exports = uni diff --git a/tests/routes/uni.js b/tests/routes/uni.js index 02220bc..35d009c 100644 --- a/tests/routes/uni.js +++ b/tests/routes/uni.js @@ -30,7 +30,7 @@ describe('Uni route testing', function () { }) ) - describe('GET uni/reports', () => { + describe.only('GET uni/reports', () => { let request1, request2, request3 const report = { reason: 'It suuucks' } let getRequest @@ -56,16 +56,15 @@ describe('Uni route testing', function () { .set('Authorization', `Bearer ${global.idToken1}`) .expect(201) // get the reports - return Promise.all([request1, request2, request3]) - .then(() => { - getRequest = supertest - .get('/api/uni/reports') - .set('Accept', 'application/json') - .set('Authorization', `Bearer ${global.idTokenSuper}`) - .expect('Content-Type', /json/) - .expect(200) - return getRequest - }) + getRequest = Promise.all([request1, request2, request3]) + .then(() => supertest + .get('/api/uni/reports') + .set('Accept', 'application/json') + .set('Authorization', `Bearer ${global.idTokenSuper}`) + .expect('Content-Type', /json/) + .expect(200) + ) + return getRequest }) it('has 3 entries', () => @@ -81,6 +80,36 @@ describe('Uni route testing', function () { }) ) // NOTE we don't check the number of reports because of the async report test for questions + + describe('DELETE a report', () => { + let deleteRequest + + before('delete a report', () => { + deleteRequest = getRequest.then(() => + supertest + .delete('/api/uni/report/1') + .set('Accept', 'application/json') + .set('Authorization', `Bearer ${global.idTokenSuper}`) + .expect(200) + ) + + return deleteRequest + }) + + it('deletes', () => + deleteRequest.then( + () => supertest + .get('/api/uni/reports') + .set('Accept', 'application/json') + .set('Authorization', `Bearer ${global.idTokenSuper}`) + .expect('Content-Type', /json/) + .expect(200) + .then(({ body }) => { + expect(body.length).to.equal(2) + }) + ) + ) + }) }) describe('GET uni/reports (error)', () => { From 34be22d568cd302b39678dd972466a946a7a3708 Mon Sep 17 00:00:00 2001 From: Alex Hinds Date: Thu, 11 Apr 2019 20:57:49 +1000 Subject: [PATCH 3/3] A number of tests weren't actually promises which didn't guarantee execution behaviour as desired. Now fixed, by adding .then(). --- tests/routes/uni.js | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/tests/routes/uni.js b/tests/routes/uni.js index 35d009c..9046cf0 100644 --- a/tests/routes/uni.js +++ b/tests/routes/uni.js @@ -9,6 +9,7 @@ describe('Uni route testing', function () { .set('Accept', 'application/json') .expect('Content-Type', /json/) .expect(200) + .then(_ => _) ) it('GET uni/faculties', () => @@ -17,6 +18,7 @@ describe('Uni route testing', function () { .set('Accept', 'application/json') .expect('Content-Type', /json/) .expect(200) + .then(_ => _) ) it('GET uni/sessions', () => @@ -30,7 +32,7 @@ describe('Uni route testing', function () { }) ) - describe.only('GET uni/reports', () => { + describe('GET uni/reports', () => { let request1, request2, request3 const report = { reason: 'It suuucks' } let getRequest @@ -43,18 +45,24 @@ describe('Uni route testing', function () { .set('Authorization', `Bearer ${global.idToken0}`) .send(report) .expect(201) + .then(body => body) + request2 = supertest .post('/api/course/ACCT1501/question/1/report') .send(report) .set('Accept', 'application/json') .set('Authorization', `Bearer ${global.idToken1}`) .expect(201) + .then(body => body) + request3 = supertest .post('/api/course/ACCT1501/question/2/report') .send(report) .set('Accept', 'application/json') .set('Authorization', `Bearer ${global.idToken1}`) .expect(201) + .then(body => body) + // get the reports getRequest = Promise.all([request1, request2, request3]) .then(() => supertest @@ -63,6 +71,7 @@ describe('Uni route testing', function () { .set('Authorization', `Bearer ${global.idTokenSuper}`) .expect('Content-Type', /json/) .expect(200) + .then(body => body) ) return getRequest }) @@ -91,6 +100,7 @@ describe('Uni route testing', function () { .set('Accept', 'application/json') .set('Authorization', `Bearer ${global.idTokenSuper}`) .expect(200) + .then(res => res) ) return deleteRequest @@ -122,6 +132,8 @@ describe('Uni route testing', function () { .set('Authorization', `Bearer ${global.idToken0}`) .expect('Content-Type', /json/) .expect(403) + .then(data => data) + return getRequest }) @@ -141,6 +153,8 @@ describe('Uni route testing', function () { .set('Accept', 'application/json') .expect('Content-Type', /json/) .expect(200) + .then(data => data) + return getRequest })