Skip to content
This repository was archived by the owner on Mar 7, 2018. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions lib/dashing/app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,22 @@ def protected!
end
end

post '/widgets' do
request.body.rewind
body = JSON.parse(request.body.read)
auth_token = body.delete("auth_token")
if !settings.auth_token || settings.auth_token == auth_token
body["widgets"].each do |widget|
widget_id = widget.delete("id")
send_event(widget_id, widget)
end
204 # response without entity body

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if you just return status 204 the comment will not be necessary anymore.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the existing pattern in the file -- they should all be modified in this way if desired.

My preference is to leave it as a comment, so that you don't have to look up 204. IMHO it is not a very common HTTP response code.

else
status 401
"Invalid API key\n"
end
end

post '/widgets/:id' do
request.body.rewind
body = JSON.parse(request.body.read)
Expand Down
22 changes: 22 additions & 0 deletions test/app_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,22 @@ def test_post_widgets_without_auth_token
assert data['updatedAt']
end

def test_post_multiple_widgets_without_auth_token
post '/widgets', JSON.generate({widgets: [{id: 'some_widget', value: 9}, {id: 'another_widget', value: 8}]})
assert_equal 204, last_response.status

assert_equal 2, @connection.length
data = parse_data @connection[0]
assert_equal (9), data['value']
assert_equal 'some_widget', data['id']
assert data['updatedAt']

data = parse_data @connection[1]
assert_equal (8), data['value']
assert_equal 'another_widget', data['id']
assert data['updatedAt']
end

def test_post_widgets_with_invalid_auth_token
app.settings.auth_token = 'sekrit'
post '/widgets/some_widget', JSON.generate({value: 9})
Expand All @@ -68,6 +84,12 @@ def test_post_widgets_with_valid_auth_token
assert_equal 204, last_response.status
end

def test_post_multiple_widgets_with_valid_auth_token
app.settings.auth_token = 'sekrit'
post '/widgets', JSON.generate({auth_token: 'sekrit', widgets: [{id: 'some_widget', value: 9}, {id: 'another_widget', value: 8}]})
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
Expand Down