-
Notifications
You must be signed in to change notification settings - Fork 239
Testing
Gonzalo Bulnes Guilpain edited this page Feb 11, 2016
·
4 revisions
Here is an example of how you can test-drive your configuration using Minitest:
class SomeControllerTest < ActionController::TestCase
test "index with token authentication via query params" do
get :index, { user_email: "[email protected]", user_token: "1G8_s7P-V-4MGojaKD7a" }
assert_response :success
end
test "index with token authentication via request headers" do
@request.headers['X-User-Email'] = "[email protected]"
@request.headers['X-User-Token'] = "1G8_s7P-V-4MGojaKD7a"
get :index
assert_response :success
end
end
Here is an example of how you can test-drive your configuration using Rspec:
require 'rails_helper'
RSpec.describe 'when user logs in', type: :request do
describe "POST #create as :json" do
let(:endpoint) { user_session_path }
let(:headers) { {'Content-Type': 'application/json', 'Accept': 'application/json' } }
let(:user) { create(:user) }
let(:email) { user.email }
let(:password) { user.password }
let(:right_request) { { user: { email: email, password: password } } }
let(:wrong_request) { { user: { email: email, password: 'wrong_password' } } }
it "should successfully return access token" do
post endpoint, JSON.dump(right_request), headers
expect(response).to be_success
json = JSON.parse(response.body)
expect(json['authentication_token']).to_not be_empty
end
it "should return error message" do
post endpoint, JSON.dump(wrong_request), headers
expect(response).to have_http_status(401)
json = JSON.parse(response.body)
expect(json['error']).to eq I18n.t('devise.failure.invalid')
end
end
end
See #46.