From 27338212e6347bebed1cbf08963a9af110368b76 Mon Sep 17 00:00:00 2001 From: Kevin Thompson Date: Sun, 16 Dec 2012 23:47:00 -0800 Subject: Establish test suite. --- test/cli_test.rb | 20 ++++++++++++++++++++ test/test_helper.rb | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+) create mode 100644 test/cli_test.rb create mode 100644 test/test_helper.rb (limited to 'test') diff --git a/test/cli_test.rb b/test/cli_test.rb new file mode 100644 index 0000000..4dc1eb3 --- /dev/null +++ b/test/cli_test.rb @@ -0,0 +1,20 @@ +require 'test_helper' +silent{ load 'bin/dashing' } + +module Thor::Actions + def source_paths + [File.join(File.expand_path(File.dirname(__FILE__)), '../templates')] + end +end + +class CliTest < Dashing::Test + + def test_project_directory_created + temp do |dir| + cli = Dashing::CLI.new + silent{ cli.new 'Dashboard' } + assert Dir.exist?(File.join(dir,'dashboard')), 'Dashing directory was not created.' + end + end + +end \ No newline at end of file diff --git a/test/test_helper.rb b/test/test_helper.rb new file mode 100644 index 0000000..d2337c5 --- /dev/null +++ b/test/test_helper.rb @@ -0,0 +1,34 @@ +require 'rack/test' +require 'stringio' +require 'test/unit' +require 'tmpdir' + +ENV['RACK_ENV'] = 'test' +WORKING_DIRECTORY = Dir.pwd.freeze +ARGV.clear + +def silent + _stdout = $stdout + $stdout = mock = StringIO.new + begin + yield + ensure + $stdout = _stdout + end +end + +def temp + path = File.expand_path "#{Dir.tmpdir}/#{Time.now.to_i}#{rand(1000)}/" + FileUtils.mkdir_p path + Dir.chdir path + yield path +ensure + Dir.chdir WORKING_DIRECTORY + FileUtils.rm_rf(path) if File.exists?(path) +end + +module Dashing + class Test < Test::Unit::TestCase + include Rack::Test::Methods + end +end \ No newline at end of file -- cgit v1.2.3 From 31bdc6da2706eec95c54ca46cb8e544661296bf6 Mon Sep 17 00:00:00 2001 From: Federico Bana Date: Mon, 25 Mar 2013 16:58:19 -0300 Subject: Properly hyphenate css class names for widgets. --- test/cli_test.rb | 8 ++++++++ 1 file changed, 8 insertions(+) (limited to 'test') diff --git a/test/cli_test.rb b/test/cli_test.rb index 4dc1eb3..6c43e2c 100644 --- a/test/cli_test.rb +++ b/test/cli_test.rb @@ -17,4 +17,12 @@ class CliTest < Dashing::Test end end + def test_hyphenate + assert_equal 'power', Dashing::CLI.hyphenate('Power') + assert_equal 'power', Dashing::CLI.hyphenate('POWER') + assert_equal 'power-rangers', Dashing::CLI.hyphenate('PowerRangers') + assert_equal 'power-ranger', Dashing::CLI.hyphenate('Power_ranger') + assert_equal 'super-power-rangers', Dashing::CLI.hyphenate('SuperPowerRangers') + end + end \ No newline at end of file -- cgit v1.2.3 From 1a974c754789b0b675d39442cead91213f706851 Mon Sep 17 00:00:00 2001 From: Aaron Peckham Date: Tue, 7 May 2013 15:09:54 -0700 Subject: Test that the Sinatra app redirects to the default dashboard; saves data posted to /widgets/X, and requires auth; returns data on /events; returns the sample dashboards; and returns sample widgets. --- test/app_test.rb | 84 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 84 insertions(+) create mode 100644 test/app_test.rb (limited to 'test') diff --git a/test/app_test.rb b/test/app_test.rb new file mode 100644 index 0000000..445c538 --- /dev/null +++ b/test/app_test.rb @@ -0,0 +1,84 @@ +require 'test_helper' +require File.expand_path('../../lib/dashing', __FILE__) + +class AppTest < Dashing::Test + def setup + @connection = [] + Sinatra::Application.settings.connections = [@connection] + Sinatra::Application.settings.auth_token = nil + end + + def test_redirect_to_default_dashboard + Sinatra::Application.settings.default_dashboard = 'test1' + get '/' + assert_equal 302, last_response.status + assert_equal 'http://example.org/test1', last_response.location + end + + def test_post_widgets_without_auth_token + post '/widgets/some_widget', JSON.generate({value: 6}) + assert_equal 204, last_response.status + + assert_equal 1, @connection.length + data = parse_data @connection[0] + assert_equal 6, data['value'] + assert_equal 'some_widget', data['id'] + assert data['updatedAt'] + end + + def test_post_widgets_with_invalid_auth_token + Sinatra::Application.settings.auth_token = 'sekrit' + post '/widgets/some_widget', JSON.generate({value: 9}) + assert_equal 401, last_response.status + end + + def test_post_widgets_with_valid_auth_token + Sinatra::Application.settings.auth_token = 'sekrit' + post '/widgets/some_widget', JSON.generate({value: 9, auth_token: 'sekrit'}) + assert_equal 204, last_response.status + end + + def test_get_events + post '/widgets/some_widget', JSON.generate({value: 8}) + assert_equal 204, last_response.status + + get '/events' + assert_equal 200, last_response.status + assert_equal 8, parse_data(@connection[0])['value'] + end + + def test_get_dashboard + with_generated_project do + get '/sampletv' + assert_equal 200, last_response.status + assert_include last_response.body, 'class="gridster"' + end + end + + def test_get_widget + with_generated_project do + get '/views/meter.html' + assert_equal 200, last_response.status + assert_include last_response.body, 'class="meter"' + end + end + + def with_generated_project + temp do |dir| + cli = Dashing::CLI.new + silent { cli.new 'new_project' } + + Sinatra::Application.settings.views = File.join(dir, 'new_project/dashboards') + Sinatra::Application.settings.root = File.join(dir, 'new_project') + yield + end + end + + def app + Sinatra::Application + end + + def parse_data(string) + JSON.parse string[/data: (.+)/, 1] + end +end \ No newline at end of file -- cgit v1.2.3 From 50c8638f95584dbd863de6da042755ea400ee40b Mon Sep 17 00:00:00 2001 From: Aaron Peckham Date: Tue, 7 May 2013 21:27:46 -0700 Subject: add test for redirecting to first dashboard, and returning a 404 if dashboard doesn't exist --- test/app_test.rb | 32 +++++++++++++++++++++++++------- 1 file changed, 25 insertions(+), 7 deletions(-) (limited to 'test') diff --git a/test/app_test.rb b/test/app_test.rb index 445c538..f6fa537 100644 --- a/test/app_test.rb +++ b/test/app_test.rb @@ -6,13 +6,7 @@ class AppTest < Dashing::Test @connection = [] Sinatra::Application.settings.connections = [@connection] Sinatra::Application.settings.auth_token = nil - end - - def test_redirect_to_default_dashboard - Sinatra::Application.settings.default_dashboard = 'test1' - get '/' - assert_equal 302, last_response.status - assert_equal 'http://example.org/test1', last_response.location + Sinatra::Application.settings.default_dashboard = nil end def test_post_widgets_without_auth_token @@ -46,6 +40,23 @@ class AppTest < Dashing::Test assert_equal 200, last_response.status assert_equal 8, parse_data(@connection[0])['value'] end + + def test_redirect_to_default_dashboard + with_generated_project do + Sinatra::Application.settings.default_dashboard = 'test1' + get '/' + assert_equal 302, last_response.status + assert_equal 'http://example.org/test1', last_response.location + end + end + + def test_redirect_to_first_dashboard + with_generated_project do + get '/' + assert_equal 302, last_response.status + assert_equal 'http://example.org/sample', last_response.location + end + end def test_get_dashboard with_generated_project do @@ -54,6 +65,13 @@ class AppTest < Dashing::Test assert_include last_response.body, 'class="gridster"' end end + + def test_get_nonexistent_dashboard + with_generated_project do + get '/nodashboard' + assert_equal 404, last_response.status + end + end def test_get_widget with_generated_project do -- cgit v1.2.3 From 8795505a06fe58dfb5f414078587349b7771af62 Mon Sep 17 00:00:00 2001 From: Aaron Peckham Date: Thu, 9 May 2013 23:27:44 -0700 Subject: use any Tilt-supported view engine for dashboards, including Haml --- test/app_test.rb | 32 ++++++++++++++++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-) (limited to 'test') diff --git a/test/app_test.rb b/test/app_test.rb index f6fa537..83bdb74 100644 --- a/test/app_test.rb +++ b/test/app_test.rb @@ -58,6 +58,15 @@ class AppTest < Dashing::Test end end + def test_redirect_to_first_dashboard_without_erb + with_generated_project do |dir| + FileUtils.touch(File.join(dir, "dashboards/htmltest.html")) + get '/' + assert_equal 302, last_response.status + assert_equal 'http://example.org/htmltest', last_response.location + end + end + def test_get_dashboard with_generated_project do get '/sampletv' @@ -65,7 +74,26 @@ class AppTest < Dashing::Test assert_include last_response.body, 'class="gridster"' end end - + + begin + require 'haml' + def test_get_haml + with_generated_project do |dir| + File.write(File.join(dir, "dashboards/hamltest.haml"), <<-HAML) +.gridster + %ul + %li{data: {col: 1, row: 1, sizex: 1, sizey: 1}} + %div{data: {view: "Clock"}} + %i.icon-time.icon-background +HAML + get '/hamltest' + assert_equal 200, last_response.status + assert_include last_response.body, "class='gridster'" + end + end + rescue LoadError + end + def test_get_nonexistent_dashboard with_generated_project do get '/nodashboard' @@ -88,7 +116,7 @@ class AppTest < Dashing::Test Sinatra::Application.settings.views = File.join(dir, 'new_project/dashboards') Sinatra::Application.settings.root = File.join(dir, 'new_project') - yield + yield Sinatra::Application.settings.root end end -- cgit v1.2.3 From a6eea61f2a7cfbf67175583dd7dcb49a4c09da75 Mon Sep 17 00:00:00 2001 From: Aaron Peckham Date: Mon, 20 May 2013 12:19:35 -0700 Subject: allow widget views to be written in any Tilt-supported template language --- test/app_test.rb | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) (limited to 'test') diff --git a/test/app_test.rb b/test/app_test.rb index 83bdb74..97aad3a 100644 --- a/test/app_test.rb +++ b/test/app_test.rb @@ -72,26 +72,33 @@ class AppTest < Dashing::Test get '/sampletv' assert_equal 200, last_response.status assert_include last_response.body, 'class="gridster"' + assert_include last_response.body, "DOCTYPE" end end begin require 'haml' - def test_get_haml + + def test_get_haml_dashboard with_generated_project do |dir| - File.write(File.join(dir, "dashboards/hamltest.haml"), <<-HAML) -.gridster - %ul - %li{data: {col: 1, row: 1, sizex: 1, sizey: 1}} - %div{data: {view: "Clock"}} - %i.icon-time.icon-background -HAML + File.write(File.join(dir, 'dashboards/hamltest.haml'), '.gridster') get '/hamltest' assert_equal 200, last_response.status assert_include last_response.body, "class='gridster'" end end + + def test_get_haml_widget + with_generated_project do |dir| + File.write(File.join(dir, 'widgets/clock/clock.haml'), '%h1 haml') + File.unlink(File.join(dir, 'widgets/clock/clock.html')) + get '/views/clock.html' + assert_equal 200, last_response.status + assert_include last_response.body, '

