diff --git a/app/models/views/datatables/datatables_report.rb b/app/models/views/datatables/datatables_report.rb index 82c3120ef..dd153789b 100644 --- a/app/models/views/datatables/datatables_report.rb +++ b/app/models/views/datatables/datatables_report.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + module Views module Datatables class DatatablesReport < ApplicationRecord @@ -14,9 +16,7 @@ module Views permitted_by_team = get_permitted_by_team_tokenized permitted_by_project = get_permitted_by_project_tokenized if user.is_admin_of_team? team - allowed_ids = for_admin( - user, permitted_by_team, permitted_by_project - ) + allowed_ids = for_admin(user, permitted_by_team) else allowed_ids = for_non_admin( user, permitted_by_team, permitted_by_project @@ -48,12 +48,10 @@ module Views .first end - def for_admin(user, permitted_by_team, permitted_by_project) + def for_admin(user, permitted_by_team) allowed_ids = [] permitted_by_team.each do |item| next unless user.id.in? item.users_ids - by_project = get_by_project_item(permitted_by_project, item) - next unless user.id.in? by_project.users_ids allowed_ids << item.report_id end allowed_ids diff --git a/spec/factories/reports.rb b/spec/factories/reports.rb index fc913d816..8cc82f942 100644 --- a/spec/factories/reports.rb +++ b/spec/factories/reports.rb @@ -2,6 +2,7 @@ FactoryBot.define do factory :report do user { User.first || association(:user) } project { Project.first || association(:project) } + team { Team.first || association(:team) } name Faker::Name.unique.name end end diff --git a/spec/models/views/datatables/datatables_report_spec.rb b/spec/models/views/datatables/datatables_report_spec.rb index 534ca3770..d1b9e3990 100644 --- a/spec/models/views/datatables/datatables_report_spec.rb +++ b/spec/models/views/datatables/datatables_report_spec.rb @@ -1,5 +1,80 @@ require 'rails_helper' -RSpec.describe DatatablesReport, type: :model do - pending "add some examples to (or delete) #{__FILE__}" +RSpec.describe Views::Datatables::DatatablesReport, type: :model do + describe 'Database table' do + it { should have_db_column :id } + it { should have_db_column :name } + it { should have_db_column :project_name } + it { should have_db_column :created_by } + it { should have_db_column :last_modified_by } + it { should have_db_column :created_at } + it { should have_db_column :updated_at } + it { should have_db_column :project_archived } + it { should have_db_column :project_visibility } + it { should have_db_column :project_id } + it { should have_db_column :team_id } + it { should have_db_column :users_with_team_read_permissions } + it { should have_db_column :users_with_project_read_permissions } + end + + describe 'is readonly' do + let(:team) { create :team } + it do + expect { + Views::Datatables::DatatablesReport.create!(team: team) + }.to raise_error( + ActiveRecord::ReadOnlyRecord, + 'Views::Datatables::DatatablesReport is marked as readonly' + ) + end + end + + describe '#tokenize/1' do + it 'returns an array of permission items' do + items = [[1, [1, 2]], [2, [3, 4]]] + tokenized = described_class.send('tokenize', items) + expect(tokenized.first).to have_attributes(report_id: 1, + users_ids: [1, 2]) + expect(tokenized.last).to have_attributes(report_id: 2, users_ids: [3, 4]) + end + end + + describe '#for_admin/3' do + let!(:user) { create :user } + let!(:user_two) { create :user, email: 'user.two@asdf.com' } + let!(:team) { create :team } + let!(:project) do + create :project, created_by: user, last_modified_by: user, team: team + end + let!(:user_project) { create :user_project, project: project, user: user } + let!(:team_two) { create :team } + let!(:user_team) do + create :user_team, team: team_two, user: user_two, role: 0 + end + let!(:user_team) { create :user_team, team: team, user: user, role: 2 } + + let!(:project_two) do + create :project, created_by: user_two, + last_modified_by: user_two, + team: team_two + end + let!(:report_one) do + create :report, user: user, + team: team, + name: 'report one', + project: project + end + let!(:report_two) do + create :report, user: user, + team: team_two, + project: project_two, + name: 'report two' + end + + it 'returns the reports ' do + reports = team.datatables_reports.visible_by(user, team) + expect(reports.length).to eq 1 + expect(reports.first.id).to eq report_one.id + end + end end