Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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
RUN dpkg-reconfigure -f noninteractive locales

ENV PATH="/opt/mssql-tools/bin:${PATH}"
ENV ACCEPT_EULA="Y"
ARG SA_PASSWORD

EXPOSE 1433
14 changes: 14 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -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'

21 changes: 8 additions & 13 deletions docker.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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 \_(* *)_/
10 changes: 10 additions & 0 deletions src/controllers/report.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this check shouldn't be needed because isModOrHigher middleware is run on the route

throw new APIError(ERRORS.MISC.AUTHORIZATION)
}

reportModel.dismissReport(params.id)
.then(getResponseHandler(res))
.catch(next)
}
2 changes: 1 addition & 1 deletion src/models/db/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class DB extends EventEmitter {
super()

console.log('Connecting to database')

this.connections = []
this.ready = false

Expand Down
14 changes: 14 additions & 0 deletions src/models/report.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions src/routes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ api.use('/course', courseRouter)
api.use('/subject', subjectRouter)

/* Root API for debugging */
api.get('/', function (req, res) {
res.send('<h1>Welcome to the API</h1>')
api.get('/', function (_, res) {
res.sendStatus(200)
})

api.use('*', function (_, res) {
Expand Down
3 changes: 3 additions & 0 deletions src/routes/uni.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
63 changes: 53 additions & 10 deletions tests/routes/uni.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ describe('Uni route testing', function () {
.set('Accept', 'application/json')
.expect('Content-Type', /json/)
.expect(200)
.then(_ => _)
)

it('GET uni/faculties', () =>
Expand All @@ -17,6 +18,7 @@ describe('Uni route testing', function () {
.set('Accept', 'application/json')
.expect('Content-Type', /json/)
.expect(200)
.then(_ => _)
)

it('GET uni/sessions', () =>
Expand All @@ -43,29 +45,35 @@ 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
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)
.then(body => body)
)
return getRequest
})

it('has 3 entries', () =>
Expand All @@ -81,6 +89,37 @@ 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)
.then(res => res)
)

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)', () => {
Expand All @@ -93,6 +132,8 @@ describe('Uni route testing', function () {
.set('Authorization', `Bearer ${global.idToken0}`)
.expect('Content-Type', /json/)
.expect(403)
.then(data => data)

return getRequest
})

Expand All @@ -112,6 +153,8 @@ describe('Uni route testing', function () {
.set('Accept', 'application/json')
.expect('Content-Type', /json/)
.expect(200)
.then(data => data)

return getRequest
})

Expand Down