Skip to content
This repository was archived by the owner on Oct 12, 2022. It is now read-only.

Implemented tests for imageboard #40

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
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
81 changes: 81 additions & 0 deletions imageboard/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
typings/

### WebStorm ###
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio

*.iml

## Directory-based project format:
.idea/
# if you remove the above rule, at least ignore the following:

# User-specific stuff:
# .idea/workspace.xml
# .idea/tasks.xml
# .idea/dictionaries

# Sensitive or high-churn files:
# .idea/dataSources.ids
# .idea/dataSources.xml
# .idea/sqlDataSources.xml
# .idea/dynamic.xml
# .idea/uiDesigner.xml

# Gradle:
# .idea/gradle.xml
# .idea/libraries

# Mongo Explorer plugin:
# .idea/mongoSettings.xml

## File-based project format:
*.ipr
*.iws

## Plugin-specific files:

# IntelliJ
/out/

# mpeltonen/sbt-idea plugin
.idea_modules/

# JIRA plugin
atlassian-ide-plugin.xml

# Crashlytics plugin (for Android Studio and IntelliJ)
com_crashlytics_export_strings.xml
crashlytics.properties
crashlytics-build.properties


### Node ###
# Logs
logs
*.log
npm-debug.log*

# Runtime data
pids
*.pid
*.seed

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage

# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# node-waf configuration
.lock-wscript

# Compiled binary addons (http://nodejs.org/api/addons.html)
build/Release

# Dependency directory
# https://docs.npmjs.com/misc/faq#should-i-check-my-node-modules-folder-into-git
node_modules

7 changes: 5 additions & 2 deletions imageboard/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ Notable features:

## Running

Note: Perform steps 3 - 6 with your working directory set to the folder containing this README:
Note: Perform steps 3 - 6 with your working directory set to the folder containing this README.md file:

