Skip to content
This repository has been archived by the owner on Apr 16, 2024. It is now read-only.

Validate input for update notification settings #771

Merged
merged 6 commits into from
Jun 10, 2018
Merged
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
3 changes: 2 additions & 1 deletion .travis/coveralls.sh
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ echo "+ sending lcov file to coveralls"
# since we are using typescript and remap our coverage data to the ts files we need to remove the "build" part of all paths
# this could easily be done with some sed magic
# search for "api/build/src" and replace it with "api/src"
sed "s/api\/build\/src/api\/src/g" api/coverage/lcov.info | $BIN_PATH_FULL/coveralls -v
sed -i "s/api\/build\/src/api\/src/g" api/coverage/lcov.info
cat api/coverage/lcov.info | $BIN_PATH_FULL/coveralls -v

echo "+ INFO: Currently only the api-coverdata are generated and send"

1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
- Fixed broken Apidoc [#737](https://github.com/h-da/geli/issues/737)
- Disabled `tutor` role. [#710](https://github.com/h-da/geli/issues/710)
- Fixed notifications on hidden units. [#733](https://github.com/utetrapp/geli/issues/733)
- Validate user input for notication settings api. [#771](https://github.com/utetrapp/geli/issues/771)

## [[0.7.0](https://github.com/h-da/geli/releases/tag/v0.7.0)] - 2018-05-05 - SS 18 intermediate Release
### Added
Expand Down
8 changes: 6 additions & 2 deletions api/src/controllers/NotificationSettingsController.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import {Authorized, BadRequestError, Body, Get, JsonController, Param, Post, Put, UseBefore} from 'routing-controllers';
import passportJwtMiddleware from '../security/passportJwtMiddleware';
import {API_NOTIFICATION_TYPE_ALL_CHANGES, INotificationSettingsModel, NotificationSettings} from '../models/NotificationSettings';
import {
API_NOTIFICATION_TYPE_ALL_CHANGES,
INotificationSettingsModel,
NotificationSettings
} from '../models/NotificationSettings';
import {INotificationSettings} from '../../../shared/models/INotificationSettings';

@JsonController('/notificationSettings')
Expand Down Expand Up @@ -81,7 +85,7 @@ export class NotificationSettingsController {
@Authorized(['student', 'teacher', 'admin'])
@Put('/:id')
async updateNotificationSettings(@Param('id') id: string, @Body() notificationSettings: INotificationSettings) {
if (!notificationSettings) {
if (!notificationSettings.course || !notificationSettings.user) {
throw new BadRequestError('notification needs fields course and user');
}
const settings: INotificationSettingsModel =
Expand Down
55 changes: 53 additions & 2 deletions api/test/controllers/TestNotificationSettingsController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@ import {Server} from '../../src/server';
import {FixtureLoader} from '../../fixtures/FixtureLoader';
import {FixtureUtils} from '../../fixtures/FixtureUtils';
import {JwtUtils} from '../../src/security/JwtUtils';
import {API_NOTIFICATION_TYPE_ALL_CHANGES, API_NOTIFICATION_TYPE_NONE, NotificationSettings} from '../../src/models/NotificationSettings';
import {
API_NOTIFICATION_TYPE_ALL_CHANGES,
API_NOTIFICATION_TYPE_NONE,
NotificationSettings
} from '../../src/models/NotificationSettings';
import {User} from '../../src/models/User';
import {Course} from '../../src/models/Course';
import chaiHttp = require('chai-http');
Expand All @@ -22,7 +26,7 @@ describe('NotificationSettings', async () => {
describe(`POST ${BASE_URL}`, async () => {
it('should create notification settings', async () => {
const course = await FixtureUtils.getRandomCourse();
const student = await User.findById(course.students[0]);
const student = course.students[0];
const newSettings = {user: student, course: course};

const res = await chai.request(app)
Expand All @@ -40,6 +44,35 @@ describe('NotificationSettings', async () => {
notificationSettings.user.toString().should.be.equal(newSettings.user._id.toString());
notificationSettings.course.toString().should.be.equal(newSettings.course._id.toString());
});

it('should fail when already exist', async () => {
const course = await FixtureUtils.getRandomCourse();
const student = course.students[0];
const newSettings = {user: student, course: course};

const res = await chai.request(app)
.post(BASE_URL)
.set('Authorization', `JWT ${JwtUtils.generateToken(student)}`)
.send(newSettings);
res.status.should.be.equals(200);

const resFail = await chai.request(app)
.post(BASE_URL)
.set('Authorization', `JWT ${JwtUtils.generateToken(student)}`)
.send(newSettings);
resFail.status.should.be.equals(400);
});

it('should fail when course or user missing', async () => {
const course = await FixtureUtils.getRandomCourse();
const student = course.students[0];

const res = await chai.request(app)
.post(BASE_URL)
.set('Authorization', `JWT ${JwtUtils.generateToken(student)}`)
.send({});
res.status.should.be.equals(400);
});
});

describe(`GET ${BASE_URL} user :id`, () => {
Expand Down Expand Up @@ -102,6 +135,24 @@ describe('NotificationSettings', async () => {
res.body.should.have.property('course');
res.body._id.should.be.a('string');
});

it('should fail when missing course or user', async () => {
const course = await FixtureUtils.getRandomCourse();
const student = course.students[Math.floor(Math.random() * course.students.length)];

const settings = await new NotificationSettings({
'user': student,
'course': course,
'notificationType': API_NOTIFICATION_TYPE_ALL_CHANGES,
'emailNotification': false
}).save();

const res = await chai.request(app)
.put(`${BASE_URL}/${settings._id}`)
.set('Authorization', `JWT ${JwtUtils.generateToken(student)}`)
.send([]);
res.should.have.status(400);
});
});
});