Skip to content

Commit

Permalink
As an admin, I can log in
Browse files Browse the repository at this point in the history
closes #1
  • Loading branch information
jonstorer committed Sep 10, 2012
1 parent f754c04 commit 850ec23
Show file tree
Hide file tree
Showing 24 changed files with 331 additions and 1 deletion.
Binary file added .DS_Store
Binary file not shown.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@ lib-cov
*.out
*.pid
*.gz
*.sw[nomp]

pids
logs
results

node_modules
npm-debug.log
npm-debug.log
21 changes: 21 additions & 0 deletions app/controllers/admin/sessions_controller.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
BaseController = require './../base_controller'
{ mongoose } = require '../../../config/database'
Administrator = mongoose.model('Administrator')

Admin = {}

class Admin.SessionsController extends BaseController
new: ->
@response.render 'admin/sessions/new'

create: ->
params =
username: @request.body.admin.username
password: @request.body.admin.password

Administrator.findOne params, (error, administrator) =>
throw error if error
@request.session.administrator_id = administrator._id
@response.redirect '/'

module.exports = Admin
4 changes: 4 additions & 0 deletions app/controllers/base_controller.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
class BaseController
constructor: (@request, @response) ->

module.exports = BaseController
14 changes: 14 additions & 0 deletions app/controllers/homes_controller.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
BaseController = require './base_controller'
{ mongoose } = require '../../config/database'
Administrator = mongoose.model('Administrator')


class HomesController extends BaseController
show: ->
_id = @request.session.administrator_id
Administrator.findOne { _id: _id }, (error, administrator) =>
throw error if error
@response.render 'homes/show',
administrator: administrator

module.exports = HomesController
9 changes: 9 additions & 0 deletions app/models/administrator.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{ mongoose, Schema } = require '../../config/database'

AdministratorSchema = new Schema
username: { type: String, trim: true }
password: { type: String, trim: true }
name: { type: String, trim: true }
created_at: { type : Date, default : Date.now }

mongoose.model('Administrator', AdministratorSchema)
10 changes: 10 additions & 0 deletions app/views/admin/sessions/new.jade
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
html
#container
form(action='sessions/create', method='post')
label(for='admin_username') username
input(id='admin_username', name='admin[username]')
br
label(for='admin_password') password
input(id='admin_password', name='admin[password]')
br
input(type='submit', name='Login')
5 changes: 5 additions & 0 deletions app/views/homes/show.jade
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
html
#container
a(href='admin/login') Login
if administrator != null
p "Logged in as #{ administrator.name }"
11 changes: 11 additions & 0 deletions config/app.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
default:
db:
uri: 'mongodb://localhost/tilt_development'
app:
port: 3000

test:
db:
uri: 'mongodb://localhost/tilt_test'
app:
port: 3030
25 changes: 25 additions & 0 deletions config/application.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
express = require 'express'
path = require 'path'
config = require 'yaml-config'
{ mongoose } = require './database'

settings = config.readConfig('config/app.yaml', process.env.NODE_ENV)

module.exports = (app) ->

app.configure ->

app.set 'port', process.env.PORT || settings.app.port || 3000
app.set 'views', "#{app.root}/app/views"
app.set 'view engine', 'jade'
app.use express.favicon()
app.use express.bodyParser()
app.use express.methodOverride()
app.use express.cookieParser('2ee27441d3ee4a527de019325dc7e8ddee6039cc7cad9801181c6fd68204129bdf3225cafabb3f6d0324a7bd851dabbd0bd3')
app.use express.session()
app.use app.router
app.use express.static("#{ app.root }/public")

app.configure 'development', ->
app.use express.logger('dev')
app.use express.errorHandler()
8 changes: 8 additions & 0 deletions config/bootstrap.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
fs = require 'fs'

module.exports = (app) ->
# Bootstrap models

for file in fs.readdirSync(app.root + '/app/models/')
if file.match(new RegExp(/.*\.coffee$/))
require app.root + '/app/models/' + file
10 changes: 10 additions & 0 deletions config/database.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
mongoose = require 'mongoose'
config = require 'yaml-config'

settings = config.readConfig('config/app.yaml', process.env.NODE_ENV)

mongoose.connect settings.db.uri

module.exports =
mongoose: mongoose
Schema: mongoose.Schema
13 changes: 13 additions & 0 deletions config/routes.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
HomesController = require './../app/controllers/homes_controller'
Admin = require './../app/controllers/admin/sessions_controller'

module.exports = (app) ->

app.get '/', (request, response) ->
new HomesController(request, response).show()

app.get '/admin/login', (request, response) ->
new Admin.SessionsController(request, response).new()

app.post '/admin/sessions/create', (request, response) ->
new Admin.SessionsController(request, response).create()
13 changes: 13 additions & 0 deletions features/admin_logs_in.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
Feature: Admin creates an Event
As an admin, I can create an event

Scenario: There is a view results link next to all my completed actions
Given an admin exists with the following information:
| username | password | name |
| admin | nimda | Admin McAdmin |
When I am on the homepage
And I click "Login"
And I fill in "admin[username]" with "admin"
And I fill in "admin[password]" with "nimda"
When I press "Login"
Then I should see "Logged in as Admin"
14 changes: 14 additions & 0 deletions features/step_definitions/admin_steps.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{ mongoose } = require '../../config/database'