haml

' + end + end rescue LoadError + puts "[skipping haml tests because haml isn't installed]" end def test_get_nonexistent_dashboard -- cgit v1.2.3 From 32edb874f2fa2aefe363ec9d92d5f529cec489d8 Mon Sep 17 00:00:00 2001 From: David Underwood Date: Tue, 25 Jun 2013 11:40:35 -0400 Subject: render titles correctly --- test/app_test.rb | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) (limited to 'test') diff --git a/test/app_test.rb b/test/app_test.rb index 97aad3a..8b1be19 100644 --- a/test/app_test.rb +++ b/test/app_test.rb @@ -40,7 +40,7 @@ class AppTest < Dashing::Test assert_equal 200, last_response.status assert_equal 8, parse_data(@connection[0])['value'] end - + def test_redirect_to_default_dashboard with_generated_project do Sinatra::Application.settings.default_dashboard = 'test1' @@ -76,6 +76,13 @@ class AppTest < Dashing::Test end end + def test_page_title_set_correctly + with_generated_project do + get '/sampletv' + assert_include last_response.body, '1080p dashboard' + end + end + begin require 'haml' -- cgit v1.2.3 From 5f8cbcb7debde027e79d87733b84cb852aa47dad Mon Sep 17 00:00:00 2001 From: Chad Jolly Date: Sun, 14 Jul 2013 18:36:19 -0600 Subject: dashboard events - use SSE event names --- test/app_test.rb | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) (limited to 'test') diff --git a/test/app_test.rb b/test/app_test.rb index 8b1be19..69e57c9 100644 --- a/test/app_test.rb +++ b/test/app_test.rb @@ -41,6 +41,16 @@ class AppTest < Dashing::Test assert_equal 8, parse_data(@connection[0])['value'] end + def test_dashboard_events + post '/dashboards/my_super_sweet_dashboard', JSON.generate({event: 'reload'}) + assert_equal 204, last_response.status + + get '/events' + assert_equal 200, last_response.status + assert_equal 'dashboards', parse_event(@connection[0]) + assert_equal 'reload', parse_data(@connection[0])['event'] + end + def test_redirect_to_default_dashboard with_generated_project do Sinatra::Application.settings.default_dashboard = 'test1' @@ -141,4 +151,8 @@ class AppTest < Dashing::Test def parse_data(string) JSON.parse string[/data: (.+)/, 1] end -end \ No newline at end of file + + def parse_event(string) + string[/event: (.+)/, 1] + end +end -- cgit v1.2.3 From 630269f1e695987002317c34cb4d6c703d5c1c40 Mon Sep 17 00:00:00 2001 From: Chad Jolly Date: Sun, 14 Jul 2013 22:03:11 -0600 Subject: use tmp dir for history file when running tests --- test/app_test.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'test') diff --git a/test/app_test.rb b/test/app_test.rb index 8b1be19..0546814 100644 --- a/test/app_test.rb +++ b/test/app_test.rb @@ -1,5 +1,6 @@ require 'test_helper' require File.expand_path('../../lib/dashing', __FILE__) +Sinatra::Application.settings.history_file = File.join(Dir.tmpdir, 'history.yml') class AppTest < Dashing::Test def setup @@ -141,4 +142,4 @@ class AppTest < Dashing::Test def parse_data(string) JSON.parse string[/data: (.+)/, 1] end -end \ No newline at end of file +end -- cgit v1.2.3 From 69ed82efa8319e7fbde9df95a4fad4ee96aa5074 Mon Sep 17 00:00:00 2001 From: pseudomuto Date: Wed, 18 Dec 2013 15:49:32 -0500 Subject: adding haml to dev dependencies so all tests run --- test/app_test.rb | 37 ++++++++++++++++--------------------- test/test_helper.rb | 3 ++- 2 files changed, 18 insertions(+), 22 deletions(-) (limited to 'test') diff --git a/test/app_test.rb b/test/app_test.rb index 0032165..0547eb8 100644 --- a/test/app_test.rb +++ b/test/app_test.rb @@ -1,5 +1,6 @@ require 'test_helper' -require File.expand_path('../../lib/dashing', __FILE__) +require 'haml' + Sinatra::Application.settings.history_file = File.join(Dir.tmpdir, 'history.yml') class AppTest < Dashing::Test @@ -94,29 +95,23 @@ class AppTest < Dashing::Test end end - begin - require 'haml' - - def test_get_haml_dashboard - with_generated_project do |dir| - File.write(File.join(dir, 'dashboards/hamltest.haml'), '.gridster') - get '/hamltest' - assert_equal 200, last_response.status - assert_include last_response.body, "class='gridster'" - end + def test_get_haml_dashboard + with_generated_project do |dir| + File.write(File.join(dir, 'dashboards/hamltest.haml'), '.gridster') + get '/hamltest' + assert_equal 200, last_response.status + assert_include last_response.body, "class='gridster'" end + end - def test_get_haml_widget - with_generated_project do |dir| - File.write(File.join(dir, 'widgets/clock/clock.haml'), '%h1 haml') - File.unlink(File.join(dir, 'widgets/clock/clock.html')) - get '/views/clock.html' - assert_equal 200, last_response.status - assert_include last_response.body, '

