summaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorAaron Peckham <apeckham@gmail.com>2013-05-07 15:09:54 -0700
committerpushmatrix <daniel.beauchamp@gmail.com>2013-05-09 21:27:14 +0200
commit1a974c754789b0b675d39442cead91213f706851 (patch)
tree275996a22af820678cfcfd44fe648f9e4f5c3b4a /test
parent31bdc6da2706eec95c54ca46cb8e544661296bf6 (diff)
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.
Diffstat (limited to 'test')
-rw-r--r--test/app_test.rb84
1 files changed, 84 insertions, 0 deletions
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