deep_thought 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (91) hide show
  1. checksums.yaml +7 -0
  2. data/Gemfile +3 -0
  3. data/LICENSE.txt +22 -0
  4. data/README.md +184 -0
  5. data/Rakefile +50 -0
  6. data/config.ru +5 -0
  7. data/db/migrate/20130414012805_create_projects.rb +15 -0
  8. data/db/migrate/20130426165010_create_states.rb +14 -0
  9. data/db/migrate/20130501003758_create_users.rb +14 -0
  10. data/db/migrate/20130508001711_add_api_key_to_users.rb +9 -0
  11. data/db/migrate/20130509014935_create_deploys.rb +27 -0
  12. data/db/migrate/20130514062148_create_delayed_jobs.rb +21 -0
  13. data/db/migrate/20130516042149_change_delayed_job_last_error_to_text.rb +9 -0
  14. data/db/migrate/20130517010210_add_ci_to_projects.rb +9 -0
  15. data/db/migrate/20130525001332_add_notification_url_to_users.rb +9 -0
  16. data/db/migrate/20131127022704_remove_deploy_type_and_ci_from_projects.rb +11 -0
  17. data/db/schema.rb +72 -0
  18. data/deep_thought.gemspec +45 -0
  19. data/lib/deep_thought.rb +62 -0
  20. data/lib/deep_thought/api.rb +156 -0
  21. data/lib/deep_thought/app.rb +342 -0
  22. data/lib/deep_thought/ci_service.rb +39 -0
  23. data/lib/deep_thought/ci_service/janky.rb +34 -0
  24. data/lib/deep_thought/deployer.rb +180 -0
  25. data/lib/deep_thought/deployer/deployer.rb +17 -0
  26. data/lib/deep_thought/deployer/shell.rb +45 -0
  27. data/lib/deep_thought/git.rb +76 -0
  28. data/lib/deep_thought/models/deploy.rb +27 -0
  29. data/lib/deep_thought/models/project.rb +32 -0
  30. data/lib/deep_thought/models/state.rb +6 -0
  31. data/lib/deep_thought/models/user.rb +45 -0
  32. data/lib/deep_thought/notifier.rb +13 -0
  33. data/lib/deep_thought/public/assets/javascripts/jquery.min.js +6 -0
  34. data/lib/deep_thought/public/assets/javascripts/main.js +36 -0
  35. data/lib/deep_thought/public/assets/stylesheets/main.css +438 -0
  36. data/lib/deep_thought/public/robots.txt +2 -0
  37. data/lib/deep_thought/scaler.rb +35 -0
  38. data/lib/deep_thought/tasks.rb +65 -0
  39. data/lib/deep_thought/version.rb +3 -0
  40. data/lib/deep_thought/views/history/index.haml +4 -0
  41. data/lib/deep_thought/views/history/show.haml +68 -0
  42. data/lib/deep_thought/views/layouts/layout.haml +32 -0
  43. data/lib/deep_thought/views/projects/edit.haml +15 -0
  44. data/lib/deep_thought/views/projects/index.haml +7 -0
  45. data/lib/deep_thought/views/projects/new.haml +8 -0
  46. data/lib/deep_thought/views/projects/show.haml +25 -0
  47. data/lib/deep_thought/views/sessions/login.haml +8 -0
  48. data/lib/deep_thought/views/users/index.haml +7 -0
  49. data/lib/deep_thought/views/users/new.haml +12 -0
  50. data/lib/deep_thought/views/users/show.haml +38 -0
  51. data/script/bootstrap +3 -0
  52. data/script/console +7 -0
  53. data/script/server +7 -0
  54. data/script/test +3 -0
  55. data/test/deep_thought_api_test.rb +114 -0
  56. data/test/deep_thought_app_test.rb +315 -0
  57. data/test/deep_thought_ci_service_test.rb +44 -0
  58. data/test/deep_thought_deployer_test.rb +72 -0
  59. data/test/deep_thought_git_test.rb +62 -0
  60. data/test/deep_thought_janky_test.rb +42 -0
  61. data/test/deep_thought_notifier_test.rb +35 -0
  62. data/test/deep_thought_project_test.rb +34 -0
  63. data/test/deep_thought_shell_test.rb +38 -0
  64. data/test/fixtures/project-test/HEAD +1 -0
  65. data/test/fixtures/project-test/config +8 -0
  66. data/test/fixtures/project-test/description +1 -0
  67. data/test/fixtures/project-test/hooks/applypatch-msg.sample +15 -0
  68. data/test/fixtures/project-test/hooks/commit-msg.sample +24 -0
  69. data/test/fixtures/project-test/hooks/post-update.sample +8 -0
  70. data/test/fixtures/project-test/hooks/pre-applypatch.sample +14 -0
  71. data/test/fixtures/project-test/hooks/pre-commit.sample +49 -0
  72. data/test/fixtures/project-test/hooks/pre-push.sample +54 -0
  73. data/test/fixtures/project-test/hooks/pre-rebase.sample +169 -0
  74. data/test/fixtures/project-test/hooks/prepare-commit-msg.sample +36 -0
  75. data/test/fixtures/project-test/hooks/update.sample +128 -0
  76. data/test/fixtures/project-test/info/exclude +6 -0
  77. data/test/fixtures/project-test/objects/13/8b8baba400e64fc384d468e80f10cf707e38d7 +1 -0
  78. data/test/fixtures/project-test/objects/6b/8fb16e1675767742afc70ac2ce038da46461f8 +0 -0
  79. data/test/fixtures/project-test/objects/7f/fc496d150b8a0015e3a16be8bacd47daed1a1c +0 -0
  80. data/test/fixtures/project-test/objects/8f/8606174a2aa11545d119fe3c4d6840fe0c4825 +0 -0
  81. data/test/fixtures/project-test/objects/9c/362942288a8ff6dc1addf7c5199bd9837e09de +0 -0
  82. data/test/fixtures/project-test/objects/b5/f7341de6145d3f8b95877cd2675d798fd172f1 +0 -0
  83. data/test/fixtures/project-test/objects/d1/f1e8664b0326316fb18796ac4ae1171e239cb6 +1 -0
  84. data/test/fixtures/project-test/objects/e6/d7ad6d61242936a2cd2bb9ae236adf43e59718 +0 -0
  85. data/test/fixtures/project-test/objects/fa/4cf5ff52462d17b6378a06fae8771c13b654b0 +1 -0
  86. data/test/fixtures/project-test/objects/fa/dda6bc20afa5cf93196bfb08a4989ee3b75ed7 +0 -0
  87. data/test/fixtures/project-test/packed-refs +4 -0
  88. data/test/fixtures/project-test/refs/heads/.gitkeep +0 -0
  89. data/test/fixtures/project-test/refs/tags/.gitkeep +0 -0
  90. data/test/test_helper.rb +42 -0
  91. metadata +462 -0