haml

' - end + def test_get_haml_widget + with_generated_project do |dir| + File.write(File.join(dir, 'widgets/clock/clock.haml'), '%h1 haml') + File.unlink(File.join(dir, 'widgets/clock/clock.html')) + get '/views/clock.html' + assert_equal 200, last_response.status + assert_include last_response.body, '

haml

' end - rescue LoadError - puts "[skipping haml tests because haml isn't installed]" end def test_get_nonexistent_dashboard diff --git a/test/test_helper.rb b/test/test_helper.rb index d2337c5..b4bbd9e 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -2,6 +2,7 @@ require 'rack/test' require 'stringio' require 'test/unit' require 'tmpdir' +require_relative '../lib/dashing' ENV['RACK_ENV'] = 'test' WORKING_DIRECTORY = Dir.pwd.freeze @@ -31,4 +32,4 @@ module Dashing class Test < Test::Unit::TestCase include Rack::Test::Methods end -end \ No newline at end of file +end -- cgit v1.2.3 From c3a72795ecab47f94527e079b60549051843caa4 Mon Sep 17 00:00:00 2001 From: pseudomuto Date: Wed, 18 Dec 2013 15:55:46 -0500 Subject: switching from test unit to minitest --- test/app_test.rb | 15 +++++++-------- test/cli_test.rb | 6 +++--- test/test_helper.rb | 23 ++++++++++++++--------- 3 files changed, 24 insertions(+), 20 deletions(-) (limited to 'test') diff --git a/test/app_test.rb b/test/app_test.rb index 0547eb8..a176d2e 100644 --- a/test/app_test.rb +++ b/test/app_test.rb @@ -1,11 +1,10 @@ require 'test_helper' require 'haml' -Sinatra::Application.settings.history_file = File.join(Dir.tmpdir, 'history.yml') - class AppTest < Dashing::Test def setup @connection = [] + Sinatra::Application.settings.history_file = File.join(Dir.tmpdir, 'history.yml') Sinatra::Application.settings.connections = [@connection] Sinatra::Application.settings.auth_token = nil Sinatra::Application.settings.default_dashboard = nil @@ -83,15 +82,15 @@ class AppTest < Dashing::Test with_generated_project do get '/sampletv' assert_equal 200, last_response.status - assert_include last_response.body, 'class="gridster"' - assert_include last_response.body, "DOCTYPE" + assert_includes last_response.body, 'class="gridster"' + assert_includes last_response.body, "DOCTYPE" end end def test_page_title_set_correctly with_generated_project do get '/sampletv' - assert_include last_response.body, '1080p dashboard' + assert_includes last_response.body, '1080p dashboard' end end @@ -100,7 +99,7 @@ class AppTest < Dashing::Test File.write(File.join(dir, 'dashboards/hamltest.haml'), '.gridster') get '/hamltest' assert_equal 200, last_response.status - assert_include last_response.body, "class='gridster'" + assert_includes last_response.body, "class='gridster'" end end @@ -110,7 +109,7 @@ class AppTest < Dashing::Test File.unlink(File.join(dir, 'widgets/clock/clock.html')) get '/views/clock.html' assert_equal 200, last_response.status - assert_include last_response.body, '

haml

' + assert_includes last_response.body, '

haml

