Skip to content

Commit 50c4dd3

Browse files
cemersozDerekTBrown
authored andcommitted
Fixed testing, implemented validateLab (#97)
* issue 32 fixed * removed swp * fixed nconf issues * issue 82, 84 fixes * fixed #82 * isse #77 * fixed tests * fixed files * fixed tests master * fixed comment * moved validator * removed duplicate file
1 parent 69d8773 commit 50c4dd3

File tree

8 files changed

+122
-5
lines changed

8 files changed

+122
-5
lines changed

collections/labs.ts

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ import { Meteor } from 'meteor/meteor';
33

44
import { Roles } from './users.ts';
55

6+
declare var validateLab : any;
7+
import validateLab = require('../server/imports/lab/checkLab');
68
export const labs : any = new Mongo.Collection('labs');
79

810
/**
@@ -93,10 +95,30 @@ labs.allow({
9395
if(Meteor.isServer){
9496
Meteor.startup(function(){
9597
var LabValidator = function(userid, doc, fieldNames?, modifier?, options?){
96-
if (typeof fieldNames === "undefined" || fieldNames.includes('tasks') || fieldNames.includes('file')){
98+
if (typeof fieldNames === "undefined"){
99+
console.log("inserting");
100+
if(!(doc.course_id && doc.file && //check for lab fields
101+
(Meteor.isServer || Roles.isInstructorFor(doc.course_id,userid))&& //check for instructor authorization
102+
validateLab(doc.file))){ //check for labfile errors
103+
return false;
104+
}
105+
97106
//TODO @CEM: Validate Lab
98107
//TODO @CEM: Generate tasks array
99108
}
109+
else if(fieldNames.includes('tasks') && !fieldNames.includes('file')){
110+
return false;
111+
}
112+
else if(fieldNames.includes('file')){
113+
if(!((Meteor.isServer || Roles.isInstructorFor(doc.course_id,userid)) && //check for instructor authorization
114+
validateLab(modifier.$set.file))){ //check for labfile errors
115+
return false;
116+
}
117+
else{
118+
if(modifier) {modifier.$set.updated = Date.now(); }
119+
doc.updated = Date.now();
120+
}
121+
}
100122
}
101123
labs.before.update(LabValidator);
102124
labs.before.insert(LabValidator);

server/imports/lab/checkLab.d.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
declare module "validateLab" {
2+
export function validateLab(str: string): boolean
3+
}

server/imports/lab/checkLab.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
"use strict";
2+
module.exports = function(str){
3+
4+
if(!str) { return false; }
5+
var tux = eval(str);
6+
//var s = tuxlab;
7+
var tuxOrig = require('./tuxlab.js');
8+
return ((typeof tux != "undefined") &&
9+
(typeof tux.setup === 'function') && //check for instructor field types
10+
(typeof tux.tasks === 'function') &&
11+
(tux.init.toString() === tuxOrig.init.toString()) && //check for unchanged
12+
(tux.newTask.toString() === tuxOrig.newTask.toString()));
13+
14+
}

server/imports/lab/methods.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
declare var Collections : any;
22
Meteor.methods({
33
'prepareLab': function(courseId : String,labId : Number){
4-
var course = Collections.courses.findOne({_id: courseId}).fetch();
4+
console.log("here");
5+
// var course = Collections.courses.findOne({_id: courseId}).fetch();
56
//var t = require('../api/labExec.js');
6-
return course.labs.find((l) => { return l._id == labId; });
7+
// return course.labs.find((l) => { return l._id == labId; });
78
}
89
});
910

server/imports/lab/tuxlab.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
var tuxlab = function(){}
2+
3+
tuxlab.prototype.init = function(){
4+
var slf = this;
5+
var ts = { prnt: slf,
6+
next: null,
7+
nextTask: function(tsk){
8+
this.next = tsk;
9+
this.prnt.taskList.push(tsk);
10+
return tsk;
11+
}
12+
}
13+
this.currentTask = ts;
14+
return ts;
15+
}
16+
17+
tuxlab.prototype.currentTask = null;
18+
tuxlab.prototype.taskList = [];
19+
tuxlab.prototype.setup;
20+
tuxlab.prototype.tasks;
21+
tuxlab.prototype.newTask = function(ttl, mdown,sFn, vFn, opt){
22+
var slf = this;
23+
var ts = { prnt: slf,
24+
setupFn: sFn,
25+
verifyFn: vFn,
26+
opts: opt,
27+
markdown: mdown,
28+
title: ttl,
29+
next: null,
30+
completed: false,
31+
nextTask: function(tsk){
32+
this.next = tsk;
33+
this.prnt.taskList.push(tsk);
34+
return tsk;
35+
},
36+
isLast: function(){ return this.next === null; }
37+
}
38+
return ts;
39+
}
40+
module.exports = new tuxlab();

tests/example_data/badlabfile1.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
var tuxlab = require('./tuxlab.js');
2+
3+
tuxlab.setup = 5;
4+
tuxlab.tasks = function(env){
5+
var s1 = function(){
6+
env.start()
7+
.then(env.createVm({dockerodeCreateOptions: {name: "derek"}}))
8+
.then(env.shell("labVm","cd ~./home/usr/derek"))
9+
.then(function(sOut){ console.log("succ: "+sOut); },
10+
function(sOut,sErr,sDock){ throw sOut+sErr+sDock; });
11+
}
12+
var v1 = function(){
13+
env.shell("labVm","pwd")
14+
.then(function(sOut){ if(sOut === "/usr/home/derek") return true; else return false; });
15+
}
16+
var s2 = function(){}
17+
var v2 = function(){}
18+
var s3 = function(){}
19+
var v3 = function(){}
20+
var s4 = function(){}
21+
var v4 = function(){}
22+
var task1 = tuxlab.newTask('TASK1','MD1',s1,v1)
23+
var task2 = tuxlab.newTask('TASK2','MD2',s2,v2)
24+
var task3 = tuxlab.newTask('TASK3','MD3',s3,v3)
25+
var task4 = tuxlab.newTask('TASK4','MD4',s4,v4)
26+
return tuxlab.init()
27+
.nextTask(task1)
28+
.nextTask(task2)
29+
.nextTask(task3)
30+
.nextTask(task4);
31+
}
32+
module.exports = tuxlab;
33+
//export tuxlab

tests/example_data/labfile1.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,5 @@ tuxlab.tasks = function(env){
3737
.nextTask(task3)
3838
.nextTask(task4);
3939
}
40+
//export modified tuxlab
4041
module.exports = tuxlab;

tests/tuxlab.db.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,17 +52,20 @@ describe('Database Schema', function(){
5252

5353
return server.promise(function(resolve, reject, labfile, course_id){
5454
var example_lab = {
55+
_id : "574467bc11091623418a429d",
5556
course_id : course_id,
5657
lab_name: "Getting Started with Git",
5758
file: labfile,
5859
tasks: [
5960
{
6061
_id: 1,
62+
updated: 1467995862937,
6163
name: "Git Clone",
6264
md: "##################"
6365
},
6466
{
6567
_id: 2,
68+
updated: 1467995862937,
6669
name: "Git Pull",
6770
md: "##################"
6871
}
@@ -85,13 +88,13 @@ describe('Database Schema', function(){
8588
// Validate that Records Exists
8689
it('should be accepted by database', function(){
8790
return server.execute(function(course_id, lab_id){
88-
var course = Collections.courses.findOne(course_id).fetch();
91+
var course = Collections.courses.findOne(course_id);
8992

9093
// Check Lab Injection
9194
expect(course).to.have.property('labs');
9295
expect(course.labs).to.include(lab_id);
9396

94-
var lab = Collections.findOne(lab_id).fetch();
97+
var lab = Collections.labs.findOne(lab_id);
9598

9699
// Confirm LabFile verifications were run
97100

0 commit comments

Comments
 (0)