require 'rails_helper' module Mks module Auth RSpec.describe UserRolesController, type: :controller do routes { Mks::Auth::Engine.routes } before(:each) do u = create(:user) token = Mks::Auth::TokenAuth.issue(name: u.full_name, email: u.email, id: u.id) request.headers['Authorization'] = "Bearer #{token}" end describe 'GET #index' do it 'returns only roles of the application module' do app_module = create(:application_module, code: 'DUMMY') 3.times { create(:user_role, application_module: app_module) } create(:user_role) get :index result = JSON(response.body) expect(result['data'].count).to eq 3 end end describe 'GET #users' do it 'fetches users of a role' do r1 = create(:user_role) r2 = create(:user_role) u1 = create(:user, roles: [r1]) u2 = create(:user, roles: [r1, r2]) u3 = create(:user, roles: [r1, r2]) create(:user, roles: [r2]) get :users, params: { id: r1.id } result = JSON(response.body) data = result['data'] users = [JSON(u1.to_json), JSON(u2.to_json), JSON(u3.to_json)] expect(data.count).to eq 3 expect((users - data).empty?).to be_truthy end end describe 'GET #assigned_roles' do it 'gets all roles with assigned ones as selected for a user' do app_module = create(:application_module, code: 'DUMMY') ur1 = create(:user_role, application_module: app_module) ur2 = create(:user_role, application_module: app_module) create(:user_role, application_module: app_module) 3.times { create(:user_role) } user = create(:user, roles: [ur1, ur2]) get :assigned_roles, params: { id: user.id } result = JSON(response.body) expect(result['data'].count).to eq 3 selected = result['data'].select { |d| (d['selected'])} unselected = result['data'].reject { |d| (d['selected'])} expect(selected.count).to eq 2 expect(unselected.count).to eq 1 end end describe 'POST #assign_roles' do it 'assigns roles to a user' do app_module = create(:application_module, code: 'DUMMY') ur1 = create(:user_role, application_module: app_module) ur2 = create(:user_role, application_module: app_module) create(:user_role, application_module: app_module) u = create(:user) u.application_modules = [app_module] payload = [ { id: ur1.id, selected: true }, { id: ur2.id, selected: true } ] post :assign_roles, params: { id: u.id, roles: payload }, format: :json result = JSON(response.body) expect(result['success']).to be_truthy u.reload expect(u.roles.count).to eq 2 end it 'assigns only selected roles' do app_module = create(:application_module, code: 'DUMMY') ur1 = create(:user_role, application_module: app_module) ur2 = create(:user_role, application_module: app_module) ur3 = create(:user_role, application_module: app_module) u = create(:user, roles: [ur1, ur2], application_modules: [app_module]) payload = [ { id: ur1.id, selected: true }, { id: ur3.id, selected: true } ] post :assign_roles, params: { id: u.id, roles: payload }, format: :json result = JSON(response.body) expect(result['success']).to be_truthy u.reload expect(u.roles.count).to eq 2 expect(u.roles).to include ur1 expect(u.roles).to include ur3 end it 'removes roles if unselected' do app_module = create(:application_module, code: 'DUMMY') ur1 = create(:user_role, application_module: app_module) ur2 = create(:user_role, application_module: app_module) u = create(:user, roles: [ur1, ur2], application_modules: [app_module]) post :assign_roles, params: { id: u.id, roles: [] }, format: :json result = JSON(response.body) expect(result['success']).to be_truthy u.reload expect(u.roles.count).to eq 0 end end end end end