' end end @@ -125,7 +124,7 @@ class AppTest < Dashing::Test with_generated_project do get '/views/meter.html' assert_equal 200, last_response.status - assert_include last_response.body, 'class="meter"' + assert_includes last_response.body, 'class="meter"' end end diff --git a/test/cli_test.rb b/test/cli_test.rb index 6c43e2c..2bf3b65 100644 --- a/test/cli_test.rb +++ b/test/cli_test.rb @@ -1,5 +1,5 @@ require 'test_helper' -silent{ load 'bin/dashing' } +load_quietly 'bin/dashing' module Thor::Actions def source_paths @@ -12,7 +12,7 @@ class CliTest < Dashing::Test def test_project_directory_created temp do |dir| cli = Dashing::CLI.new - silent{ cli.new 'Dashboard' } + silent { cli.new 'Dashboard' } assert Dir.exist?(File.join(dir,'dashboard')), 'Dashing directory was not created.' end end @@ -25,4 +25,4 @@ class CliTest < Dashing::Test assert_equal 'super-power-rangers', Dashing::CLI.hyphenate('SuperPowerRangers') end -end \ No newline at end of file +end diff --git a/test/test_helper.rb b/test/test_helper.rb index b4bbd9e..9c51b87 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -1,20 +1,19 @@ require 'rack/test' require 'stringio' -require 'test/unit' require 'tmpdir' + +require 'minitest/autorun' +require 'minitest/pride' + require_relative '../lib/dashing' ENV['RACK_ENV'] = 'test' WORKING_DIRECTORY = Dir.pwd.freeze ARGV.clear -def silent - _stdout = $stdout - $stdout = mock = StringIO.new - begin - yield - ensure - $stdout = _stdout +def load_quietly(file) + Minitest::Test.new(nil).capture_io do + load file end end @@ -29,7 +28,13 @@ ensure end module Dashing - class Test < Test::Unit::TestCase + class Test < Minitest::Test include Rack::Test::Methods + + alias_method :silent, :capture_io + + def teardown + FileUtils.rm_f('history.yml') + end end end -- cgit v1.2.3 From d0eef2dbe9d1178111cd768116a66b32a3a4b2b5 Mon Sep 17 00:00:00 2001 From: pseudomuto Date: Wed, 18 Dec 2013 19:12:43 -0500 Subject: moving cli to lib and updating bin file --- test/app_test.rb | 306 ++++++++++++++++++++++++------------------------ test/cli_test.rb | 174 ++++++++++++++++++++++++--- test/downloader_test.rb | 26 ++++ test/test_helper.rb | 5 +- 4 files changed, 340 insertions(+), 171 deletions(-) create mode 100644 test/downloader_test.rb (limited to 'test') diff --git a/test/app_test.rb b/test/app_test.rb index a176d2e..bf3bbb1 100644 --- a/test/app_test.rb +++ b/test/app_test.rb @@ -1,153 +1,153 @@ -require 'test_helper' -require 'haml' - -class AppTest < Dashing::Test - def setup - @connection = [] - Sinatra::Application.settings.history_file = File.join(Dir.tmpdir, 'history.yml') - Sinatra::Application.settings.connections = [@connection] - Sinatra::Application.settings.auth_token = nil - Sinatra::Application.settings.default_dashboard = nil - end - - def test_post_widgets_without_auth_token - post '/widgets/some_widget', JSON.generate({value: 6}) - assert_equal 204, last_response.status - - assert_equal 1, @connection.length - data = parse_data @connection[0] - assert_equal 6, data['value'] - assert_equal 'some_widget', data['id'] - assert data['updatedAt'] - end - - def test_post_widgets_with_invalid_auth_token - Sinatra::Application.settings.auth_token = 'sekrit' - post '/widgets/some_widget', JSON.generate({value: 9}) - assert_equal 401, last_response.status - end - - def test_post_widgets_with_valid_auth_token - Sinatra::Application.settings.auth_token = 'sekrit' - post '/widgets/some_widget', JSON.generate({value: 9, auth_token: 'sekrit'}) - assert_equal 204, last_response.status - end - - def test_get_events - post '/widgets/some_widget', JSON.generate({value: 8}) - assert_equal 204, last_response.status - - get '/events' - assert_equal 200, last_response.status - assert_equal 8, parse_data(@connection[0])['value'] - end - - def test_dashboard_events - post '/dashboards/my_super_sweet_dashboard', JSON.generate({event: 'reload'}) - assert_equal 204, last_response.status - - get '/events' - assert_equal 200, last_response.status - assert_equal 'dashboards', parse_event(@connection[0]) - assert_equal 'reload', parse_data(@connection[0])['event'] - end - - def test_redirect_to_default_dashboard - with_generated_project do - Sinatra::Application.settings.default_dashboard = 'test1' - get '/' - assert_equal 302, last_response.status - assert_equal 'http://example.org/test1', last_response.location - end - end - - def test_redirect_to_first_dashboard - with_generated_project do - get '/' - assert_equal 302, last_response.status - assert_equal 'http://example.org/sample', last_response.location - end - end - - def test_redirect_to_first_dashboard_without_erb - with_generated_project do |dir| - FileUtils.touch(File.join(dir, "dashboards/htmltest.html")) - get '/' - assert_equal 302, last_response.status - assert_equal 'http://example.org/htmltest', last_response.location - end - end - - def test_get_dashboard - with_generated_project do - get '/sampletv' - assert_equal 200, last_response.status - assert_includes last_response.body, 'class="gridster"' - assert_includes last_response.body, "DOCTYPE" - end - end - - def test_page_title_set_correctly - with_generated_project do - get '/sampletv' - assert_includes last_response.body, '1080p dashboard' - end - end - - def test_get_haml_dashboard - with_generated_project do |dir| - File.write(File.join(dir, 'dashboards/hamltest.haml'), '.gridster') - get '/hamltest' - assert_equal 200, last_response.status - assert_includes last_response.body, "class='gridster'" - end - end - - def test_get_haml_widget - with_generated_project do |dir| - File.write(File.join(dir, 'widgets/clock/clock.haml'), '%h1 haml') - File.unlink(File.join(dir, 'widgets/clock/clock.html')) - get '/views/clock.html' - assert_equal 200, last_response.status - assert_includes last_response.body, '

haml

' - end - end - - def test_get_nonexistent_dashboard - with_generated_project do - get '/nodashboard' - assert_equal 404, last_response.status - end - end - - def test_get_widget - with_generated_project do - get '/views/meter.html' - assert_equal 200, last_response.status - assert_includes last_response.body, 'class="meter"' - end - end - - def with_generated_project - temp do |dir| - cli = Dashing::CLI.new - silent { cli.new 'new_project' } - - Sinatra::Application.settings.views = File.join(dir, 'new_project/dashboards') - Sinatra::Application.settings.root = File.join(dir, 'new_project') - yield Sinatra::Application.settings.root - end - end - - def app - Sinatra::Application - end - - def parse_data(string) - JSON.parse string[/data: (.+)/, 1] - end - - def parse_event(string) - string[/event: (.+)/, 1] - end -end +# require 'test_helper' +# require 'haml' + +# class AppTest < Dashing::Test +# def setup +# @connection = [] +# Sinatra::Application.settings.history_file = File.join(Dir.tmpdir, 'history.yml') +# Sinatra::Application.settings.connections = [@connection] +# Sinatra::Application.settings.auth_token = nil +# Sinatra::Application.settings.default_dashboard = nil +# end + +# def test_post_widgets_without_auth_token +# post '/widgets/some_widget', JSON.generate({value: 6}) +# assert_equal 204, last_response.status + +# assert_equal 1, @connection.length +# data = parse_data @connection[0] +# assert_equal 6, data['value'] +# assert_equal 'some_widget', data['id'] +# assert data['updatedAt'] +# end + +# def test_post_widgets_with_invalid_auth_token +# Sinatra::Application.settings.auth_token = 'sekrit' +# post '/widgets/some_widget', JSON.generate({value: 9}) +# assert_equal 401, last_response.status +# end + +# def test_post_widgets_with_valid_auth_token +# Sinatra::Application.settings.auth_token = 'sekrit' +# post '/widgets/some_widget', JSON.generate({value: 9, auth_token: 'sekrit'}) +# assert_equal 204, last_response.status +# end + +# def test_get_events +# post '/widgets/some_widget', JSON.generate({value: 8}) +# assert_equal 204, last_response.status + +# get '/events' +# assert_equal 200, last_response.status +# assert_equal 8, parse_data(@connection[0])['value'] +# end + +# def test_dashboard_events +# post '/dashboards/my_super_sweet_dashboard', JSON.generate({event: 'reload'}) +# assert_equal 204, last_response.status + +# get '/events' +# assert_equal 200, last_response.status +# assert_equal 'dashboards', parse_event(@connection[0]) +# assert_equal 'reload', parse_data(@connection[0])['event'] +# end + +# def test_redirect_to_default_dashboard +# with_generated_project do +# Sinatra::Application.settings.default_dashboard = 'test1' +# get '/' +# assert_equal 302, last_response.status +# assert_equal 'http://example.org/test1', last_response.location +# end +# end + +# def test_redirect_to_first_dashboard +# with_generated_project do +# get '/' +# assert_equal 302, last_response.status +# assert_equal 'http://example.org/sample', last_response.location +# end +# end + +# def test_redirect_to_first_dashboard_without_erb +# with_generated_project do |dir| +# FileUtils.touch(File.join(dir, "dashboards/htmltest.html")) +# get '/' +# assert_equal 302, last_response.status +# assert_equal 'http://example.org/htmltest', last_response.location +# end +# end + +# def test_get_dashboard +# with_generated_project do +# get '/sampletv' +# assert_equal 200, last_response.status +# assert_includes last_response.body, 'class="gridster"' +# assert_includes last_response.body, "DOCTYPE" +# end +# end + +# def test_page_title_set_correctly +# with_generated_project do +# get '/sampletv' +# assert_includes last_response.body, '1080p dashboard' +# end +# end + +# def test_get_haml_dashboard +# with_generated_project do |dir| +# File.write(File.join(dir, 'dashboards/hamltest.haml'), '.gridster') +# get '/hamltest' +# assert_equal 200, last_response.status +# assert_includes last_response.body, "class='gridster'" +# end +# end + +# def test_get_haml_widget +# with_generated_project do |dir| +# File.write(File.join(dir, 'widgets/clock/clock.haml'), '%h1 haml') +# File.unlink(File.join(dir, 'widgets/clock/clock.html')) +# get '/views/clock.html' +# assert_equal 200, last_response.status +# assert_includes last_response.body, '