@@ -0,0 +1,2 @@
1
+ User-agent: *
2
+ Disallow: /
@@ -0,0 +1,35 @@
1
+ require 'httparty'
2
+
3
+ module DeepThought
4
+ module Scaler
5
+ def self.scale
6
+ if (ENV['RACK_ENV'] != 'development' && ENV['RACK_ENV'] != 'test') && (ENV['HEROKU_APP'] && ENV['HEROKU_APP'] != '') && (ENV['HEROKU_API_KEY'] && ENV['HEROKU_API_KEY'] != '')
7
+ if Delayed::Job.count > 0
8
+ scale_up
9
+ else
10
+ scale_down
11
+ end
12
+ end
13
+ end
14
+
15
+ private
16
+
17
+ def self.scale_up
18
+ options = {:body => {:type => 'worker', :qty => '1'}, :basic_auth => {:username => '', :password => ENV['HEROKU_API_KEY']}}
19
+ HTTParty.post("https://api.heroku.com/apps/#{ENV['HEROKU_APP']}/ps/scale", options)
20
+ end
21
+
22
+ def self.scale_down
23
+ options = {:body => {:type => 'worker', :qty => '0'}, :basic_auth => {:username => '', :password => ENV['HEROKU_API_KEY']}}
24
+ HTTParty.post("https://api.heroku.com/apps/#{ENV['HEROKU_APP']}/ps/scale", options)
25
+ end
26
+
27
+ Delayed::Backend::ActiveRecord::Job.class_eval do
28
+ after_destroy :after_destroy
29
+
30
+ def after_destroy
31
+ DeepThought::Scaler.scale
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,65 @@
1
+ require "rake"
2
+ require "rake/tasklib"
3
+ require "active_record"
4
+
5
+ module DeepThought
6
+ module Tasks
7
+ extend Rake::DSL
8
+
9
+ desc "Create a user"
10
+ task :create_user, [:email, :password] do |t, args|
11
+ user = DeepThought::User.create(:email => "#{args[:email]}", :password => "#{args[:password]}", :password_confirmation => "#{args[:password]}")
12
+
13
+ if user.errors.count > 0
14
+ puts "Error when creating new user: #{user.errors.messages}"
15
+ else
16
+ puts "Created new user with email: #{user.email}."
17
+ end
18
+ end
19
+
20
+ namespace :jobs do
21
+ desc "Start a delayed_job worker"
22
+ task :work do
23
+ Delayed::Worker.new(:min_priority => ENV['MIN_PRIORITY'], :max_priority => ENV['MAX_PRIORITY']).start
24
+ end
25
+
26
+ desc "Get number of jobs in the delayed_job queue"
27
+ task :count do
28
+ puts Delayed::Job.count
29
+ end
30
+
31
+ desc "Clear the delayed_job queue"
32
+ task :clear do
33
+ Delayed::Job.delete_all
34
+ end
35
+ end
36
+
37
+ namespace :db do
38
+ desc "Migrate the database"
39
+ task :migrate do
40
+ ActiveRecord::Migration.verbose = true
41
+ ActiveRecord::Migrator.migrate(db("migrate"))
42
+ end
43
+
44
+ desc "Rolls the schema back to the previous version"
45
+ task :rollback do
46
+ ActiveRecord::Migration.verbose = true
47
+ ActiveRecord::Migrator.rollback(db("migrate"), 1)
48
+ end
49
+
50
+ desc 'Reset the database'
51
+ task :reset do
52
+ ActiveRecord::Migration.verbose = true
53
+ ActiveRecord::Migrator.down(db("migrate"))
54
+ ActiveRecord::Migrator.migrate(db("migrate"))
55
+ end
56
+ end
57
+
58
+ private
59
+
60
+ def self.db(action = '')
61
+ @db ||= Pathname(__FILE__).expand_path.join("../../../db")
62
+ @db.join(action).to_s
63
+ end
64
+ end
65
+ end
@@ -0,0 +1,3 @@
1
+ module DeepThought
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,4 @@
1
+ %ul.list
2
+ - deploys.each do |deploy|
3
+ %li
4
+ %a{:class => "#{'alert' if !deploy.was_successful}", :href => "/projects/#{project.name}/deploys/#{deploy.id}"}= "#{deploy.created_at.strftime('%m/%d/%Y | %H:%M:%S')}: deploy #{deploy.branch}#{' to ' + deploy.environment if deploy.environment}#{'/' + deploy.box if deploy.box} by #{deploy.user.email}"
@@ -0,0 +1,68 @@
1
+ - if deploy.in_progress
2
+ .field
3
+ %label.two-column.left in progress:
4
+ %span.two-column.right.info= deploy.in_progress
5
+ - else
6
+ .field
7
+ %label.two-column.left was successful:
8
+ %span.two-column.right.info= deploy.was_successful
9
+
10
+ .field
11
+ %label.two-column.left created at:
12
+ %span.two-column.right.info= deploy.created_at.strftime('%m/%d/%Y %H:%M:%S')
13
+
14
+ .field
15
+ %label.two-column.left by:
16
+ %span.two-column.right.info.long-line= deploy.user.email
17
+
18
+ - if deploy.on_behalf_of
19
+ .field
20
+ %label.two-column.left on behalf of:
21
+ %span.two-column.right.info.long-line= deploy.on_behalf_of
22
+
23
+ .field
24
+ %label.two-column.left via:
25
+ %span.two-column.right.info= deploy.via
26
+
27
+ - if deploy.started_at
28
+ .field
29
+ %label.two-column.left started at:
30
+ %span.two-column.right.info= deploy.started_at.strftime('%m/%d/%Y %H:%M:%S')
31
+
32
+ - if deploy.finished_at
33
+ .field
34
+ %label.two-column.left finished at:
35
+ %span.two-column.right.info= deploy.finished_at.strftime('%m/%d/%Y %H:%M:%S')
36
+
37
+ .field
38
+ %label.two-column.left branch:
39
+ %span.two-column.right.info.long-line= deploy.branch
40
+
41
+ .field
42
+ %label.two-column.left commit:
43
+ %span.two-column.right.info.long-line= deploy.commit
44
+
45
+ - if deploy.environment
46
+ .field
47
+ %label.two-column.left environment:
48
+ %span.two-column.right.info.long-line= deploy.environment
49
+
50
+ - if deploy.box
51
+ .field
52
+ %label.two-column.left box:
53
+ %span.two-column.right.info.long-line= deploy.box
54
+
55
+ - if deploy.actions
56
+ - YAML.load(deploy.actions).each do |action|
57
+ .field
58
+ %label.two-column.left action:
59
+ %span.two-column.right.info.long-line= action
60
+
61
+ - if deploy.variables
62
+ - YAML.load(deploy.variables).each do |variable|
63
+ .field
64
+ %label.two-column.left.long-line= "#{variable[0]}:"
65
+ %span.two-column.right.info.long-line= variable[1]
66
+
67
+ - if deploy.log
68
+ %span.info.log= deploy.log
@@ -0,0 +1,32 @@
1
+ !!! 5
2
+ %html
3
+ %head
4
+ %title Deep Thought
5
+ %meta{:charset => 'utf-8'}
6
+ %meta{:name => 'robots', :content => 'noindex'}
7
+ %link{:href => 'https://fonts.googleapis.com/css?family=Noto+Sans:400,700|Droid+Sans+Mono', :rel => 'stylesheet', :type => 'text/css'}
8
+ %link{:href => '/assets/stylesheets/main.css', :rel => 'stylesheet', :type => 'text/css'}
9
+ %meta{:name => 'viewport', :content => 'user-scalable=0, initial-scale=1.0'}
10
+ %body
11
+ %header
12
+ %nav
13
+ - if current_user
14
+ %ul
15
+ %li
16
+ %a{:href => "/", :class => current_route('projects')} projects
17
+ %li
18
+ %a{:href => "/users", :class => current_route('users')} users
19
+ %li
20
+ %a{:href => "/users/#{@current_user.id}", :class => current_route('me')} me
21
+ %li
22
+ %form{:class => 'logout', :"accept-charset" => "UTF-8", :action => "/logout", :method => "POST"}
23
+ = csrf_tag
24
+ %input{:type => 'hidden', :name => "_method", :value => "delete"}
25
+ %input{:type => "submit", :value => "logout"}
26
+ %div{:class => "deep-thought #{'alert' if is_deploying?}"}
27
+ %h1= settings.deep_thought_message
28
+ .content
29
+ = yield
30
+
31
+ %script{:src => '/assets/javascripts/jquery.min.js', :type => 'text/javascript'}
32
+ %script{:src => '/assets/javascripts/main.js', :type => 'text/javascript'}
@@ -0,0 +1,15 @@
1
+ %form{:"accept-charset" => "UTF-8", :action => "/projects/edit/#{project.name}", :method => "POST"}
2
+ = csrf_tag
3
+ %input{:type => 'hidden', :name => "_method", :value => "put"}
4
+ .field
5
+ %input{:id => "name", :name => "project[name]", :placeholder => "name", :value => project.name, :type => "text"}
6
+ .field
7
+ %input{:id => "repo", :name => "project[repo_url]", :placeholder => "repository url", :value => project.repo_url, :type => "text"}
8
+ .field
9
+ %input{:class => "button", :type => "submit", :value => "update project"}
10
+
11
+ .delete-project
12
+ %form{:"accept-charset" => "UTF-8", :action => "/projects/delete/#{project.name}", :method => "POST"}
13
+ = csrf_tag
14
+ %input{:type => 'hidden', :name => "_method", :value => "delete"}
15
+ %input{:class => "button", :type => "submit", :value => "delete project"}
@@ -0,0 +1,7 @@
1
+ .add-project
2
+ %a.button{:href => '/projects/add/new'} + add project
3
+
4
+ %ul.list
5
+ - projects.each do |project|
6
+ %li
7
+ %a{:href => "/projects/#{project.name}"}= project.name
@@ -0,0 +1,8 @@
1
+ %form{:"accept-charset" => "UTF-8", :action => "/projects/add/new", :method => "POST"}
2
+ = csrf_tag
3
+ .field
4
+ %input{:id => "name", :name => "project[name]", :placeholder => "name", :value => project.name, :type => "text"}
5
+ .field
6
+ %input{:id => "repo", :name => "project[repo_url]", :placeholder => "repository url", :value => project.repo_url, :type => "text"}
7
+ .field
8
+ %input{:class => "button", :type => "submit", :value => "create project"}
@@ -0,0 +1,25 @@
1
+ .deploy
2
+ %form{:"accept-charset" => "UTF-8", :action => "/projects/#{project.name}/deploy", :method => "POST"}
3
+ = csrf_tag
4
+ .field
5
+ %label.two-column{:for => 'branch'} branch:
6
+ %select.two-column{:id => 'branch', :name => "deploy[branch]"}
7
+ - branches.each do |branch|
8
+ %option= branch
9
+ .field
10
+ %input{:id => "environment", :name => "deploy[environment]", :placeholder => "environment <default>", :type => "text"}
11
+ .field
12
+ %input{:id => "box", :name => "deploy[box]", :placeholder => "box <default>", :type => "text"}
13
+ .field
14
+ %a.box#add-action{:href => "#"} + add action
15
+ .field
16
+ %a.box#add-variable{:href => "#"} + add variable
17
+ .field
18
+ - if is_deploying?
19
+ %input{:id => "deploy", :class => "button disabled", :type => "submit", :value => "can't deploy while deploying", :disabled => "disabled"}
20
+ - else
21
+ %input{:id => "deploy", :class => "button", :type => "submit", :value => "deploy"}
22
+
23
+ %a.box{:href => "/projects/edit/#{project.name}"} edit...
24
+
25
+ %a.box{:href => "/projects/#{project.name}/deploys"} history...
@@ -0,0 +1,8 @@
1
+ %form{:"accept-charset" => "UTF-8", :action => "/login", :method => "POST"}
2
+ = csrf_tag
3
+ .field
4
+ %input{:id => "email", :name => "email", :placeholder => "your@email.here", :type => "text"}
5
+ .field
6
+ %input{:id => "password", :name => "password", :placeholder => "password", :type => "password"}
7
+ .field
8
+ %input{:class => "button", :type => "submit", :value => "login"}
@@ -0,0 +1,7 @@
1
+ .add-user
2
+ %a.button{:href => '/users/new'} + add user
3
+
4
+ %ul.list
5
+ - users.each do |user|
6
+ %li
7
+ %a{:href => "/users/#{user.id}"}= user.email
@@ -0,0 +1,12 @@
1
+ %form{:"accept-charset" => "UTF-8", :action => "/users/new", :method => "POST"}
2
+ = csrf_tag
3
+ .field
4
+ %input{:id => "email", :name => "user[email]", :placeholder => "user@email.here", :value => user.email, :type => "text"}
5
+ .field
6
+ %input{:id => "password", :name => "user[password]", :placeholder => "password", :type => "password"}
7
+ .field
8
+ %input{:id => "password_confirmation", :name => "user[password_confirmation]", :placeholder => "confirm password", :type => "password"}
9
+ .field
10
+ %input.long-line{:id => "notification_url", :name => "user[notification_url]", :placeholder => "notification url (if you're a bot)", :type => "text"}
11
+ .field
12
+ %input{:class => "button", :type => "submit", :value => "create user"}
@@ -0,0 +1,38 @@
1
+ - if current_user.id == user.id
2
+ .user-info
3
+ %form{:"accept-charset" => "UTF-8", :action => "/users/#{user.id}", :method => "POST"}
4
+ = csrf_tag
5
+ %input{:type => 'hidden', :name => "_method", :value => "put"}
6
+ .field
7
+ %input{:id => "email", :name => "user[email]", :value => user.email, :type => "text"}
8
+ .field
9
+ %input{:id => "password", :name => "user[password]", :placeholder => "new password", :type => "password"}
10
+ .field
11
+ %input{:id => "password_confirmation", :name => "user[password_confirmation]", :placeholder => "confirm password", :type => "password"}
12
+ .field
13
+ %input.long-line{:id => "notification_url", :name => "user[notification_url]", :value => user.notification_url, :placeholder => "notification url (if you're a bot)", :type => "text"}
14
+ .field
15
+ %input{:class => "button", :type => "submit", :value => "update"}
16
+
17
+ .user-api-key
18
+ - if current_user.api_key
19
+ %span.info.long-line= current_user.api_key
20
+ - else
21
+ %span.info no api key (yet)
22
+
23
+ %form{:"accept-charset" => "UTF-8", :action => "/users/#{user.id}/key", :method => "POST"}
24
+ = csrf_tag
25
+ %input{:class => "button", :type => "submit", :value => "generate new api key"}
26
+ - else
27
+ %ul.list
28
+ %li
29
+ %p= user.email
30
+ - if user.notification_url
31
+ %li
32
+ %p= user.notification_url
33
+
34
+ .delete-user
35
+ %form{:"accept-charset" => "UTF-8", :action => "/users/#{user.id}", :method => "POST"}
36
+ = csrf_tag
37
+ %input{:type => 'hidden', :name => "_method", :value => "delete"}
38
+ %input{:class => "button", :type => "submit", :value => "delete user"}
@@ -0,0 +1,3 @@
1
+ #!/bin/sh
2
+
3
+ bundle install --binstubs
@@ -0,0 +1,7 @@
1
+ #!/bin/sh
2
+
3
+ while read line; do
4
+ export $line
5
+ done < .env
6
+
7
+ bin/racksh
@@ -0,0 +1,7 @@
1
+ #!/bin/sh
2
+
3
+ while read line; do
4
+ export $line
5
+ done < .env
6
+
7
+ bin/shotgun -p4242 -sthin -Ilib config.ru & rake jobs:work
@@ -0,0 +1,3 @@
1
+ #!/bin/sh
2
+
3
+ testrb ./test/*_test.rb
@@ -0,0 +1,114 @@
1
+ require File.expand_path '../test_helper.rb', __FILE__
2
+
3
+ class DeepThoughtApiTest < MiniTest::Unit::TestCase
4
+ include Rack::Test::Methods
5
+
6
+ def setup
7
+ DatabaseCleaner.start
8
+
9
+ if File.directory?(".projects/_test")
10
+ FileUtils.rm_rf(".projects/_test")
11
+ end
12
+
13
+ deployer = mock('class')
14
+ deployer.stubs(:new).returns(deployer)
15
+ deployer.stubs(:setup)
16
+ DeepThought::Deployer.register_adapter('mock', deployer)
17
+
18
+ @user = DeepThought::User.create(:email => 'test@test.com', :password => 'secret', :password_confirmation => 'secret', :api_key => '12345')
19
+ end
20
+
21
+ def teardown
22
+ if File.directory?(".projects/_test")
23
+ FileUtils.rm_rf(".projects/_test")
24
+ end
25
+
26
+ DatabaseCleaner.clean
27
+ end
28
+
29
+ def app
30
+ DeepThought.app
31
+ end
32
+
33
+ def test_api_unauthorized
34
+ header 'Accept', 'application/json'
35
+ get '/deploy/'
36
+ assert !last_response.ok?
37
+ assert_equal 401, last_response.status
38
+ end
39
+
40
+ def test_api_get
41
+ header 'Accept', 'application/json'
42
+ get '/deploy/', {}, {"HTTP_AUTHORIZATION" => 'Token token="12345"'}
43
+ assert !last_response.ok?
44
+ assert_equal "I don't got what you're trying to GET.", last_response.body
45
+ end
46
+
47
+ def test_api_post_empty
48
+ header 'Accept', 'application/json'
49
+ post '/deploy/', {}, {"HTTP_AUTHORIZATION" => 'Token token="12345"'}
50
+ assert !last_response.ok?
51
+ assert_equal "Must supply app name.", last_response.body
52
+ end
53
+
54
+ def test_api_setup_success
55
+ header 'Accept', 'application/json'
56
+ post '/deploy/setup/_test', {:repo_url => './test/fixtures/project-test'}.to_json, {"HTTP_AUTHORIZATION" => 'Token token="12345"'}
57
+ assert last_response.ok?
58
+ end
59
+
60
+ def test_api_setup_failed
61
+ header 'Accept', 'application/json'
62
+ post '/deploy/setup/_test', {}, {"HTTP_AUTHORIZATION" => 'Token token="12345"'}
63
+ assert !last_response.ok?
64
+ assert_equal "Sorry, but I need a project name and repo url. No exceptions, despite how nicely you ask.", last_response.body
65
+ end
66
+
67
+ def test_api_non_project
68
+ header 'Accept', 'application/json'
69
+ post '/deploy/_test', {}, {"HTTP_AUTHORIZATION" => 'Token token="12345"'}
70
+ assert !last_response.ok?
71
+ assert_equal "Hmm, that project doesn't appear to exist. Have you set it up?", last_response.body
72
+ end
73
+
74
+ def test_api_no_repo
75
+ project = DeepThought::Project.create(:name => '_test', :repo_url => 'http://fake.url')
76
+ header 'Accept', 'application/json'
77
+ post '/deploy/_test', {}, {"HTTP_AUTHORIZATION" => 'Token token="12345"'}
78
+ assert !last_response.ok?
79
+ assert_equal "I can't seem to access that repo. Are you sure the URL is correct and that I have access to it?", last_response.body
80
+ end
81
+
82
+ def test_api_no_branch
83
+ project = DeepThought::Project.create(:name => '_test', :repo_url => './test/fixtures/project-test')
84
+ branch = 'no-branch'
85
+ header 'Accept', 'application/json'
86
+ post '/deploy/_test', {:branch => branch}.to_json, {"HTTP_AUTHORIZATION" => 'Token token="12345"'}
87
+ assert !last_response.ok?
88
+ assert_equal "#{project.name} doesn't appear to have a branch called #{branch}. Have you pushed it?", last_response.body
89
+ end
90
+
91
+ def test_api_in_deployment
92
+ DeepThought::Deployer.lock_deployer
93
+ project = DeepThought::Project.create(:name => '_test', :repo_url => './test/fixtures/project-test')
94
+ header 'Accept', 'application/json'
95
+ post '/deploy/_test', {}, {"HTTP_AUTHORIZATION" => 'Token token="12345"'}
96
+ assert !last_response.ok?
97
+ assert_equal "Sorry, but I'm currently in mid-deployment. Ask me again when I'm done.", last_response.body
98
+ end
99
+
100
+ def test_api_status_ready
101
+ header 'Accept', 'application/json'
102
+ get '/deploy/status', {}, {"HTTP_AUTHORIZATION" => 'Token token="12345"'}
103
+ assert last_response.ok?
104
+ assert_equal "I'm ready to ponder the infinitely complex questions of the universe.", last_response.body
105
+ end
106
+
107
+ def test_api_status_busy
108
+ DeepThought::Deployer.lock_deployer
109
+ header 'Accept', 'application/json'
110
+ get '/deploy/status', {}, {"HTTP_AUTHORIZATION" => 'Token token="12345"'}
111
+ assert !last_response.ok?
112
+ assert_equal "I'm currently in mid-deployment.", last_response.body
113
+ end
114
+ end