diff options
Diffstat (limited to 'features')
-rw-r--r-- | features/messages.feature | 74 | ||||
-rw-r--r-- | features/step_definitions/api_steps.rb | 8 | ||||
-rw-r--r-- | features/step_definitions/messages_steps.rb | 32 | ||||
-rw-r--r-- | features/support/hooks.rb | 14 |
4 files changed, 124 insertions, 4 deletions
diff --git a/features/messages.feature b/features/messages.feature new file mode 100644 index 0000000..34c0892 --- /dev/null +++ b/features/messages.feature @@ -0,0 +1,74 @@ +Feature: Receive messages for the user + + In order to stay in touch with the provider + As an authenticated user + I want to receive messages from the provider + + Background: + Given I authenticated + Given I set headers: + | Accept | application/json | + | Content-Type | application/json | + | Authorization | Token token="MY_AUTH_TOKEN" | + + Scenario: There are no messages yet + When I send a GET request to "/1/messages.json" + Then the response status should be "200" + And the response should be: + """ + [] + """ + + Scenario: Fetch the unread messages + Given there is a message for me with: + | id | 1a2b3c4d | + | text | Your provider says hi ! | + When I send a GET request to "/1/messages.json" + Then the response status should be "200" + And the response should be: + """ + [{ + "id": "1a2b3c4d", + "text": "Your provider says hi !" + }] + """ + + Scenario: Send unread messages until marked as read + Given there is a message for me + And I have sent a GET request to "/1/messages.json" + When I send a GET request to "/1/messages.json" + Then the response status should be "200" + And the response should include that message + + Scenario: Mark message as read + Given there is a message for me with: + | id | 1a2b3c4d | + When I send a PUT request to "/1/messages/1a2b3c4d.json" + Then the response status should be "200" + And the response should be: + """ + { "success": "marked as read", + "message": "The message has been marked as read" } + """ + And that message should be marked as read + + Scenario: Message not found + When I send a PUT request to "/1/messages/1a2b3c4d.json" + Then the response status should be "404" + And the response should be: + """ + { "error": "not found", + "message": "The message could not be found" } + """ + + Scenario: Do not send read messages + Given there is a message for me + And that message is marked as read + When I send a GET request to "/1/messages.json" + Then the response status should be "200" + And the response should be: + """ + [] + """ + + diff --git a/features/step_definitions/api_steps.rb b/features/step_definitions/api_steps.rb index a4f369c..7188694 100644 --- a/features/step_definitions/api_steps.rb +++ b/features/step_definitions/api_steps.rb @@ -13,8 +13,9 @@ end Given /^I set headers:$/ do |headers| headers.rows_hash.each do |key,value| - value.sub!('MY_AUTH_TOKEN', @my_auth_token.to_s) if @my_auth_token - header key, value + replace = value.dup + replace.sub!('MY_AUTH_TOKEN', @my_auth_token.to_s) if @my_auth_token + header key, replace end end @@ -36,7 +37,7 @@ When /^I digest\-authenticate as the user "(.*?)" with the password "(.*?)"$/ do digest_authorize user, pass end -When /^I send a (GET|POST|PUT|DELETE) request (?:for|to) "([^"]*)"(?: with the following:)?$/ do |*args| +When /^I (?:have sent|send) a (GET|POST|PUT|DELETE) request (?:for|to) "([^"]*)"(?: with the following:)?$/ do |*args| request_type = args.shift path = args.shift input = args.shift @@ -50,7 +51,6 @@ When /^I send a (GET|POST|PUT|DELETE) request (?:for|to) "([^"]*)"(?: with the f request_opts[:input] = input end end - request path, request_opts end diff --git a/features/step_definitions/messages_steps.rb b/features/step_definitions/messages_steps.rb new file mode 100644 index 0000000..30bc7c3 --- /dev/null +++ b/features/step_definitions/messages_steps.rb @@ -0,0 +1,32 @@ +Given /^there is a message for me$/ do + @message = FactoryGirl.create :message, user_ids_to_show: [@user.id] +end + +Given /^there is a message for me with:$/ do |options| + attributes = options.rows_hash + attributes.merge! user_ids_to_show: [@user.id] + if old_message = Message.find(attributes['id']) + old_message.destroy + end + @message = FactoryGirl.create :message, attributes +end + +Given(/^that message is marked as read$/) do + @message.mark_as_read_by(@user) + @message.save +end + +Then /^the response should (not)?\s?include that message$/ do |negative| + json = JSON.parse(last_response.body) + message = json.detect{|message| message['id'] == @message.id} + if negative.present? + assert !message + else + assert_equal @message.text, message['text'] + end +end + +Then /^that message should be marked as read$/ do + assert @message.reload.read_by? @user + assert !@message.unread_by?(@user) +end diff --git a/features/support/hooks.rb b/features/support/hooks.rb index 19928d8..f11e602 100644 --- a/features/support/hooks.rb +++ b/features/support/hooks.rb @@ -5,6 +5,7 @@ After '@tempfile' do end end +# store end of server log for failing scenarios After do |scenario| if scenario.failed? logfile_path = Rails.root + 'tmp' @@ -16,3 +17,16 @@ After do |scenario| end end end + +# clear all records we created +After do + names = self.instance_variables.reject do |v| + v.to_s.starts_with?('@_') + end + names.each do |name| + record = self.instance_variable_get name + if record.is_a?(CouchRest::Model::Base) && record.persisted? + record.reload && record.destroy + end + end +end |