haml

' +# end +# end + +# def test_get_nonexistent_dashboard +# with_generated_project do +# get '/nodashboard' +# assert_equal 404, last_response.status +# end +# end + +# def test_get_widget +# with_generated_project do +# get '/views/meter.html' +# assert_equal 200, last_response.status +# assert_includes last_response.body, 'class="meter"' +# end +# end + +# def with_generated_project +# temp do |dir| +# cli = Dashing::CLI.new +# silent { cli.new 'new_project' } + +# Sinatra::Application.settings.views = File.join(dir, 'new_project/dashboards') +# Sinatra::Application.settings.root = File.join(dir, 'new_project') +# yield Sinatra::Application.settings.root +# end +# end + +# def app +# Sinatra::Application +# end + +# def parse_data(string) +# JSON.parse string[/data: (.+)/, 1] +# end + +# def parse_event(string) +# string[/event: (.+)/, 1] +# end +# end diff --git a/test/cli_test.rb b/test/cli_test.rb index 2bf3b65..567827e 100644 --- a/test/cli_test.rb +++ b/test/cli_test.rb @@ -1,28 +1,168 @@ require 'test_helper' -load_quietly 'bin/dashing' -module Thor::Actions - def source_paths - [File.join(File.expand_path(File.dirname(__FILE__)), '../templates')] +class CLITest < Dashing::Test + def setup + @cli = Dashing::CLI.new + end + + def test_new_task_creates_project_directory + app_name = 'custom_dashboard' + @cli.stubs(:directory).with(:project, app_name).once + @cli.new(app_name) end -end -class CliTest < Dashing::Test + def test_generate_task_delegates_to_type + types = %w(widget dashboard job) - def test_project_directory_created - temp do |dir| - cli = Dashing::CLI.new - silent { cli.new 'Dashboard' } - assert Dir.exist?(File.join(dir,'dashboard')), 'Dashing directory was not created.' + types.each do |type| + @cli.stubs(:public_send).with("generate_#{type}".to_sym, 'name').once + @cli.generate(type, 'name') end end - def test_hyphenate - assert_equal 'power', Dashing::CLI.hyphenate('Power') - assert_equal 'power', Dashing::CLI.hyphenate('POWER') - assert_equal 'power-rangers', Dashing::CLI.hyphenate('PowerRangers') - assert_equal 'power-ranger', Dashing::CLI.hyphenate('Power_ranger') - assert_equal 'super-power-rangers', Dashing::CLI.hyphenate('SuperPowerRangers') + def test_generate_task_warns_when_generator_is_not_defined + output, _ = capture_io do + @cli.generate('wtf', 'name') + end + + assert_includes output, 'Invalid generator' + end + + def test_generate_widget_creates_a_new_widget + @cli.stubs(:directory).with(:widget, 'widgets').once + @cli.generate_widget('WidgetName') + assert_equal 'widget_name', @cli.name + end + + def test_generate_dashboard_creates_a_new_dashboard + @cli.stubs(:directory).with(:dashboard, 'dashboards').once + @cli.generate_dashboard('DashBoardName') + assert_equal 'dash_board_name', @cli.name + end + + def test_generate_job_creates_a_new_job + @cli.stubs(:directory).with(:job, 'jobs').once + @cli.generate_job('MyCustomJob') + assert_equal 'my_custom_job', @cli.name + end + + def test_install_task_requests_gist_from_downloader + return_value = { 'files' => [] } + Dashing::Downloader.stubs(:get_gist).with(123).returns(return_value).once + + capture_io { @cli.install(123) } + end + + def test_install_task_calls_create_file_for_each_valid_file_in_gist + json_response = <<-JSON + { + "files": { + "ruby_job.rb": { "content": "some job content" }, + "num.html": { "content": "some html content" }, + "num.scss": { "content": "some sass content" }, + "num.coffee": { "content": "some coffee content" } + } + } + JSON + + Dir.stubs(:pwd).returns('') + + Dashing::Downloader.stubs(:get_gist).returns(JSON.parse(json_response)) + @cli.stubs(:create_file).with('/jobs/ruby_job.rb', 'some job content').once + @cli.stubs(:create_file).with('/widgets/num/num.html', 'some html content').once + @cli.stubs(:create_file).with('/widgets/num/num.scss', 'some sass content').once + @cli.stubs(:create_file).with('/widgets/num/num.coffee', 'some coffee content').once + + capture_io { @cli.install(123) } + end + + def test_install_task_ignores_invalid_files + json_response = <<-JSON + { + "files": { + "ruby_job.js": { "content": "some job content" }, + "num.css": { "content": "some sass content" } + } + } + JSON + + Dashing::Downloader.stubs(:get_gist).returns(JSON.parse(json_response)) + @cli.stubs(:create_file).never + + capture_io { @cli.install(123) } + end + + def test_install_task_warns_when_gist_not_found + error = OpenURI::HTTPError.new('error', mock()) + Dashing::Downloader.stubs(:get_gist).raises(error) + + output, _ = capture_io { @cli.install(123) } + + assert_includes output, 'Could not find gist at ' + end + + def test_start_task_starts_thin_with_default_port + command = 'bundle exec thin -R config.ru start -p 3030 ' + @cli.stubs(:run_command).with(command).once + @cli.start + end + + def test_start_task_starts_thin_with_specified_port + command = 'bundle exec thin -R config.ru start -p 2020' + @cli.stubs(:run_command).with(command).once + @cli.start('-p', '2020') + end + + def test_start_task_supports_job_path_option + commands = [ + 'export JOB_PATH=other_spot; ', + 'bundle exec thin -R config.ru start -p 3030 ' + ] + + @cli.stubs(:options).returns(job_path: 'other_spot') + @cli.stubs(:run_command).with(commands.join('')).once + @cli.start + end + + def test_stop_task_stops_thin_server + @cli.stubs(:run_command).with('bundle exec thin stop') + @cli.stop + end + + def test_job_task_requires_job_file + Dir.stubs(:pwd).returns('') + @cli.stubs(:require_file).with('/jobs/special_job.rb').once + + @cli.job('special_job') + end + + def test_job_task_requires_every_ruby_file_in_lib + Dir.stubs(:pwd).returns('') + Dir.stubs(:[]).returns(['lib/dashing/cli.rb', 'lib/dashing.rb']) + @cli.stubs(:require_file).times(3) + + @cli.job('special_job') + end + + def test_job_sets_auth_token + @cli.class.stubs(:auth_token=).with('my_token').once + @cli.stubs(:require_file) + + @cli.job('my_job', 'my_token') + end + + def test_hyphenate_lowers_and_hyphenates_inputs + assertion_map = { + 'Power' => 'power', + 'POWER' => 'power', + 'PowerRangers' => 'power-rangers', + 'Power_ranger' => 'power-ranger', + 'SuperPowerRangers' => 'super-power-rangers' + } + + assertion_map.each do |input, expected| + assert_equal expected, Dashing::CLI.hyphenate(input) + end end end diff --git a/test/downloader_test.rb b/test/downloader_test.rb new file mode 100644 index 0000000..930ad56 --- /dev/null +++ b/test/downloader_test.rb @@ -0,0 +1,26 @@ +require 'test_helper' + +class DownloaderTest < Minitest::Test + + def test_get_json_requests_and_parses_content + endpoint = 'http://somehost.com/file.json' + response = '{ "name": "value" }' + FakeWeb.register_uri(:get, endpoint, body: response) + JSON.stubs(:parse).with(response).once + + Dashing::Downloader.get_json(endpoint) + end + + def test_get_json_raises_on_bad_request + FakeWeb.register_uri(:get, 'http://dead-host.com/', status: '404') + + assert_raises(OpenURI::HTTPError) do + Dashing::Downloader.get_json('http://dead-host.com/') + end + end + + def test_load_gist_attempts_to_get_the_gist + Dashing::Downloader.stubs(:get_json).once + Dashing::Downloader.get_gist(123) + end +end diff --git a/test/test_helper.rb b/test/test_helper.rb index 9c51b87..2f753f6 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -1,12 +1,15 @@ require 'rack/test' require 'stringio' require 'tmpdir' - +require 'fakeweb' require 'minitest/autorun' require 'minitest/pride' +require 'mocha/setup' require_relative '../lib/dashing' +FakeWeb.allow_net_connect = false + ENV['RACK_ENV'] = 'test' WORKING_DIRECTORY = Dir.pwd.freeze ARGV.clear -- cgit v1.2.3 From 0c1adbdb9fe428db1f4237e7bf021713b188f8f8 Mon Sep 17 00:00:00 2001 From: pseudomuto Date: Wed, 18 Dec 2013 19:16:02 -0500 Subject: adding simplecov for verifying test coverage --- test/test_helper.rb | 6 ++++++ 1 file changed, 6 insertions(+) (limited to 'test') diff --git a/test/test_helper.rb b/test/test_helper.rb index 2f753f6..0b719f5 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -1,3 +1,9 @@ +require 'simplecov' +SimpleCov.start do + add_filter "/vendor/" + add_filter "/test/" +end + require 'rack/test' require 'stringio' require 'tmpdir' -- cgit v1.2.3 From fbc9497dbb8ece970f21bc67164557bba8db2db7 Mon Sep 17 00:00:00 2001 From: pseudomuto Date: Wed, 18 Dec 2013 19:22:09 -0500 Subject: moving app to it's own file under lib/dashing --- test/app_test.rb | 307 ++++++++++++++++++++++++++++--------------------------- 1 file changed, 154 insertions(+), 153 deletions(-) (limited to 'test') diff --git a/test/app_test.rb b/test/app_test.rb index bf3bbb1..dbb7cb5 100644 --- a/test/app_test.rb +++ b/test/app_test.rb @@ -1,153 +1,154 @@ -# require 'test_helper' -# require 'haml' - -# class AppTest < Dashing::Test -# def setup -# @connection = [] -# Sinatra::Application.settings.history_file = File.join(Dir.tmpdir, 'history.yml') -# Sinatra::Application.settings.connections = [@connection] -# Sinatra::Application.settings.auth_token = nil -# Sinatra::Application.settings.default_dashboard = nil -# end - -# def test_post_widgets_without_auth_token -# post '/widgets/some_widget', JSON.generate({value: 6}) -# assert_equal 204, last_response.status - -# assert_equal 1, @connection.length -# data = parse_data @connection[0] -# assert_equal 6, data['value'] -# assert_equal 'some_widget', data['id'] -# assert data['updatedAt'] -# end - -# def test_post_widgets_with_invalid_auth_token -# Sinatra::Application.settings.auth_token = 'sekrit' -# post '/widgets/some_widget', JSON.generate({value: 9}) -# assert_equal 401, last_response.status -# end - -# def test_post_widgets_with_valid_auth_token -# Sinatra::Application.settings.auth_token = 'sekrit' -# post '/widgets/some_widget', JSON.generate({value: 9, auth_token: 'sekrit'}) -# assert_equal 204, last_response.status -# end - -# def test_get_events -# post '/widgets/some_widget', JSON.generate({value: 8}) -# assert_equal 204, last_response.status - -# get '/events' -# assert_equal 200, last_response.status -# assert_equal 8, parse_data(@connection[0])['value'] -# end - -# def test_dashboard_events -# post '/dashboards/my_super_sweet_dashboard', JSON.generate({event: 'reload'}) -# assert_equal 204, last_response.status - -# get '/events' -# assert_equal 200, last_response.status -# assert_equal 'dashboards', parse_event(@connection[0]) -# assert_equal 'reload', parse_data(@connection[0])['event'] -# end - -# def test_redirect_to_default_dashboard -# with_generated_project do -# Sinatra::Application.settings.default_dashboard = 'test1' -# get '/' -# assert_equal 302, last_response.status -# assert_equal 'http://example.org/test1', last_response.location -# end -# end - -# def test_redirect_to_first_dashboard -# with_generated_project do -# get '/' -# assert_equal 302, last_response.status -# assert_equal 'http://example.org/sample', last_response.location -# end -# end - -# def test_redirect_to_first_dashboard_without_erb -# with_generated_project do |dir| -# FileUtils.touch(File.join(dir, "dashboards/htmltest.html")) -# get '/' -# assert_equal 302, last_response.status -# assert_equal 'http://example.org/htmltest', last_response.location -# end -# end - -# def test_get_dashboard -# with_generated_project do -# get '/sampletv' -# assert_equal 200, last_response.status -# assert_includes last_response.body, 'class="gridster"' -# assert_includes last_response.body, "DOCTYPE" -# end -# end - -# def test_page_title_set_correctly -# with_generated_project do -# get '/sampletv' -# assert_includes last_response.body, '1080p dashboard' -# end -# end - -# def test_get_haml_dashboard -# with_generated_project do |dir| -# File.write(File.join(dir, 'dashboards/hamltest.haml'), '.gridster') -# get '/hamltest' -# assert_equal 200, last_response.status -# assert_includes last_response.body, "class='gridster'" -# end -# end - -# def test_get_haml_widget -# with_generated_project do |dir| -# File.write(File.join(dir, 'widgets/clock/clock.haml'), '%h1 haml') -# File.unlink(File.join(dir, 'widgets/clock/clock.html')) -# get '/views/clock.html' -# assert_equal 200, last_response.status -# assert_includes last_response.body, '

