diff --git a/app/controllers/admin/app_configs_controller.rb b/app/controllers/admin/app_configs_controller.rb index 91b18b85b..ef7546cb9 100644 --- a/app/controllers/admin/app_configs_controller.rb +++ b/app/controllers/admin/app_configs_controller.rb @@ -1,6 +1,4 @@ class Admin::AppConfigsController < AdminController - def index; end - def edit; end def update diff --git a/app/controllers/admin/transcripts_controller.rb b/app/controllers/admin/transcripts_controller.rb index 34fd596f6..58ed33adf 100644 --- a/app/controllers/admin/transcripts_controller.rb +++ b/app/controllers/admin/transcripts_controller.rb @@ -1,5 +1,6 @@ class Admin::TranscriptsController < ApplicationController include ActionController::MimeResponds + include IndexTemplate before_action :authenticate_admin! diff --git a/app/controllers/admin/users_controller.rb b/app/controllers/admin/users_controller.rb index cc6209a30..0121351ff 100644 --- a/app/controllers/admin/users_controller.rb +++ b/app/controllers/admin/users_controller.rb @@ -21,7 +21,9 @@ def update def destroy authorize User - unless @user.delete + if @user.delete + head :no_content + else render json: @user.errors, status: :unprocessable_entity end end diff --git a/app/controllers/collections_controller.rb b/app/controllers/collections_controller.rb index 44ae186a8..a274412ad 100644 --- a/app/controllers/collections_controller.rb +++ b/app/controllers/collections_controller.rb @@ -21,32 +21,35 @@ def list # GET /collections/the-uid.json def show; end + # NOTE (jonjon): Based on routes.rb line 35 + # these actions below is not being used anymore + # POST /collections.json - def create - @collection = Collection.new(collection_params) + # def create + # @collection = Collection.new(collection_params) - if @collection.save - render json: @collection, status: :created, location: @collection - else - render json: @collection.errors, status: :unprocessable_entity - end - end + # if @collection.save + # render json: @collection, status: :created, location: @collection + # else + # render json: @collection.errors, status: :unprocessable_entity + # end + # end # PATCH/PUT /collections/the-uid.json - def update - if @collection.update(collection_params) - head :no_content - else - render json: @collection.errors, status: :unprocessable_entity - end - end + # def update + # if @collection.update(collection_params) + # head :no_content + # else + # render json: @collection.errors, status: :unprocessable_entity + # end + # end # DELETE /collections//the-uid.json - def destroy - @collection.destroy + # def destroy + # @collection.destroy - head :no_content - end + # head :no_content + # end private diff --git a/app/controllers/transcript_edits_controller.rb b/app/controllers/transcript_edits_controller.rb index e360002cb..088dad148 100644 --- a/app/controllers/transcript_edits_controller.rb +++ b/app/controllers/transcript_edits_controller.rb @@ -83,22 +83,24 @@ def create end # PATCH/PUT /transcript_edits/1.json - def update - @transcript_edit = TranscriptEdit.find(params[:id]) + # Note (jonjon): As per routes, this action is not included + # def update + # @transcript_edit = TranscriptEdit.find(params[:id]) - if @transcript_edit.update(transcript_edit_params) - head :no_content - else - render json: @transcript_edit.errors, status: :unprocessable_entity - end - end + # if @transcript_edit.update(transcript_edit_params) + # head :no_content + # else + # render json: @transcript_edit.errors, status: :unprocessable_entity + # end + # end # DELETE /transcript_edits/1.json - def destroy - @transcript_edit.destroy + # Note (jonjon): As per routes, this action is not included + # def destroy + # @transcript_edit.destroy - head :no_content - end + # head :no_content + # end private diff --git a/app/controllers/transcript_speaker_edits_controller.rb b/app/controllers/transcript_speaker_edits_controller.rb index ecc32ec2b..06532cf92 100644 --- a/app/controllers/transcript_speaker_edits_controller.rb +++ b/app/controllers/transcript_speaker_edits_controller.rb @@ -3,20 +3,20 @@ class TranscriptSpeakerEditsController < ApplicationController before_action :authenticate_user, only: [:create] before_action :set_transcript_speaker_edit, only: [:show, :update, :destroy] + # Note (jonjon): As per the routes, only :create action is included + # # GET /transcript_speaker_edits.json + # def index + # @transcript_speaker_edits = [] - # GET /transcript_speaker_edits.json - def index - @transcript_speaker_edits = [] + # render json: @transcript_speaker_edits + # end - render json: @transcript_speaker_edits - end - - # GET /transcript_speaker_edits/1.json - def show - @transcript_speaker_edit = nil + # # GET /transcript_speaker_edits/1.json + # def show + # @transcript_speaker_edit = nil - render json: @transcript_speaker_edit - end + # render json: @transcript_speaker_edit + # end # POST /transcript_speaker_edits.json def create @@ -65,22 +65,22 @@ def create end # PATCH/PUT /transcript_speaker_edits/1.json - def update - @transcript_speaker_edit = TranscriptSpeakerEdit.find(params[:id]) - - if @transcript_speaker_edit.update(transcript_speaker_edit_params) - head :no_content - else - render json: @transcript_speaker_edit.errors, status: :unprocessable_entity - end - end - - # DELETE /transcript_speaker_edits/1.json - def destroy - @transcript_speaker_edit.destroy - - head :no_content - end + # def update + # @transcript_speaker_edit = TranscriptSpeakerEdit.find(params[:id]) + + # if @transcript_speaker_edit.update(transcript_speaker_edit_params) + # head :no_content + # else + # render json: @transcript_speaker_edit.errors, status: :unprocessable_entity + # end + # end + + # # DELETE /transcript_speaker_edits/1.json + # def destroy + # @transcript_speaker_edit.destroy + + # head :no_content + # end private diff --git a/spec/controllers/admin/app_configs_controller_spec.rb b/spec/controllers/admin/app_configs_controller_spec.rb new file mode 100644 index 000000000..b7213658a --- /dev/null +++ b/spec/controllers/admin/app_configs_controller_spec.rb @@ -0,0 +1,52 @@ +require 'rails_helper' + +RSpec.describe Admin::AppConfigsController, type: :controller do + render_views + + let(:user) { create(:user, :admin, email: "user@email.com", password: "password") } + let!(:config) { AppConfig.instance } + + before { sign_in user } + + describe '#edit' do + it 'can view edit page' do + get :edit, params: { id: config.id } + + page = response.body + + expect(page).to have_content('Show theme') + expect(page).to have_content('Show institutions') + expect(page).to have_content('Main title') + expect(page).to have_content('Intro title') + expect(page).to have_content('Intro text') + expect(page).to have_content('Hero image') + expect(page).to include('Save') + end + end + + describe '#update' do + it 'updates app config' do + params = { + id: config.id, + app_config: { + show_theme: true, + show_institutions: true, + main_title: 'Any title possible', + image: nil, + intro_title: 'Any intro title', + intro_text: 'Any intro text' + } + } + + put :update, params: params + expect(response.body).to have_content('redirected') + + config.reload + expect(config.show_theme).to be_truthy + expect(config.show_institutions).to be_truthy + expect(config.main_title).to eq 'Any title possible' + expect(config.intro_title).to eq 'Any intro title' + expect(config.intro_text).to eq 'Any intro text' + end + end +end diff --git a/spec/controllers/admin/collections_controller_spec.rb b/spec/controllers/admin/collections_controller_spec.rb new file mode 100644 index 000000000..43585f8ac --- /dev/null +++ b/spec/controllers/admin/collections_controller_spec.rb @@ -0,0 +1,41 @@ +require 'rails_helper' + +RSpec.describe CollectionsController, type: :controller do + render_views + + let(:admin) { create(:user, :admin) } + let(:collection) { create(:collection) } + let!(:institution) { collection.institution } + + before { sign_in admin } + + let(:page) { response.body } + + describe "#index" do + it "visits index page" do + create(:page, page_type: "collection") + + get :index + + expect(response).to have_http_status(:success) + end + end + + describe "#list" do + it "visit list page" do + post :list, params: { institution_slug: institution.slug }, format: :js + + expect(response).to have_http_status(:success) + end + end + + describe "#show" do + it "visit show page" do + collection = create(:collection) + + get :show, params: { id: collection.uid }, format: :json + + expect(response).to have_http_status(:success) + end + end +end diff --git a/spec/controllers/admin/pages_controller_spec.rb b/spec/controllers/admin/pages_controller_spec.rb new file mode 100644 index 000000000..9f4fcf317 --- /dev/null +++ b/spec/controllers/admin/pages_controller_spec.rb @@ -0,0 +1,104 @@ +require 'rails_helper' +include Rack::Test::Methods + +RSpec.describe Admin::PagesController, type: :controller do + render_views + + let(:user) { create(:user, :admin, email: "user@email.com", password: "password") } + let!(:page) { create(:page) } + + before { sign_in user } + + describe '#index' do + it 'list pages' do + get :index + + expect(response.body).to include("Pages") + expect(response.body).to include("faq") + end + end + + describe '#create' do + it 'create page' do + params = { + page: { + content: 'Any content', + page_type: 'about', + published: true, + admin_access: true + } + } + + expect { post(:create, params: params ) }.to( + change { Page.count } + ) + end + end + + describe '#edit' do + it 'can view edit page' do + get :edit, params: { id: page.id } + + expect(response.body).to include 'Editing Page' + expect(response.body).to include page.page_type + expect(response.body).to include page.content + end + end + + describe '#show' do + it 'can preview page' do + get :show, params: { id: page.id } + + expect(response.body).to include page.page_type + expect(response.body).to include page.content + end + end + + describe '#update' do + it 'update page' do + params = { + id: page.id, + page: { + content: 'Any content', + page_type: 'faqs', + published: true, + admin_access: true + } + } + + put(:update, params: params ) + + page.reload + + expect(page.content).to eq 'Any content' + expect(page.page_type).to eq 'faqs' + end + end + + describe '#destroy' do + it 'deletes a page' do + expect { delete :destroy, params: { id: page.id } }.to( + change { Page.count }.by(-1) + ) + end + end + + describe '#upload' do + it 'create page' do + params = { + id: page.id, + page: { + image: Rack::Test::UploadedFile.new("spec/fixtures/image.jpg", "image/jpg") + } + } + + expect { post :upload, params: params }.to( + change { CmsImageUpload.count }.by(1) + ) + + response_json = JSON.parse(response.body) + expect(response_json['url']).to be_present + expect(response_json['upload_id']).to be_present + end + end +end diff --git a/spec/controllers/admin/site_alerts_controller_spec.rb b/spec/controllers/admin/site_alerts_controller_spec.rb new file mode 100644 index 000000000..f357f270e --- /dev/null +++ b/spec/controllers/admin/site_alerts_controller_spec.rb @@ -0,0 +1,105 @@ +require 'rails_helper' + +RSpec.describe Admin::SiteAlertsController, type: :controller do + render_views + + let(:user) { create(:user, :admin) } + let!(:site_alert) do + create( + :site_alert, + message: 'any message', + machine_name: 'any-machine-name' + ) + end + let(:page) { response.body } + + before { sign_in user } + + describe '#index' do + it 'lists site alert' do + get :index + + expect(page).to have_content('Site Alerts') + expect(page).to have_content('any message') + end + end + + describe '#new' do + it 'renders new page' do + get :new + + expect(page).to have_content('New Page') + expect(page).to have_content('Level') + expect(page).to have_content('Machine name') + expect(page).to have_content('Message') + expect(page).to have_content('Publish version to live site') + end + end + + describe '#create' do + let(:params) do + { + site_alert: { + level: 'warning', + machine_name: 'any-dummy-machine-name', + message: 'any dummy site alert message', + published: true + } + } + end + + it 'create a new site alert' do + expect { post :create, params: params }.to( + change { SiteAlert.count }.by(1) + ) + end + end + + describe '#edit' do + it 'renders edit page' do + get :edit, params: { id: site_alert.id } + + expect(page).to have_content('Editing Page') + expect(page).to have_content('Level') + expect(page).to have_content('Machine name') + expect(page).to include('any-machine-name') + expect(page).to have_content('Message') + expect(page).to include('any message') + expect(page).to have_content('Publish version to live site') + end + end + + describe '#update' do + let(:params) do + { + id: site_alert.id, + site_alert: { + level: 'warning', + machine_name: 'any-dummy-machine-name', + message: 'any dummy site alert message', + published: true + } + } + end + + it 'updates site alert' do + put :update, params: params + + site_alert.reload + + expect(site_alert.level).to eq 'warning' + expect(site_alert.machine_name).to eq 'any-dummy-machine-name' + expect(site_alert.message).to eq 'any dummy site alert message' + expect(site_alert.published).to be_truthy + end + end + + describe '#destroy' do + it 'destroys site alert' do + params = { id: site_alert.id } + expect { delete :destroy, params: params }.to( + change { SiteAlert.count }.by(-1) + ) + end + end +end diff --git a/spec/controllers/admin/stats_controller_spec.rb b/spec/controllers/admin/stats_controller_spec.rb new file mode 100644 index 000000000..3d101bf9a --- /dev/null +++ b/spec/controllers/admin/stats_controller_spec.rb @@ -0,0 +1,46 @@ +require 'rails_helper' + +RSpec.describe Admin::StatsController, type: :controller do + render_views + + let(:user) { create(:user, :admin) } + let!(:flag) do + institution = create(:institution) + collection = create(:collection, institution: institution) + transcript = create(:transcript, collection: collection) + flag = create(:flag, transcript_id: transcript.id) + + flag.decorate + end + let(:page) { response.body } + + before { sign_in user } + + describe '#index' do + before { get :index } + + it 'renders Unresolved Flags' do + expect(page).to have_content('Unresolved Flags') + expect(page).to have_content(flag.institution&.name) + expect(page).to have_content(flag.flag_type.label ) + expect(page).to have_content(flag.text) + transcript_line = flag.transcript_line + transcript = transcript_line.transcript.decorate + expect(page).to have_content(transcript.title) + end + + it 'renders Institution edits section' do + expect(page).to have_content('Institution edits') + transcript_line = flag.transcript_line + expect(page).to include(transcript_line.transcript.collection.institution.name) + end + + it 'renders Site-wide edits section' do + expect(page).to have_content('Site-wide edits') + end + + it 'renders Site-wide registrations section' do + expect(page).to have_content('Site-wide registrations') + end + end +end diff --git a/spec/controllers/admin/themes_controller_spec.rb b/spec/controllers/admin/themes_controller_spec.rb new file mode 100644 index 000000000..fbb6abadf --- /dev/null +++ b/spec/controllers/admin/themes_controller_spec.rb @@ -0,0 +1,57 @@ +require 'rails_helper' + +RSpec.describe Admin::ThemesController, type: :controller do + render_views + + let(:user) { create(:user, :admin) } + + before { sign_in user } + + let!(:theme) { create(:theme) } + let(:page) { response.body } + + describe '#index' do + it "can visit index page" do + get :index + + expect(page).to have_content("Themes") + expect(page).to have_content(theme.name) + end + end + + describe '#edit' do + it "can visit index page" do + get :edit, params: { id: theme.id } + + expect(page).to have_content("Editing Theme") + expect(page).to include("name") + expect(page).to include(theme.name) + end + end + + describe '#create' do + it "creates theme" do + expect { post :create, params: { theme: { name: "Any theme name" } } }.to( + change { Theme.count }.by(1) + ) + end + end + + describe '#create' do + it "updates theme" do + put :update, params: { id: theme.id, theme: { name: "Any theme name" } } + + theme.reload + + expect(theme.name).to eq "Any theme name" + end + end + + describe '#destroy' do + it "destroys theme" do + expect { delete :destroy, params: { id: theme.id } }.to( + change { Theme.count }.by(-1) + ) + end + end +end diff --git a/spec/controllers/admin/transcription_conventions_controller_spec.rb b/spec/controllers/admin/transcription_conventions_controller_spec.rb new file mode 100644 index 000000000..38ce0db39 --- /dev/null +++ b/spec/controllers/admin/transcription_conventions_controller_spec.rb @@ -0,0 +1,96 @@ +require 'rails_helper' + +RSpec.describe Admin::TranscriptionConventionsController, type: :controller do + render_views + + let(:user) { create(:user, :admin) } + + before { sign_in user } + + let(:transcription_convention) { create(:transcription_convention) } + let!(:institution) { transcription_convention.institution } + let(:page) { response.body } + + describe '#index' do + it "can visit index page" do + get :index, params: { institution_id: institution.id } + + expect(page).to have_content("Transcription Conventions for #{institution.name}") + expect(page).to have_content(transcription_convention.convention_key) + expect(page).to have_content(transcription_convention.convention_text) + expect(page).to have_content(transcription_convention.example) + end + end + + describe '#new' do + it "can visit new page" do + get :new, params: { institution_id: institution.id } + + expect(page).to have_content("New record") + expect(page).to include("transcription_convention[convention_key]") + expect(page).to include("transcription_convention[convention_text]") + expect(page).to include("transcription_convention[example]") + end + end + + describe "#edit" do + it "can visit edit page" do + get :edit, params: { institution_id: institution.id, id: transcription_convention.id } + + expect(page).to have_content("Editing #{transcription_convention.convention_key}") + expect(page).to include("transcription_convention[convention_key]") + expect(page).to include("transcription_convention[convention_text]") + expect(page).to include("transcription_convention[example]") + expect(page).to include(transcription_convention.convention_key) + expect(page).to include(transcription_convention.convention_text) + expect(page).to include(transcription_convention.example) + end + end + + describe "#create" do + it "creates transcription convention" do + params = { + institution_id: institution.id, + transcription_convention: { + convention_key: "Any convention key", + convention_text: "Any convention text", + example: "Any convention example" + } + } + + expect { post :create, params: params }.to( + change { TranscriptionConvention.count }.by(1) + ) + end + end + + describe "#update" do + it "updates transcription convention" do + params = { + institution_id: institution.id, + id: transcription_convention.id, + transcription_convention: { + convention_key: "Any convention key", + convention_text: "Any convention text", + example: "Any convention example" + } + } + + put :update, params: params + + transcription_convention.reload + + expect(transcription_convention.convention_key).to eq "Any convention key" + expect(transcription_convention.convention_text).to eq "Any convention text" + expect(transcription_convention.example).to eq "Any convention example" + end + end + + describe "#destroy" do + it "destroy transcriptiopn convention" do + expect { delete :destroy, params: { institution_id: institution.id, id: transcription_convention.id } }.to( + change { TranscriptionConvention.count }.by(-1) + ) + end + end +end diff --git a/spec/controllers/admin/transcripts_controller_spec.rb b/spec/controllers/admin/transcripts_controller_spec.rb new file mode 100644 index 000000000..e26f394e5 --- /dev/null +++ b/spec/controllers/admin/transcripts_controller_spec.rb @@ -0,0 +1,30 @@ +require 'rails_helper' + +RSpec.describe Admin::TranscriptsController, type: :controller do + render_views + + let(:user) { create(:user, :admin) } + + before { sign_in user } + + let(:page) { response.body } + + describe "#index" do + context "json" do + it "visits index page" do + request.accept = "application/json" + get :index + + expect(JSON.parse(page).keys).to contain_exactly("entries") + end + end + + context "html" do + it "visits index page" do + get :index + + expect(response).to have_http_status(:success) + end + end + end +end diff --git a/spec/controllers/admin/users_controller_spec.rb b/spec/controllers/admin/users_controller_spec.rb new file mode 100644 index 000000000..dadcd62d3 --- /dev/null +++ b/spec/controllers/admin/users_controller_spec.rb @@ -0,0 +1,56 @@ +require 'rails_helper' + +RSpec.describe Admin::UsersController, type: :controller do + render_views + + let(:admin) { create(:user, :admin) } + let!(:user) { create(:user) } + let(:institution) { create(:institution) } + let(:user_role) { create(:user_role, :content_editor) } + + before { sign_in admin } + + let(:page) { response.body } + + describe "#index" do + it "visits index page" do + get :index + + expect(page).to have_content("Registered Users") + expect(page).to have_content("Administrative Users") + + expect(page).to have_content(user.name) + expect(page).to have_content(user.email) + expect(page).to have_content(admin.name) + expect(page).to have_content(admin.email) + end + end + + describe "#update" do + it "updates user" do + params = { + id: user.id, + user: { + user_role_id: user_role.id, + institution_id: institution.id + } + } + + put :update, params: params + + user.reload + + expect(user.user_role).to eq user_role + expect(user.institution).to eq institution + end + end + + describe "#destroy" do + it "destroys user" do + request.accept = "application/json" + expect { delete :destroy, params: { id: user.id } }.to( + change { User.count }.by(-1) + ) + end + end +end diff --git a/spec/controllers/concerns/searchable_spec.rb b/spec/controllers/concerns/searchable_spec.rb new file mode 100644 index 000000000..ce6413a08 --- /dev/null +++ b/spec/controllers/concerns/searchable_spec.rb @@ -0,0 +1,165 @@ +require 'rails_helper' + +RSpec.describe Searchable do + let(:controller) { FakesController.new } + let(:params) { {} } + + before do + class FakesController < ApplicationController + include Searchable + end + + allow(controller).to receive(:params).and_return( + ActionController::Parameters.new(params) + ) + end + + after do + Object.send :remove_const, :FakesController + end + + describe '#sort_params' do + subject(:sort_params) { controller.send(:sort_params) } + + context 'turn collection id as array' do + let(:params) do + { + data: { + collection_id: '1' + } + } + end + + it 'collection id as array' do + expect(sort_params.dig('collection_id')).to eq (['1']) + end + end + + context 'require params' do + let(:params) do + { + data: { + sort_id: 'asc', + text: 'any-text', + q: 'any-query', + institution_id: 'any institution id', + theme: ['theme_1'], + collection_id: ['1'], + unrequired_params: 1 + } + } + end + + it 'returns required params' do + expect(sort_params.keys).to contain_exactly( + "sort_id", "text", "q", "institution_id", "theme", "collection_id" + ) + end + end + end + + describe '#build_params' do + subject(:build_params) { controller.send(:build_params) } + + let(:params) do + { + data: { + sort_id: 'asc', + text: 'any-text', + q: 'any-query', + institution_id: 'any institution id', + theme: ['theme_1'], + collection_id: ['1'], + unrequired_params: 1 + } + } + end + + context 'when all params is present' do + it 'returns all params' do + expect(build_params.keys).to contain_exactly( + "sort_id", "text", "q", "institution_id", "theme", "collection_id" + ) + end + end + + context 'when collection_id value is 0' do + let(:params) do + { + data: { + sort_id: 'asc', + text: 'any-text', + q: 'any-query', + institution_id: 'any institution id', + theme: ['theme_1'], + collection_id: ['0'], + unrequired_params: 1 + } + } + end + + it 'returns all params except collection id' do + expect(build_params.keys).to contain_exactly( + "sort_id", "text", "q", "institution_id", "theme" + ) + end + end + end + + describe '#load_institutions' do + subject(:load_institutions) { controller.send(:load_institutions) } + + context 'when collection id is present' do + let(:collection) { create(:collection) } + let(:params) do + { + data: { + collection_id: [collection.id] + } + } + end + + it 'it returns institution' do + expect(load_institutions).to include(collection.institution) + end + end + + context 'when collection id is not present' do + it 'returns all institution' do + institution_a = create(:institution) + institution_b = create(:institution) + + expect(load_institutions).to include(institution_a, institution_b) + end + end + end + + describe '#load_collection' do + subject(:load_collection) { controller.send(:load_collection) } + + let(:collection) { create(:collection) } + + context 'when institution params is present' do + let(:params) do + { + data: { + institution_id: collection.institution.id + } + } + end + + it 'returns collection' do + expect(load_collection).to contain_exactly(collection) + end + end + + context 'when institution params is not present' do + it 'returns all collection' do + collection_a = create(:collection) + collection_b = create(:collection) + + expect(load_collection).to contain_exactly(collection_a, collection_b) + end + end + end +end diff --git a/spec/controllers/flags_controller_spec.rb b/spec/controllers/flags_controller_spec.rb new file mode 100644 index 000000000..49d45da5a --- /dev/null +++ b/spec/controllers/flags_controller_spec.rb @@ -0,0 +1,51 @@ +require 'rails_helper' + +RSpec.describe FlagsController, type: :controller do + render_views + + let(:admin) { create(:user, :admin) } + let!(:flag) { create(:flag) } + let(:page) { response.body } + let(:response_json) { JSON.parse(page) } + + before { sign_in admin } + + describe '#index' do + it 'visits index page' do + request.accept = "application/json" + get :index, params: { transcript_line_id: flag.transcript_line_id } + + expect(response_json['flags']).not_to be_empty + end + end + + describe '#show' do + it 'visits show page' do + request.accept = "application/json" + get :show, params: { id: flag.id } + + expect(response_json).to eq flag.as_json.except('is_deleted', 'is_resolved') + end + end + + describe '#create' do + it 'creates flag' do + transcript_line = create(:transcript_line) + params = { + flag: { + transcript_id: transcript_line.transcript_id, + transcript_line_id: transcript_line.id, + flag_type_id: flag.flag_type_id, + user_id: admin.id, + text: 'any text', + is_resolved: 0 + } + } + + request.accept = "application/json" + expect { post :create, params: params }.to( + change { Flag.count }.by(1) + ) + end + end +end diff --git a/spec/controllers/search_controller_spec.rb b/spec/controllers/search_controller_spec.rb new file mode 100644 index 000000000..97b565616 --- /dev/null +++ b/spec/controllers/search_controller_spec.rb @@ -0,0 +1,17 @@ +require 'rails_helper' + +RSpec.describe SearchController, type: :controller do + render_views + + let(:admin) { create(:user, :admin) } + + before { sign_in admin } + + describe '#index' do + it 'visits index page' do + get :index + + expect(response).to have_http_status(:success) + end + end +end diff --git a/spec/controllers/transcript_edit_controller_spec.rb b/spec/controllers/transcript_edit_controller_spec.rb new file mode 100644 index 000000000..96da58d33 --- /dev/null +++ b/spec/controllers/transcript_edit_controller_spec.rb @@ -0,0 +1,100 @@ +require 'rails_helper' + +RSpec.describe TranscriptEditsController, type: :controller do + render_views + + let(:admin) { create(:user, :admin) } + let(:page) { response.body } + let(:response_json) { JSON.parse(page) } + let!(:transcript_edit) { create(:transcript_edit) } + + before { sign_in admin } + + describe '#index' do + before do + request.accept = "application/json" + get :index, params: params + end + + context 'with transcript_line_id params' do + let(:params) { { transcript_line_id: transcript_edit.transcript_line.id } } + + it 'respond with json' do + expect(response_json['edits']).not_to be_empty + end + end + + context 'when user_id params' do + let(:params) { { user_id: transcript_edit.user_id } } + + it 'respond with json' do + expect(response_json['edits']).not_to be_empty + end + end + end + + describe '#show' do + it 'respond with json' do + request.accept = "application/json" + get :show, params: { id: transcript_edit.id } + + expect(response_json).to eq transcript_edit.as_json.except('is_deleted') + end + end + + describe '#create' do + it 'creates transcript edit' do + allow_any_instance_of(TranscriptLine).to receive(:recalculate) + + transcript_line = create(:transcript_line) + params = { + transcript_edit: { + transcript_id: transcript_line.transcript.id, + transcript_line_id: transcript_line.id, + user_id: admin.id, + text: 'any text', + is_deleted: 0 + } + } + + request.accept = "application/json" + expect { post :create, params: params }.to( + change { TranscriptEdit.count }.by(1) + ) + end + end + + # Note (jonjon): uncomment this when this actions added to routes + xdescribe '#update' do + it 'updates transcript edit' do + allow_any_instance_of(TranscriptLine).to receive(:recalculate) + + params = { + id: transcript_edit.id, + transcript_edit: { + transcript_id: transcript_edit.transcript_line.transcript.id, + transcript_line_id: transcript_edit.transcript_line.id, + user_id: transcript_edit.user_id, + text: 'any text', + is_deleted: 0 + } + } + + request.accept = "application/json" + put :update, params: params + + transcript_edit.reload + + expect(transcript_edit.text).to eq 'any text' + end + end + + # Note (jonjon): uncomment this when this actions added to routes + xdescribe '#destroy' do + it 'destroy transcript edit' do + expect { delete :destroy, params: { id: transcript_edit.id } }.to( + change { TranscriptEdit.count }.by(-1) + ) + end + end +end diff --git a/spec/controllers/transcript_files_spec.rb b/spec/controllers/transcript_files_spec.rb new file mode 100644 index 000000000..06f274545 --- /dev/null +++ b/spec/controllers/transcript_files_spec.rb @@ -0,0 +1,69 @@ +require 'rails_helper' + +RSpec.describe TranscriptFilesController, type: :controller do + render_views + + let(:admin) { create(:user, :admin) } + let(:transcript_edit) { create(:transcript_edit) } + let(:transcript) { transcript_edit.transcript } + let!(:transcript_speaker) { create(:transcript_speaker, transcript: transcript) } + let(:json_response) { JSON.parse(response.body) } + + before { sign_in admin } + + describe '#index' do + it 'visits index page' do + request.accept = "application/json" + get :index, params: { updated_after: Date.current.strftime('%Y-%m-%d') } + + expect(response).to have_http_status(:success) + end + end + + describe '#show' do + before do + request.accept = "application/json" + get :show, params: params + end + + context 'with edits' do + let(:params) { { id: transcript.uid, edit: 1 } } + + it 'responds with json' do + expect(json_response.keys).to contain_exactly( + "id", + "url", + "last_updated", + "title", + "description", + "audio_url", + "image_url", + "duration", + "lines", + "speakers", + "statuses" + ) + end + end + + context 'with speaker' do + let(:params) { { id: transcript.uid, speaker: 1 } } + + it 'responds with json' do + expect(json_response.keys).to contain_exactly( + "id", + "url", + "last_updated", + "title", + "description", + "audio_url", + "image_url", + "duration", + "lines", + "speakers", + "statuses" + ) + end + end + end +end diff --git a/spec/controllers/transcript_lines_controller_spec.rb b/spec/controllers/transcript_lines_controller_spec.rb new file mode 100644 index 000000000..4bdc4247a --- /dev/null +++ b/spec/controllers/transcript_lines_controller_spec.rb @@ -0,0 +1,19 @@ +require 'rails_helper' + +RSpec.describe TranscriptLinesController, type: :controller do + render_views + + let(:admin) { create(:user, :admin) } + let(:flag) { create(:flag) } + let!(:transcript_line) { flag.transcript_line } + + before { sign_in admin } + + describe '#resolve' do + it 'visits index page' do + post :resolve, params: { id: transcript_line.id } + + expect(response).to have_http_status(:success) + end + end +end diff --git a/spec/controllers/transcript_speaker_edits_controller_spec.rb b/spec/controllers/transcript_speaker_edits_controller_spec.rb new file mode 100644 index 000000000..06753a490 --- /dev/null +++ b/spec/controllers/transcript_speaker_edits_controller_spec.rb @@ -0,0 +1,28 @@ +require 'rails_helper' + +RSpec.describe TranscriptSpeakerEditsController, type: :controller do + render_views + + let(:admin) { create(:user, :admin) } + let(:flag) { create(:flag) } + let(:transcript_line) { flag.transcript_line } + let!(:transcript_speaker) { create(:transcript_speaker, transcript: transcript_line.transcript) } + + before { sign_in admin } + + describe '#create' do + it 'creates new TranscriptSpeakerEdit' do + request.accept = "application/json" + expect do + post :create, params: { + transcript_speaker_edit: { + transcript_id: transcript_line.transcript_id, + transcript_line_id: transcript_line.id, + user_id: admin.id, + speaker_id: transcript_speaker.speaker_id + } + } + end.to change { TranscriptSpeakerEdit.count }.by(1) + end + end +end diff --git a/spec/decorators/transcript_line_decorator_spec.rb b/spec/decorators/transcript_line_decorator_spec.rb new file mode 100644 index 000000000..83a1596ee --- /dev/null +++ b/spec/decorators/transcript_line_decorator_spec.rb @@ -0,0 +1,29 @@ +require 'rails_helper' + +RSpec.describe TranscriptLineDecorator do + let(:decorator) { described_class.new(transcript_line) } + let(:transcript_line) { create(:transcript_line) } + + describe '#search_title' do + subject(:search_title) { decorator.search_title } + + let(:expectation) do + "#{transcript_line.transcript.decorate.collection_title}"\ + " - #{transcript_line.transcript.title}" + end + + it { is_expected.to eq expectation } + end + + describe '#image_url' do + subject(:image_url) { decorator.image_url } + + it { is_expected.to eq transcript_line.transcript.image_url } + end + + describe '#humanize_duration' do + subject(:humanize_duration) { decorator.humanize_duration(12345) } + + it { is_expected.to eq '(3h 25m 45s)' } + end +end diff --git a/spec/factories/collection.rb b/spec/factories/collections.rb similarity index 100% rename from spec/factories/collection.rb rename to spec/factories/collections.rb diff --git a/spec/factories/flag_type.rb b/spec/factories/flag_types.rb similarity index 100% rename from spec/factories/flag_type.rb rename to spec/factories/flag_types.rb diff --git a/spec/factories/flag.rb b/spec/factories/flags.rb similarity index 100% rename from spec/factories/flag.rb rename to spec/factories/flags.rb diff --git a/spec/factories/institution.rb b/spec/factories/institutions.rb similarity index 100% rename from spec/factories/institution.rb rename to spec/factories/institutions.rb diff --git a/spec/factories/page.rb b/spec/factories/pages.rb similarity index 100% rename from spec/factories/page.rb rename to spec/factories/pages.rb diff --git a/spec/factories/site_alerts.rb b/spec/factories/site_alerts.rb index 41ad2542e..5935121da 100644 --- a/spec/factories/site_alerts.rb +++ b/spec/factories/site_alerts.rb @@ -1,9 +1,4 @@ FactoryBot.define do factory :site_alert do - id { "MyString" } - message { "MyText" } - user_id { 1 } - publish_at { "2023-01-16 12:31:05" } - unpublish_at { "2023-01-16 12:31:05" } end end diff --git a/spec/factories/transcript_speaker.rb b/spec/factories/transcript_speaker.rb index ee8da03d2..aa862f472 100644 --- a/spec/factories/transcript_speaker.rb +++ b/spec/factories/transcript_speaker.rb @@ -2,6 +2,5 @@ factory :transcript_speaker do transcript speaker - collection end end diff --git a/spec/factories/transcript.rb b/spec/factories/transcripts.rb similarity index 100% rename from spec/factories/transcript.rb rename to spec/factories/transcripts.rb diff --git a/spec/jobs/delete_institution_job_spec.rb b/spec/jobs/delete_institution_job_spec.rb new file mode 100644 index 000000000..e70d52ef8 --- /dev/null +++ b/spec/jobs/delete_institution_job_spec.rb @@ -0,0 +1,26 @@ +require 'rails_helper' + +RSpec.describe DeleteInstitutionJob do + let!(:institution) { create(:institution) } + let(:user) { create(:user) } + + describe "#perform_later" do + it "enqueues job" do + ActiveJob::Base.queue_adapter = :test + + expect { + described_class.perform_later(institution.name, institution.id, user.email) + }.to have_enqueued_job + end + + it 'destroy institution and execute mailer' do + mailer = double(deliver_now: true) + allow(InstitutionMailer).to receive(:delete_institution).and_return(mailer) + + expect(InstitutionMailer).to receive(:delete_institution).and_return(mailer) + expect { + described_class.perform_now(institution.name, institution.id, user.email) + }.to change { Institution.count }.by(-1) + end + end +end diff --git a/spec/jobs/recalculate_transcripts_job_spec.rb b/spec/jobs/recalculate_transcripts_job_spec.rb new file mode 100644 index 000000000..5fc0a248d --- /dev/null +++ b/spec/jobs/recalculate_transcripts_job_spec.rb @@ -0,0 +1,11 @@ +require 'rails_helper' + +RSpec.describe RecalculateTranscriptsJob do + describe "#perform_later" do + it "enqueues job" do + ActiveJob::Base.queue_adapter = :test + + expect { described_class.perform_later }.to have_enqueued_job + end + end +end diff --git a/spec/jobs/voice_base_upload_job_spec.rb b/spec/jobs/voice_base_upload_job_spec.rb new file mode 100644 index 000000000..d661f2a97 --- /dev/null +++ b/spec/jobs/voice_base_upload_job_spec.rb @@ -0,0 +1,21 @@ +require 'rails_helper' + +RSpec.describe VoiceBaseUploadJob do + let!(:transcript) { create(:transcript) } + let(:user) { create(:user) } + + describe "#perform_later" do + it "enqueues job" do + ActiveJob::Base.queue_adapter = :test + + expect { described_class.perform_later(transcript) }.to have_enqueued_job + end + + it 'execute VoiceBase API' do + allow(VoiceBase::VoicebaseApiService).to receive(:upload_media).with(transcript.id) + + expect(VoiceBase::VoicebaseApiService).to receive(:upload_media).with(transcript.id) + described_class.perform_now(transcript.id) + end + end +end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 8017576f9..162bbb9c5 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,7 +1,32 @@ require 'simplecov' require 'pundit/matchers' -SimpleCov.start +SimpleCov.start do + add_filter 'app/channels/application_cable/channel.rb' + add_filter 'app/channels/application_cable/connection.rb' + add_filter 'app/controllers/users/passwords_controller.rb' + add_filter 'app/models/application_record.rb' + add_filter 'app/uploaders/cms_image_uploader.rb' + add_filter 'app/channels/application_cable/channel.rb' + add_filter 'app/channels/application_cable/connection.rb' + add_filter 'app/mailers/application_mailer.rb' + add_filter 'app/models/cms_image_upload.rb' + add_filter 'app/controllers/v2_controller.rb' + add_filter 'app/controllers/amplify_base_controller.rb' + # This controller is unused + # not sure if we will be needed this in the future + add_filter 'app/controllers/admin/reports_controller.rb' + add_filter 'app/controllers/default_controller.rb' + add_filter 'app/controllers/institutions_controller.rb' + # This is all handled by devise + add_filter 'app/controllers/users/omniauth_callbacks_controller.rb' + add_filter 'app/controllers/users/registrations_controller.rb' + add_filter 'app/controllers/users/sessions_controller.rb' + # nothing to test here + add_filter 'app/decorators/admin/institution_decorator.rb' + add_filter 'app/decorators/institution_decorator.rb' + add_filter 'app/jobs/daily_analytics_job.rb' +end SimpleCov.start 'rails' # This file was generated by the `rails generate rspec:install` command. Conventionally, all diff --git a/spec/support/capybara.rb b/spec/support/capybara.rb index ed4f2c16b..42252e59b 100644 --- a/spec/support/capybara.rb +++ b/spec/support/capybara.rb @@ -1,6 +1,8 @@ require 'capybara/rails' require 'capybara/rspec' +Webdrivers::Chromedriver.required_version = '114.0.5735.90' + Capybara.register_driver :chrome do |app| Capybara::Selenium::Driver.new app, browser: :chrome,