Skip to content

Commit d14d799

Browse files
author
Jon Myrick
authored
Merge pull request CodewarsClone#24 from CodewarsClone/testCtrl
Finished Oauth and testing suite ability
2 parents 09d051d + 388bc41 commit d14d799

7 files changed

Lines changed: 76 additions & 50 deletions

File tree

controllers/testCtrl.js

Lines changed: 30 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -5,56 +5,54 @@
55
*
66
*/
77

8-
// This is where wer run a script.. I have verified that so long as we get it in string format we run unit tests
9-
10-
/*exec(`docker run --rm codewars/node-runner run -l javascript -c "${script}" -t cw -f "${test}"`,
11-
(err, stdout, stderr) => {
12-
if (err) console.log('err', err);
13-
console.log(typeof stdout);
14-
console.log('stdout', stdout);
15-
console.log('stderr', stderr);
16-
});*/
178

189
const app = require('../server');
19-
2010
const db = app.get('db');
21-
11+
const Q = require('q');
2212
const exec = require('child_process').exec;
2313

24-
let test = `Test.assertEquals(addTwo(2), 4)`;
2514

2615
module.exports = {
27-
testScript: (req, res, next) => {
28-
console.log('body');
29-
console.log(req.body);
30-
console.log('params', req.params);
16+
testKata: (req, res, next) => {
17+
console.log('hit testCtrl');
3118

3219
let body = req.body;
3320

3421

3522
db.read.kata_by_id([req.params.kataId], (err, kataArray)=>{
3623
if(err) console.log(err);
3724
let kata = kataArray[0];
25+
let promiseArr = [];
26+
27+
28+
3829

39-
exec(`docker run --rm codewars/node-runner run -l javascript -c "${body.script}" -t cw -f "${kata.test}"`,
40-
(err, stdout, stderr) => {
41-
if (err) {
42-
console.log('err', err);
43-
} else if (stdout) {
44-
res.json(stdout);
45-
} else if (stderr) {
46-
res.json(stderr);
47-
}
48-
return
49-
});
30+
kata.test_script.forEach((ele, i) =>{
31+
let deffered = Q.defer();
32+
33+
exec(`docker run --rm codewars/node-runner run -l javascript -c "${body.script}" -t cw -f "${ele.test}"`,
34+
(err, stdOut, stdErr) => {
35+
if (err) {
36+
console.log(err);
37+
} else if (stdOut) {
38+
deffered.resolve(stdOut);
39+
ele.result = stdOut;
40+
} else if (stdErr) {
41+
deffered.resolve(stdErr);
42+
ele.result = stdErr;
43+
}
44+
});
45+
46+
promiseArr.push(deffered.promise)
47+
});
48+
49+
50+
Q.all(promiseArr).then((response) => {
51+
res.json(kata.test_script);
52+
})
5053
});
5154

5255

53-
54-
},
55-
56-
testKata: (req, res, next) => {
57-
5856
},
5957

6058
testExamplesKata: (req, res, next) => {

db/create/new_user_from_github.sql

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
INSERT INTO users (github_id, first_name, last_name, email, username, picutre_url)
2+
VALUES ($1, null, null, $2, $3, $4);

db/read/user_by_github_id.sql

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
select * from users
2+
where github_id = $1;

db/start/tables.sql

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
DROP TABLE IF EXISTS rating, solutions, kata, users;
1+
DROP TABLE IF EXISTS ratings, rating, solutions, katas, kata, users;
22

33
CREATE TABLE users (
44
id serial primary key,
@@ -14,33 +14,33 @@ CREATE TABLE users (
1414
CREATE TABLE katas (
1515
id serial primary key,
1616
kyu integer not null,
17-
test_script text not null,
17+
test_script json not null,
1818
description text,
1919
starter_code text not null,
2020
name varchar(255),
21-
examples text
21+
examples json
2222
);
2323

2424
CREATE TABLE solutions (
2525
id serial primary key,
2626
user_id integer references users(id),
27-
kata_id integer references kata(id),
27+
kata_id integer references katas(id),
2828
script text not null
2929
);
3030

3131
CREATE TABLE ratings (
3232
id serial primary key,
3333
user_id integer references users(id),
34-
kata_id integer references kata(id),
34+
kata_id integer references katas(id),
3535
solution_id integer references solutions(id),
3636
liked boolean not null
3737
);
3838

39-
INSERT INTO users (github_id, first_name, last_name, email, username, picture_url)
40-
VALUES (null, 'bob', 'smith', 'bob@smith.com', 'bobIScool', null);
39+
INSERT INTO users (github_id, first_name, last_name, email, username, picutre_url)
40+
VALUES ('12', 'bob', 'smith', 'bob@smith.com', 'bobIScool', null);
4141

42-
INSERT INTO katas (kyu, test_script, description, starter_code, name, examples)
43-
VALUES (8, 'Test.assertEquals(a, 1)', 'a should equal 1', 'var a = 1', 'Sumbit This', null);
42+
INSERT INTO katas (kyu, description, starter_code, name, examples, test_script)
43+
VALUES (8, 'var a should equal 1', 'var a = 1', 'Sumbit This', '[{"test":"Test.assertEquals(a, 1)","result":""}]', '[{"test":"Test.assertEquals(a, 0)","result":""},{"test":"Test.assertEquals(a, 2)","result":""},{"test":"Test.assertEquals(a, 1)","result":""}]');
4444

4545
INSERT INTO solutions (user_id, kata_id, script)
4646
VALUES (1, 1, 'var a = 1'),

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
"gulp-uglify": "^2.0.0",
3535
"massive": "^2.5.0",
3636
"passport": "^0.3.2",
37-
"passport-github2": "^0.1.10"
37+
"passport-github2": "^0.1.10",
38+
"q": "^1.4.1"
3839
}
3940
}

server.js

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,35 @@ passport.use(new GithubStrategy({
2222
clientSecret: config.githubSecret,
2323
callbackURL: '/auth/github/callback'
2424
}, (accessToken, refreshToken, profile, done) => {
25-
// code goes here
26-
// go to database and look for profile.id
27-
// create user using profile.id
28-
return done(null/*error*/, profile/*info that goes on session*/);
25+
db.read.user_by_github_id([profile.id], (err, user) => {
26+
console.log(user);
27+
if (err) {
28+
console.log(err);
29+
} else if (user [0]) {
30+
done(null, user);
31+
} else {
32+
console.log('attempting account creation')
33+
db.create.new_user_from_github([profile.id, profile._json.email, profile.displayName, profile._json.avatar_url],
34+
(err) => {
35+
if (err) {
36+
console.log(err);
37+
} else {
38+
db.read.user_by_github_id([profile.id], (err, newUser) => {
39+
if(err) {
40+
console.log(err);
41+
} else {
42+
console.log(newUser);
43+
done(null, newUser[0])
44+
}
45+
});
46+
}
47+
})
48+
}
49+
50+
});
51+
52+
53+
// return done(null, profile);
2954
}));
3055

3156

@@ -57,7 +82,7 @@ app.use(passport.session());
5782

5883

5984

60-
app.get('/auth/github', passport.authenticate('github'));
85+
app.get('/auth/github', passport.authenticate('github', {scope: ['user:email', 'user']}));
6186

6287
app.get('/auth/github/callback',
6388
passport.authenticate('github', {failureRedirect: '/login'}),
@@ -79,8 +104,6 @@ app.post('/test/:kataId', testCtrl.testKata);
79104
app.post('/test/examples/:kataId', testCtrl.testExamplesKata);
80105
app.post('/solution/:kataId', kataCtrl.postSolution);
81106

82-
app.put('/test/:kataId', testCtrl.testScript);
83-
84107

85108

86109
app.listen(config.port, function () {

src/login.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
<p class='font16-reg logo-font logo'>clonewars</p><br><br>
1313
</div>
1414
<div>
15-
<a href='http://www.github.com' style=>
15+
<a href='/auth/github' style=>
1616
<div class='button font12-reg'>
1717
<div>
1818
<!--<img src='../src/assets/imgs/ST.png' class='button-icon'>-->

0 commit comments

Comments
 (0)