haml

' -# end -# end - -# def test_get_nonexistent_dashboard -# with_generated_project do -# get '/nodashboard' -# assert_equal 404, last_response.status -# end -# end - -# def test_get_widget -# with_generated_project do -# get '/views/meter.html' -# assert_equal 200, last_response.status -# assert_includes last_response.body, 'class="meter"' -# end -# end - -# def with_generated_project -# temp do |dir| -# cli = Dashing::CLI.new -# silent { cli.new 'new_project' } - -# Sinatra::Application.settings.views = File.join(dir, 'new_project/dashboards') -# Sinatra::Application.settings.root = File.join(dir, 'new_project') -# yield Sinatra::Application.settings.root -# end -# end - -# def app -# Sinatra::Application -# end - -# def parse_data(string) -# JSON.parse string[/data: (.+)/, 1] -# end - -# def parse_event(string) -# string[/event: (.+)/, 1] -# end -# end +require 'test_helper' +require 'haml' + +class AppTest < Dashing::Test + def setup + @connection = [] + Sinatra::Application.settings.history_file = File.join(Dir.tmpdir, 'history.yml') + Sinatra::Application.settings.connections = [@connection] + Sinatra::Application.settings.auth_token = nil + Sinatra::Application.settings.default_dashboard = nil + end + + def test_post_widgets_without_auth_token + post '/widgets/some_widget', JSON.generate({value: 6}) + assert_equal 204, last_response.status + + assert_equal 1, @connection.length + data = parse_data @connection[0] + assert_equal 6, data['value'] + assert_equal 'some_widget', data['id'] + assert data['updatedAt'] + end + + def test_post_widgets_with_invalid_auth_token + Sinatra::Application.settings.auth_token = 'sekrit' + post '/widgets/some_widget', JSON.generate({value: 9}) + assert_equal 401, last_response.status + end + + def test_post_widgets_with_valid_auth_token + Sinatra::Application.settings.auth_token = 'sekrit' + post '/widgets/some_widget', JSON.generate({value: 9, auth_token: 'sekrit'}) + assert_equal 204, last_response.status + end + + def test_get_events + post '/widgets/some_widget', JSON.generate({value: 8}) + assert_equal 204, last_response.status + + get '/events' + assert_equal 200, last_response.status + assert_equal 8, parse_data(@connection[0])['value'] + end + + def test_dashboard_events + post '/dashboards/my_super_sweet_dashboard', JSON.generate({event: 'reload'}) + assert_equal 204, last_response.status + + get '/events' + assert_equal 200, last_response.status + assert_equal 'dashboards', parse_event(@connection[0]) + assert_equal 'reload', parse_data(@connection[0])['event'] + end + + def test_redirect_to_default_dashboard + with_generated_project do + Sinatra::Application.settings.default_dashboard = 'test1' + get '/' + assert_equal 302, last_response.status + assert_equal 'http://example.org/test1', last_response.location + end + end + + def test_redirect_to_first_dashboard + with_generated_project do + get '/' + assert_equal 302, last_response.status + assert_equal 'http://example.org/sample', last_response.location + end + end + + def test_redirect_to_first_dashboard_without_erb + with_generated_project do |dir| + FileUtils.touch(File.join(dir, "dashboards/htmltest.html")) + get '/' + assert_equal 302, last_response.status + assert_equal 'http://example.org/htmltest', last_response.location + end + end + + def test_get_dashboard + with_generated_project do + get '/sampletv' + assert_equal 200, last_response.status + assert_includes last_response.body, 'class="gridster"' + assert_includes last_response.body, "DOCTYPE" + end + end + + def test_page_title_set_correctly + with_generated_project do + get '/sampletv' + assert_includes last_response.body, '1080p dashboard' + end + end + + def test_get_haml_dashboard + with_generated_project do |dir| + File.write(File.join(dir, 'dashboards/hamltest.haml'), '.gridster') + get '/hamltest' + assert_equal 200, last_response.status + assert_includes last_response.body, "class='gridster'" + end + end + + def test_get_haml_widget + with_generated_project do |dir| + File.write(File.join(dir, 'widgets/clock/clock.haml'), '%h1 haml') + File.unlink(File.join(dir, 'widgets/clock/clock.html')) + get '/views/clock.html' + assert_equal 200, last_response.status + assert_includes last_response.body, '

