Skip to main content

rspecs for devise-token-auth | Ruby On Rails


sessions_controller_spec.rb

require 'rails_helper'
RSpec.describe DeviseTokenAuth::SessionsController, type: :controller do
before do
@request.env["devise.mapping"] = Devise.mappings[:user]
end

describe "POST #create" do
context "when input invalid credentials" do
it "should not login" do
post :create, params: {
email: 'invalid_user@yopmail.com',
password: 'invalid_password'
}

expect(response.status).to eq(401)
expect(subject.current_user).to be_nil
end
end

context "when input valid credentials" do
let(:valid_email) { 'valid_user@yopmail.com' }
let(:valid_password) { 'password' }
let!(:user) { create(:user, :with_role, email: valid_email, password: valid_password, role_name: 'aba_admin') }
it "should login successfully" do
post :create, params: {
email: valid_email,
password: valid_password
}

expect(response).to have_http_status :ok
expect(subject.current_user.id).to eq(user.id)
end
end
end

describe "DELETE #destroy" do
let!(:user) { create(:user, :with_role, role_name: 'aba_admin') }
let!(:auth_headers) { user.create_new_auth_token }

context "when sign out" do
it "should logout successfully" do
set_auth_headers(auth_headers)
delete :destroy
response_body = JSON.parse(response.body)

expect(response.status).to eq(200)
expect(response_body['success']).to eq(true)
end

it "should not logout" do
delete :destroy
response_body = JSON.parse(response.body)

expect(response.status).to eq(404)
expect(response_body['success']).to eq(false)
expect(response_body['errors']).to include('User was not found or was not logged in.')
end
end
end
end



registrations_controller_spec.rb

# require 'swagger_helper'
require 'rails_helper'

RSpec.describe DeviseTokenAuth::RegistrationsController, type: :controller do
before(:each) do
@request.env["devise.mapping"] = Devise.mappings[:user]
@email = "testuser1@yopmail.com"
end

describe "POST #create" do
context "when input valid values" do
let(:password) { 'Abcd@123' }

it "should create a user" do
post :create, params: {
email: @email,
password: password,
confirm_password: password
}
user = JSON.parse(response.body)

expect(response).to have_http_status :ok
expect(user["data"]["email"]).to eq(@email)
end
end

context "when input invalid values" do
it "should fail for invalid email" do
post :create, params: {
email: 'user'
}
response_body = JSON.parse(response.body)

expect(response.status).to eq(422)
expect(response_body["errors"]["email"]).to include("is not an email")
end

it "should fail for invalid password" do
post :create, params: {
email: @email
}
response_body = JSON.parse(response.body)
expect(response.status).to eq(422)
expect(response_body["errors"]["password"]).to include("can't be blank")
end
end
end
end

passwords_controller_spec.rb

require 'rails_helper'
RSpec.describe DeviseTokenAuth::PasswordsController, type: :controller do
before do
@request.env["devise.mapping"] = Devise.mappings[:user]
end

describe "POST #create" do
let(:valid_email) { 'valid_user@yopmail.com' }
let(:valid_password) { 'Abcd@123' }
let!(:user) { create(:user, :with_role, email: valid_email, password: valid_password, role_name: 'aba_admin') }
context "when input invalid email" do
it "should raise error" do
post :create, params: {
email: 'invalid_user@yopmail.com',
redirect_url: '/'
}
expect(response.status).to eq(404)
expect(user.reload.reset_password_token).not_to be_present
end
end
context "when redirect url is missing" do
it "should raise error" do
post :create, params: {
email: valid_email
}
expect(response.status).to eq(401)
expect(user.reload.reset_password_token).not_to be_present
end
end
context "when input valid email" do
it "should login successfully" do
post :create, params: {
email: valid_email,
redirect_url: '/'
}
expect(response).to have_http_status :ok
expect(user.reload.reset_password_token).to be_present
end
end
end
end

Comments

Popular posts from this blog

Why "F" and "L" suffix | (10.0F, 10L)

Let us take it this way, We will create their needs. So we will get why they are needed. Try to guess, which functions will be executed in the following program: public class MyClass {     public static void main(String args[]) {         MyClass obj = new MyClass();         obj.fun1(10);     }     void fun1(byte val){         System.out.println(val);     }     void fun1(int val){         System.out.println(val);     }     void fun1(float val){         System.out.println(val);     }     void fun1(long val){         System.out.println(val);     }     } It seems like every method is capable to run this program because 10 is still literal because It has no data type. Before Java, In previous technologies, this scenario gave an ambiguity error. But Java solves this problem by removing the concepts of literals. It means Java provide a data type immediately when these born. So here 10 is no more literal. Java provides Integer data type for it. So now it is of Integer t

Promises and Async-await in depth : Asynchronous Programming in Javascript

Promises and Asynchronous Programming One of the most powerful aspects of JavaScript is how easily it handles asynchronous programming. As a language created for the Web, JavaScript needed to be able to respond to asynchronous user interactions such as clicks and key presses from the beginning. Node.js further popularized asynchronous programming in JavaScript by using callbacks as an alternative to events. As more and more programs started using asynchronous programming, events and callbacks were no longer powerful enough to support everything developers wanted to do.  Promises  are the solution to this problem. Promises are another option for asynchronous programming, and they work like futures and deferreds do in other languages. A promise specifies some code to be executed later (as with events and callbacks) and also explicitly indicates whether the code succeeded or failed at its job. You can chain promises together based on success or failure in ways that make your code easier t

Swagger File | Devise Token Auth

openapi : 3.0.1 info : title : API consumes : - application/json produces : - application/json servers : - url : http://localhost:3000 schemes : - "https" - "http" paths : "/auth" : post : summary : User registration requestBody : content : application/json : schema : $ref : "#/definitions/UserRegistrationParameters" responses : "200" : description : "Valid input" content : application/json : example : status : 'success' data : email : "testuser2@yopmail.com" uid : "testuser2@yopmail.com" first_name : "testuser2" last_name : "lname" role : "ABA Admin" "422" : description : "Invalid input"