mardi 2 avril 2019

Rails 5 how to write api controller functional test for session create action

i am writing functional test for api session controller create 
action
here is my code which i am running


this is my api session_controller.rb 

class Api::V2::Users::SessionsController < Api::V2::BaseController

prepend_before_action :allow_params_authentication!, only: :create
skip_before_action :authenticate_user!, only: :create

def create
 resource = warden.authenticate!(scope: :user)
 sign_in(:user, resource)
 yield resource if block_given?
 render json: {access_token: current_token, user: resource}
end


def destroy
 signed_out = (Devise.sign_out_all_scopes ? sign_out : 
 sign_out(resource_name))
 yield if block_given?
  head :no_content
 end

 private

 def current_token
 request.env['warden-jwt_auth.token']
 end
 end


and this is my sessions_controller_test.rb


require 'test_helper'
require 'devise/jwt/test_helpers'

//controller_test
class SessionsControllerTest < ActionDispatch::IntegrationTest

  token = Knock::AuthToken.new(payload: { sub: user.id }).token
  token = { 'Authorization': "Bearer #{token}" }


   URL = '/v2/users?me=true'
   AUTH_URL = '/user_token'
   describe 'GET /users?me=true' do
   it 'should return the user info' do
   user  = create(:user)
   get URL, headers: authenticated_header(user)
   puts response.body
    end
   end

    describe "aunauthorised user" do
    it 'should return unauth for retrieve current user info before 
    login' do
    get URL
    expect(response).to have_http_status(:unauthorized)
    end
   end
  end

any suggestions are welcome. i need to run test as when user sucessfully created is show success message I expect it generates token while sucessfully creating user any suggestions are welcome. i need to run test as when user sucessfully created is show success message

"I expect it generates token while sucessfully creating user"

Aucun commentaire:

Enregistrer un commentaire