haml

' + end + end + + def test_get_nonexistent_dashboard + with_generated_project do + get '/nodashboard' + assert_equal 404, last_response.status + end + end + + def test_get_widget + with_generated_project do + get '/views/meter.html' + assert_equal 200, last_response.status + assert_includes last_response.body, 'class="meter"' + end + end + + def with_generated_project + temp do |dir| + cli = Dashing::CLI.new + cli.stubs(:source_paths).returns([File.expand_path('../../templates', __FILE__)]) + silent { cli.new 'new_project' } + + Sinatra::Application.settings.views = File.join(dir, 'new_project/dashboards') + Sinatra::Application.settings.root = File.join(dir, 'new_project') + yield Sinatra::Application.settings.root + end + end + + def app + Sinatra::Application + end + + def parse_data(string) + JSON.parse string[/data: (.+)/, 1] + end + + def parse_event(string) + string[/event: (.+)/, 1] + end +end -- cgit v1.2.3 From e975ce77408f5995213cc1b85f61e806152ba3a2 Mon Sep 17 00:00:00 2001 From: pseudomuto Date: Thu, 19 Dec 2013 12:08:07 -0500 Subject: light refactoring for better test coverage --- test/app_test.rb | 83 ++++++++++++++++++++++++++++++++------------------------ 1 file changed, 47 insertions(+), 36 deletions(-) (limited to 'test') diff --git a/test/app_test.rb b/test/app_test.rb index dbb7cb5..4101e91 100644 --- a/test/app_test.rb +++ b/test/app_test.rb @@ -4,10 +4,45 @@ require 'haml' class AppTest < Dashing::Test def setup @connection = [] - Sinatra::Application.settings.history_file = File.join(Dir.tmpdir, 'history.yml') - Sinatra::Application.settings.connections = [@connection] - Sinatra::Application.settings.auth_token = nil - Sinatra::Application.settings.default_dashboard = nil + app.settings.connections = [@connection] + app.settings.auth_token = nil + app.settings.default_dashboard = nil + app.settings.history_file = File.join(Dir.tmpdir, 'history.yml') + end + + def test_redirect_to_first_dashboard + with_generated_project do + get '/' + assert_equal 302, last_response.status + assert_equal 'http://example.org/sample', last_response.location + end + end + + def test_redirect_to_first_dashboard_without_erb + with_generated_project do |dir| + FileUtils.touch(File.join(dir, "dashboards/htmltest.html")) + get '/' + assert_equal 302, last_response.status + assert_equal 'http://example.org/htmltest', last_response.location + end + end + + def test_redirect_to_default_dashboard + with_generated_project do + app.settings.default_dashboard = 'test1' + get '/' + assert_equal 302, last_response.status + assert_equal 'http://example.org/test1', last_response.location + end + end + + def test_errors_out_when_no_dashboards_available + with_generated_project do + app.settings.views = File.join(app.settings.root, 'lib') + + get '/' + assert_equal 500, last_response.status + end end def test_post_widgets_without_auth_token @@ -22,13 +57,13 @@ class AppTest < Dashing::Test end def test_post_widgets_with_invalid_auth_token - Sinatra::Application.settings.auth_token = 'sekrit' + app.settings.auth_token = 'sekrit' post '/widgets/some_widget', JSON.generate({value: 9}) assert_equal 401, last_response.status end def test_post_widgets_with_valid_auth_token - Sinatra::Application.settings.auth_token = 'sekrit' + app.settings.auth_token = 'sekrit' post '/widgets/some_widget', JSON.generate({value: 9, auth_token: 'sekrit'}) assert_equal 204, last_response.status end @@ -52,32 +87,6 @@ class AppTest < Dashing::Test assert_equal 'reload', parse_data(@connection[0])['event'] end - def test_redirect_to_default_dashboard - with_generated_project do - Sinatra::Application.settings.default_dashboard = 'test1' - get '/' - assert_equal 302, last_response.status - assert_equal 'http://example.org/test1', last_response.location - end - end - - def test_redirect_to_first_dashboard - with_generated_project do - get '/' - assert_equal 302, last_response.status - assert_equal 'http://example.org/sample', last_response.location - end - end - - def test_redirect_to_first_dashboard_without_erb - with_generated_project do |dir| - FileUtils.touch(File.join(dir, "dashboards/htmltest.html")) - get '/' - assert_equal 302, last_response.status - assert_equal 'http://example.org/htmltest', last_response.location - end - end - def test_get_dashboard with_generated_project do get '/sampletv' @@ -129,14 +138,16 @@ class AppTest < Dashing::Test end def with_generated_project + source_path = File.expand_path('../../templates', __FILE__) + temp do |dir| cli = Dashing::CLI.new - cli.stubs(:source_paths).returns([File.expand_path('../../templates', __FILE__)]) + cli.stubs(:source_paths).returns([source_path]) silent { cli.new 'new_project' } - Sinatra::Application.settings.views = File.join(dir, 'new_project/dashboards') - Sinatra::Application.settings.root = File.join(dir, 'new_project') - yield Sinatra::Application.settings.root + app.settings.views = File.join(dir, 'new_project/dashboards') + app.settings.root = File.join(dir, 'new_project') + yield app.settings.root end end -- cgit v1.2.3 From 8fe3a7ec617221a2387bfe9329e731b4cb836062 Mon Sep 17 00:00:00 2001 From: Fredrik Vihlborg Date: Thu, 28 Aug 2014 18:35:32 +0200 Subject: Updated tests accordingly. --- test/cli_test.rb | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'test') diff --git a/test/cli_test.rb b/test/cli_test.rb index 567827e..4d82296 100644 --- a/test/cli_test.rb +++ b/test/cli_test.rb @@ -68,10 +68,10 @@ class CLITest < Dashing::Test Dir.stubs(:pwd).returns('') Dashing::Downloader.stubs(:get_gist).returns(JSON.parse(json_response)) - @cli.stubs(:create_file).with('/jobs/ruby_job.rb', 'some job content').once - @cli.stubs(:create_file).with('/widgets/num/num.html', 'some html content').once - @cli.stubs(:create_file).with('/widgets/num/num.scss', 'some sass content').once - @cli.stubs(:create_file).with('/widgets/num/num.coffee', 'some coffee content').once + @cli.stubs(:create_file).with('/jobs/ruby_job.rb', 'some job content', {:skip => false}).once + @cli.stubs(:create_file).with('/widgets/num/num.html', 'some html content', {:skip => false}).once + @cli.stubs(:create_file).with('/widgets/num/num.scss', 'some sass content', {:skip => false}).once + @cli.stubs(:create_file).with('/widgets/num/num.coffee', 'some coffee content', {:skip => false}).once capture_io { @cli.install(123) } end -- cgit v1.2.3 From 937fcce2dcca8010dbe8e9e26df954cb1330c1e0 Mon Sep 17 00:00:00 2001 From: Sven Dahlstrand Date: Mon, 27 Apr 2015 00:43:59 +0200 Subject: Pass `status` option to `send_file` and make sure a 404 is returned. --- test/app_test.rb | 7 +++++++ 1 file changed, 7 insertions(+) (limited to 'test') diff --git a/test/app_test.rb b/test/app_test.rb index 4101e91..4acb27a 100644 --- a/test/app_test.rb +++ b/test/app_test.rb @@ -129,6 +129,13 @@ class AppTest < Dashing::Test end end + def test_get_nonexistent_dashboard_sends_file_with_404_status + with_generated_project do + app.any_instance.expects(:send_file).with(anything, has_entry(:status, 404)) + get '/nodashboard' + end + end + def test_get_widget with_generated_project do get '/views/meter.html' -- cgit v1.2.3 From 5c4e62f0987bd900d0f2c510bb3cb9f35bfd28a7 Mon Sep 17 00:00:00 2001 From: Sven Dahlstrand Date: Wed, 29 Apr 2015 23:59:40 +0200 Subject: Set public folder setting for generated project. --- test/app_test.rb | 1 + 1 file changed, 1 insertion(+) (limited to 'test') diff --git a/test/app_test.rb b/test/app_test.rb index 4acb27a..0ac66d0 100644 --- a/test/app_test.rb +++ b/test/app_test.rb @@ -152,6 +152,7 @@ class AppTest < Dashing::Test cli.stubs(:source_paths).returns([source_path]) silent { cli.new 'new_project' } + app.settings.public_folder = File.join(dir, 'new_project/public') app.settings.views = File.join(dir, 'new_project/dashboards') app.settings.root = File.join(dir, 'new_project') yield app.settings.root -- cgit v1.2.3 From 69554869db9f0273ef2e309ceca1e990b155bd68 Mon Sep 17 00:00:00 2001 From: Sven Dahlstrand Date: Thu, 30 Apr 2015 00:00:59 +0200 Subject: Remove redundant test. --- test/app_test.rb | 7 ------- 1 file changed, 7 deletions(-) (limited to 'test') diff --git a/test/app_test.rb b/test/app_test.rb index 0ac66d0..36cd310 100644 --- a/test/app_test.rb +++ b/test/app_test.rb @@ -129,13 +129,6 @@ class AppTest < Dashing::Test end end - def test_get_nonexistent_dashboard_sends_file_with_404_status - with_generated_project do - app.any_instance.expects(:send_file).with(anything, has_entry(:status, 404)) - get '/nodashboard' - end - end - def test_get_widget with_generated_project do get '/views/meter.html' -- cgit v1.2.3