the very first tests for the API
This commit is contained in:
parent
792ebd142b
commit
9f2cab94cb
19
features/api/authentication.feature
Normal file
19
features/api/authentication.feature
Normal file
@ -0,0 +1,19 @@
|
||||
Feature: Authentication
|
||||
In order to consume the API
|
||||
As an admin
|
||||
I need to get an authentication token
|
||||
|
||||
Background:
|
||||
Given I have the site: "test site" set up
|
||||
|
||||
Scenario: Fail to get a token without an email and a password
|
||||
When I post to "/locomotive/api/tokens.json"
|
||||
Then the JSON response at "message" should be "The request must contain the user email and password."
|
||||
|
||||
Scenario: Get a token
|
||||
When I post to "/locomotive/api/tokens.json" with:
|
||||
"""
|
||||
{ "email": "admin@locomotiveapp.org", "password": "easyone" }
|
||||
"""
|
||||
Then the JSON response at "token" should be a string
|
||||
And the JSON response should not have "message"
|
29
features/api/pages.feature
Normal file
29
features/api/pages.feature
Normal file
@ -0,0 +1,29 @@
|
||||
Feature: Pages
|
||||
In order to access the Page resources
|
||||
As an admin
|
||||
I will perform the basic RESTFUL actions on them
|
||||
|
||||
Background:
|
||||
Given I have the site: "test site" set up
|
||||
And I have an "admin" API token
|
||||
And a page named "hello world" with the template:
|
||||
"""
|
||||
Hello world :-)
|
||||
"""
|
||||
And a page named "goodbye-world" with the template:
|
||||
"""
|
||||
Goodbye world :-(
|
||||
"""
|
||||
|
||||
Scenario: Protect the pages resources if not authenticated
|
||||
Given I do not have an API token
|
||||
When I visit "/locomotive/api/pages.json"
|
||||
Then the JSON response at "error" should be "You need to sign in or sign up before continuing."
|
||||
|
||||
Scenario: Accessing pages
|
||||
When I visit "/locomotive/api/pages.json"
|
||||
Then the JSON should have the following:
|
||||
| 0/fullpath | "index" |
|
||||
| 1/fullpath | "hello-world" |
|
||||
| 2/fullpath | "404" |
|
||||
| 3/fullpath | "goodbye-world" |
|
38
features/step_definitions/api_steps.rb
Normal file
38
features/step_definitions/api_steps.rb
Normal file
@ -0,0 +1,38 @@
|
||||
def last_json
|
||||
@json_response.try(:body) || page.source
|
||||
end
|
||||
|
||||
Given /^I have an? "([^"]*)" API token$/ do |role|
|
||||
@membership = Locomotive::Site.first.memberships.where(:role => role.downcase).first \
|
||||
|| FactoryGirl.create(role.downcase.to_sym, :site => Locomotive::Site.first)
|
||||
|
||||
login_params = {
|
||||
'email' => @membership.account.email,
|
||||
'password' => 'easyone'
|
||||
}
|
||||
|
||||
response = post("http://#{@site.domains.first}/locomotive/api/tokens.json", login_params.to_json, { 'CONTENT_TYPE' => 'application/json' })
|
||||
|
||||
if response.status == 200
|
||||
@auth_token = JSON.parse(response.body)['token']
|
||||
else
|
||||
raise JSON.parse(response.body)['message']
|
||||
end
|
||||
end
|
||||
|
||||
Given /^I do not have an API token$/ do
|
||||
@auth_token = nil
|
||||
end
|
||||
|
||||
When /^I visit "([^"]*)"$/ do |path|
|
||||
@json_response = get("http://#{@site.domains.first}#{path}", { auth_token: @auth_token }, { 'CONTENT_TYPE' => 'application/json' })
|
||||
end
|
||||
|
||||
# http://stackoverflow.com/questions/9009392/racktest-put-method-with-json-fails-to-convert-json-to-params
|
||||
When /^I post to "([^"]*)"$/ do |path|
|
||||
@json_response = post("http://#{@site.domains.first}#{path}", '', { 'CONTENT_TYPE' => 'application/json' })
|
||||
end
|
||||
|
||||
When /^I post to "([^"]*)" with:$/ do |path, json_string|
|
||||
@json_response = post("http://#{@site.domains.first}#{path}", json_string, { 'CONTENT_TYPE' => 'application/json' })
|
||||
end
|
Loading…
Reference in New Issue
Block a user