RSpec.describe "Users", type: :request do
let(:user) { create(:user) }
let(:headers) { valid_headers(user.username) }
describe 'PATCH /users/logout' do
before { patch users_logout_path, params: {}, headers: headers }
it 'nullifies user token' do
expect(user.token).to be_nil
expect(response).to have_http_status(204)
end
end
enddef valid_headers(username)
{
'Authorization' => "Bearer #{token_generator(username)}",
'Content-Type' => 'application/json'
}
end
def token_generator(username)
JsonWebToken.encode(sub: username)
enddef logout
@current_user.update_attribute(:token, nil)
head :no_content
end#ApplicationController
class ApplicationController < ActionController::API
before_action :authorize_request
attr_reader :current_user
private
def authorize_request
@current_user = (AuthorizeApiRequest.new(request.headers).call)[:user]
end
endRSpec.describe 'Users', type: :request do
let(:user) { create(:user) }
let(:headers) { valid_headers(user.username) }
describe 'PATCH /users/logout' do
before do
patch users_logout_path, params: {}, headers: headers
end
it 'nullifies user token' do
user_without_token = User.find(user.id)
expect(user_without_token.token).to be_nil
expect(response).to have_http_status(204)
end
end
end--
You received this message because you are subscribed to a topic in the Google Groups "rspec" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/rspec/pVS-VwwtRGM/unsubscribe.
To unsubscribe from this group and all its topics, send an email to rspec+un...@googlegroups.com.
To post to this group, send email to rs...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rspec/8d3f0603-465d-4388-9b4f-813dd511fbe0%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.