2019-03-05 17:51:08 Ruby

Ruby

Copy Copied! Full
# frozen_string_literal: true require 'rails_helper' RSpec.describe 'Sale::V1::Ec::ItemImages', type: :request do let!(:ec_item) { FactoryBot.create(:ec_item) } # 200 shared_examples 'POST#create request success' do it 'return ok response' do expect(response).to have_http_status(:ok) json = JSON.parse(response.body, symbolize_names: true) expect(json).to a_hash_including(ec_item_image_matcher) end end # 200 成功するパラメータの時のテストパターン(全ユーザ) shared_examples 'when request try all user, return success' do context 'with not signed in user' do it_behaves_like 'unauthorized response' end context 'with signed in none scope', :signed_in do it_behaves_like 'unpermitted response' end context 'with signed in viewer user', :viewer_signed_in do it_behaves_like 'unpermitted response' end context 'with signed in viewer user', :editor_signed_in do it_behaves_like 'POST#create request success' end context 'with signed in viewer user', :admin_signed_in do it_behaves_like 'POST#create request success' end end # 失敗(403)するユーザの時のテストパターン(アクセス権限がないユーザ) shared_examples 'when request tried by invalid users, return forbidden' do context 'with signed in none scope', :signed_in do it_behaves_like 'unpermitted response' end context 'with signed in viewer user', :viewer_signed_in do it_behaves_like 'unpermitted response' end end # 異常系(404)のテスト shared_examples 'when request fails by not_found' do it 'returns not_found response' do expect(response).to have_http_status(:not_found) json = JSON.parse(response.body, symbolize_names: true) expect(json).to match( type: 'generic', title: 'エラー', full_messages: ['対象データが取得出来ませんでした。'], messages: {} ) end end # 422 shared_examples 'when request fails by unprocessable_entity' do it 'returns unprocessable_entity response' do expect(response).to have_http_status(:unprocessable_entity) json = JSON.parse(response.body, symbolize_names: true) expect(json).to match( type: 'validation', title: '入力内容にエラーがあります', messages: { file: ['は20枚までしか登録できません。'] }, full_messages: ['Fileは20枚までしか登録できません。'] ) end end # 権限を与える(422エラー) shared_examples 'when request tried by authorized users, return unprocessable_entity' do context 'with signed in editor user', :editor_signed_in do it_behaves_like 'when request fails by unprocessable_entity' end context 'with signed in admin user', :admin_signed_in do it_behaves_like 'when request fails by unprocessable_entity' end end # 失敗(404)するパラメータの時のテストパターン(権限があるユーザ) shared_examples 'when request tried by invalid users, return not_found' do context 'with signed in editor user', :editor_signed_in do it_behaves_like 'when request fails by not_found' end context 'with signed in admin user', :admin_signed_in do it_behaves_like 'when request fails by not_found' end end # テスト実施 describe 'POST#create' do include_context 'with master data' # 200 context 'when correct request paramater' do before do file_jpeg = fixture_file_upload( Rails.root.join('spec', 'fixtures', 'sample_image_14.JPEG'), 'image/jpg' ) request_with_token( :post, sale_v1_ec_item_images_create_path(ec_item.id), { files: [ file: file_jpeg, order: 1 ] }, 'Content-Type' => 'multipart/form-data' ) end it_behaves_like 'when request try all user, return success' end # 403 context 'with not signed in user' do before do file_jpeg = fixture_file_upload( Rails.root.join('spec', 'fixtures', 'sample_image_14.JPEG'), 'image/jpg' ) request_with_token( :post, sale_v1_ec_item_images_create_path(ec_item.id), { files: [ file: file_jpeg, order: 1 ] }, 'Content-Type' => 'multipart/form-data' ) end it_behaves_like 'when request tried by invalid users, return forbidden' end # 404 与えられたec_item_idが存在しない場合 context 'when ec_item_id is not_found' do before do file_jpeg = fixture_file_upload( Rails.root.join('spec', 'fixtures', 'sample_image_14.JPEG'), 'image/jpg' ) request_with_token( :post, sale_v1_ec_item_images_create_path(ec_item.id + 1), { files: [ file: file_jpeg, order: 1 ] }, 'Content-Type' => 'multipart/form-data' ) end it_behaves_like 'when request tried by invalid users, return not_found' end # 422 画像が21枚だった場合 context 'when 422' do before do file_jpeg = fixture_file_upload( Rails.root.join('spec', 'fixtures', 'sample_image_14.JPEG'), 'image/jpg' ) FactoryBot.create_list(:ec_item_image, 20, ec_item: ec_item) request_with_token( :post, sale_v1_ec_item_images_create_path(ec_item.id), { files: [ file: file_jpeg, order: 1 ] }, 'Content-Type' => 'multipart/form-data' ) end it_behaves_like 'when request tried by authorized users, return unprocessable_entity' end end end