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. --- bin/dashing | 8 ------ test/app_test.rb | 84 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 84 insertions(+), 8 deletions(-) create mode 100644 test/app_test.rb diff --git a/bin/dashing b/bin/dashing index 8c7e348..30824ce 100755 --- a/bin/dashing +++ b/bin/dashing @@ -11,14 +11,6 @@ class MockScheduler end end -def send_event(id, data) - req = Net::HTTP::Post.new("/widgets/#{id}") - req["content-type"] = "application/json" - req.body = JSON.unparse(data.merge(:auth_token => Dashing::CLI.auth_token)) - res = Net::HTTP.new('localhost', 3030).start { |http| http.request(req) } - puts "Data Sent to #{id}: #{data}" -end - SCHEDULER = MockScheduler.new module Dashing 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