1. Install MongoDB if necessary (see http://docs.mongodb.org/manual/installation/ )

2. Run the following command to launch the MongoDB process:
2. Ensure MongoDB is running, e.g.: by launching it manually:
`<mongoinstalldir>\bin\mongod`

3. Restore the sample app data to MongoDB in another command prompt with the following command:
Expand All @@ -25,6 +25,9 @@ Note: Perform steps 3 - 6 with your working directory set to the folder containi
4. Install the app's node dependencies with the following command:
`npm install`

5. After ensuring that `tsd` is available globally (`npm install -g tsd`), install the typings with the following command:
`tsd install`

5. Compile the app with the following command:
`tsc --sourcemap --module commonjs app.ts`

Expand Down
57 changes: 29 additions & 28 deletions imageboard/app.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
/// <reference path='typings/node/node.d.ts' />
/// <reference path='typings/mongodb/mongodb.d.ts' />
/// <reference path='typings/express/express.d.ts' />
/// <reference path='typings/express/express-middleware.d.ts' />

/// <reference path='typings/body-parser/body-parser.d.ts' />
/// <reference path='typings/method-override/method-override.d.ts' />
/// <reference path='typings/errorhandler/errorhandler.d.ts' />

import http = require("http")
import url = require("url")
Expand All @@ -22,10 +23,10 @@ app.set('view engine', 'jade');
app.set('view options', { layout: false });
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
app.use(methodOverride());
app.use(methodOverride(""));
app.use(express.static(__dirname + '/public'));

var env = process.env.NODE_ENV || 'development';
const env = process.env.NODE_ENV || 'development';
if (env === 'development') {
app.use(errorHandler({ dumpExceptions: true, showStack: true }));
}
Expand All @@ -40,43 +41,43 @@ app.get('/', routes.index);

app.get('/findImages', function(req, res) {
console.log('getting images from' + req.query['url']);
var req2 = http.get(url.parse(req.query['url']), function(urlres) {
console.log("Got response: " + urlres.statusCode);

http.get(url.parse(req.query['url']), function(urlres) {
console.log("Got response: " + urlres.statusCode);
var text = "";
urlres.on('data', function(chunk: string) {
urlres.on('data', function(chunk: string) {
text += chunk;
});
});
urlres.on('end', function() {
console.log(text);
var re = /<img[^>]+src=[\"\']([^\'\"]+)[\"\']/g;
console.log(text);
const re = /<img[^>]+src=[\"\']([^\'\"]+)[\"\']/g;
var match, matches = [];
while(match = re.exec(text)) {
matches.push(match[1]);
}
res.write(JSON.stringify(matches));
res.end();
});
}).on('error', function(a,e) {
console.log("Got error: " + e.message);
});
}).on('error', function(a,e) {
console.log("Got error: " + e.message);
});
});

app.get('/user/:userid', function(req, res) {
console.log('getting user ' + req.params.userid);
db.getUser(req.params.userid, function(user) {
res.render('user', {
res.render('user', {
title: user._id,
username: user._id,
boards: user.boards
username: user._id,
boards: user.boards
});
});
});

app.get('/user/:userid/newboard', function(req, res) {
res.render('newboard', {
res.render('newboard', {
username: req.params.userid
});
});
});

app.post('/user/:userid/newboard', function(req, res) {
Expand All @@ -88,14 +89,14 @@ app.post('/user/:userid/newboard', function(req, res) {
app.get('/user/:userid/:boardid', function(req, res) {
console.log('getting ' + req.params.userid + ", " + req.params.boardid);
db.getUser(req.params.userid, function(user) {
var board = user.boards.filter(function(board) {
const board = user.boards.filter(function(board) {
return decodeURIComponent(req.params.boardid) === board.title;
})[0];
if(board) {
db.getImages(board.images, function(images) {
res.render('board', {
res.render('board', {
title: user._id,
username: user._id,
username: user._id,
board: board,
images: images
});
Expand All @@ -107,10 +108,10 @@ app.get('/user/:userid/:boardid', function(req, res) {
});

app.get('/user/:userid/:boardid/newpin', function(req, res) {
res.render('newpin', {
res.render('newpin', {
username: req.params.userid,
boardid: req.params.boardid
});
});
});

app.post('/user/:userid/:boardid/newpin', function(req, res) {
Expand All @@ -122,15 +123,15 @@ app.post('/user/:userid/:boardid/newpin', function(req, res) {
app.get('/image/:imageid', function(req, res) {
console.log('getting image ' + req.params.imageid);
db.getImage(req.params.imageid, function(image) {
res.render('image', {
res.render('image', {
title: "image",
image: image
});
});
});

app.listen(3000, function(){
console.log("Demo Express server listening on port %d in %s mode", 3000, app.settings.env);
app.listen(process.env.PORT || 3000, function() {
console.log("Demo Express server listening on port %d in %s mode", this.address().port, app.settings.env);
});

export var App = app;
export var App = app;
35 changes: 23 additions & 12 deletions imageboard/package.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,25 @@
{
"name": "imageboard-sample",
"version": "0.0.1",
"private": true,
"dependencies": {
"express": "4.11.1",
"body-parser": "1.10.2",
"errorhandler": "1.3.2",
"method-override": "2.3.1",
"ejs": ">= 0.5.0",
"jade": ">= 0.0.1",
"mongodb": ">= 1.4.29"
"name": "imageboard-sample",
"version": "0.0.2",
"private": true,
"scripts": {
"test": "mocha -u exports -R spec test/test_*.js",
"test_ts": "mocha --compilers ts:typescript-require -u exports -R spec test/test_*.ts",
"start": "node app.js"
},
"dependencies": {
"body-parser": "1.10.2",
"ejs": ">= 0.5.0",
"errorhandler": "1.3.2",
"express": "^4.13.3",
"jade": ">= 0.0.1",
"method-override": "2.3.1",
"mongodb": ">=1.3.0"
},
"devDependencies": {
"chai": "^3.2.0",
"mocha": "^2.3.2",
"supertest": "^1.1.0",
"typescript-require": "^0.2.9"
}
}
}
28 changes: 28 additions & 0 deletions imageboard/test/test_routes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
///<reference path='../typings/supertest/supertest.d.ts'/>

/// <reference path="../typings/mocha/mocha.d.ts" />
/// <reference path="../typings/chai/chai.d.ts" />

import supertest = require('supertest');
import app = require('../app');
const request = supertest(app.App);

describe('GET /', function () {
it('respond with HTML', function (done) {
request
.get('/')
.set('Accept', 'text/html; charset=utf-8')
.expect('Content-Type', 'text/html; charset=utf-8')
.expect(200, done);
})
});

describe('GET /findImages', function () {
it('respond with HTML', function (done) {
request
.get('/findImages')
.set('Accept', 'text/html; charset=utf-8')
.query({url: 'http://www.mememaker.net/static/images/memes/4269694.jpg'})
.expect(200, done);
})
});
45 changes: 45 additions & 0 deletions imageboard/tsd.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
{
"version": "v4",
"repo": "borisyankov/DefinitelyTyped",
"ref": "master",
"path": "typings",
"bundle": "typings/tsd.d.ts",
"installed": {
"superagent/superagent.d.ts": {
"commit": "7a3ca1f0b8a0960af9fc1838f3234cc9d6ce0645"
},
"supertest/supertest.d.ts": {
"commit": "7a3ca1f0b8a0960af9fc1838f3234cc9d6ce0645"
},
"node/node.d.ts": {
"commit": "52b0ea5c9719831eecf6ba7436660e30061a4b3c"
},
"mocha/mocha.d.ts": {
"commit": "7a3ca1f0b8a0960af9fc1838f3234cc9d6ce0645"
},
"chai/chai.d.ts": {
"commit": "7a3ca1f0b8a0960af9fc1838f3234cc9d6ce0645"
},
"mongodb/mongodb.d.ts": {
"commit": "52b0ea5c9719831eecf6ba7436660e30061a4b3c"
},
"serve-static/serve-static.d.ts": {
"commit": "52b0ea5c9719831eecf6ba7436660e30061a4b3c"
},
"mime/mime.d.ts": {
"commit": "52b0ea5c9719831eecf6ba7436660e30061a4b3c"
},
"express/express.d.ts": {
"commit": "52b0ea5c9719831eecf6ba7436660e30061a4b3c"
},
"body-parser/body-parser.d.ts": {
"commit": "52b0ea5c9719831eecf6ba7436660e30061a4b3c"
},
"method-override/method-override.d.ts": {
"commit": "52b0ea5c9719831eecf6ba7436660e30061a4b3c"
},
"errorhandler/errorhandler.d.ts": {
"commit": "52b0ea5c9719831eecf6ba7436660e30061a4b3c"
}
}
}
23 changes: 0 additions & 23 deletions imageboard/typings/express/express-middleware.d.ts

This file was deleted.

Loading