Administrator = mongoose.model('Administrator')

steps = module.exports = ->

@World = require('../support/world').World

@Then /^an admin exists with the following information:$/, (table, next) ->
@_(table.hashes()).each (row) =>
administrator = new Administrator row
administrator.save (error) ->
throw error if error
next()
33 changes: 33 additions & 0 deletions features/step_definitions/shared_steps.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
should = require 'should'

steps = module.exports = ->

@World = require('../support/world').World

@After (next) ->
@clean next

@Then /^I (?:am on|go to) (.+)$/, (path, next) ->
@browser.visit(@selectorFor(path), next)

@Then /^I fill in "(.+)" with "(.+)"$/, (name, value, next) ->
@browser.fill(name, value, next)

@Then /^I press "(.+)"$/, (name, next) ->
@browser.pressButton(name, next)

@Then /^I click "(.+)"$/, (name, next) ->
@browser.clickLink(name, next)

@Then /^I should see "(.+)"$/, (text, next) ->
@browser.html().should.include(text)
next()

@Then /^show me the page$/, (next) ->
@browser.wait =>
if @browser.errors.length
console.log "\nBrowser Errors:", @browser.errors
console.log @browser.html()
#@browser.viewInBrowser()

next()
12 changes: 12 additions & 0 deletions features/support/selectors.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module.exports =
# Elements

'^(.*) within (.*)$': (inner, outer) ->
return "#{@selectorFor(outer)} #{@selectorFor(inner)}"

'^(.*) containing "([^"]*)"$': (namedElement, text) ->
return "#{@selectorFor(namedElement)}:contains('#{text}')"

# Paths
'the home ?page': '/'
'the admin login page': '/admin/login/'
52 changes: 52 additions & 0 deletions features/support/world.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
process.env.NODE_ENV = 'test'

app = require '../../tilt'

{Factory} = require 'forgery'
selectors = require './selectors'
_ = require 'underscore'
{ mongoose } = require '../../config/database'
DatabaseCleaner = require 'database-cleaner'
zombie = require 'zombie'
zombie.site = "http://localhost:#{ app.get('port') }/"

databaseCleaner = new DatabaseCleaner('mongodb')

require '../../spec/factories'

class World
constructor: (callback) ->
# this.browser will be available in step definitions
@browser = new zombie.Browser()
@_ = _
@selectors = selectors

# tell Cucumber we're finished and to use 'this' as the world instance
callback(@)

clean: (next) ->
databaseCleaner.clean(mongoose.connection.db, next)

selectorFor: (selector) ->
for regexp, path of @selectors
match = selector.match(new RegExp(regexp))

if match
if typeof path == 'string'
return path
else
return path.apply @, match.slice(1)

throw new Error("Could not find path for '#{selector}'")

Factory: (factoryName, options) ->
for key, value of options
options[key] = switch value
when 'true' then true
when 'false' then false
when 'null' then null
else value

Factory factoryName, options

module.exports.World = World
30 changes: 30 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"name": "tiltnyc",
"version": "0.0.1",
"private": true,
"scripts": {
"start": "node app",
"specs": "./node_modules/.bin/mocha --compilers coffee:coffee-script spec/*/*spec*",
"features": "./node_modules/.bin/cucumber-js features/*",
"build": "./node_modules/.bin/mocha --compilers coffee:coffee-script spec/*/*spec* && ./node_modules/.bin/cucumber-js features/*"
},
"dependencies": {
"coffee-script": "1.3.3",
"express": "3.0.0rc4",
"fs": "latest",
"jade": "0.27.2",
"mongoose": "3.1.1",
"package": "latest",
"underscore": "1.3.1",
"walk": "latest",
"yaml-config": "latest"
},
"devDependencies": {
"cucumber": "latest",
"database-cleaner": "latest",
"forgery": "latest",
"mocha": "latest",
"should": "latest",
"zombie": "1.4.1"
}
}
8 changes: 8 additions & 0 deletions public/stylesheets/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
body {
padding: 50px;
font: 14px "Lucida Grande", Helvetica, Arial, sans-serif;
}

a {
color: #00B7FF;
}
8 changes: 8 additions & 0 deletions spec/factories/index.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{Factory} = require 'forgery'

# Define Factories bellow this line

Factory.define 'Administrator',
name: 'Admin'
username: 'admin'
password: 'password'
8 changes: 8 additions & 0 deletions spec/models/administrator.spec.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{ mongoose } = require '../spec_helper'
Administrator = mongoose.model('Administrator')

describe 'Administrator', ->
describe '#save()', ->
it 'saves without error', (done) ->
admin = new Administrator(username: admin)
admin.save(done)
6 changes: 6 additions & 0 deletions spec/spec_helper.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
require '../tilt'

{ mongoose } = require '../config/database'

module.exports =
mongoose: mongoose
15 changes: 15 additions & 0 deletions tilt.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
express = require 'express'
http = require 'http'

app = express()

app.root = __dirname

require('./config/bootstrap' )(app)
require('./config/application')(app)
require('./config/routes' )(app)

module.exports = app

http.createServer(app).listen app.get('port'), ->
console.log 'Express server listening on port ' + app.get('port')

0 comments on commit 850ec23

Please sign in to comment.