2019-04-19 21:24:54 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-08-30 00:49:07 +08:00
|
|
|
require 'rails_helper'
|
|
|
|
|
|
|
|
describe TinyMceAsset, type: :model do
|
|
|
|
it 'should be of class TinyMceAsset' do
|
|
|
|
expect(subject.class).to eq TinyMceAsset
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'Database table' do
|
|
|
|
it { should have_db_column :estimated_size }
|
2019-04-19 21:24:54 +08:00
|
|
|
it { should have_db_column :object_id }
|
|
|
|
it { should have_db_column :object_type }
|
2017-08-30 00:49:07 +08:00
|
|
|
it { should have_db_column :team_id }
|
|
|
|
it { should have_db_column :created_at }
|
|
|
|
it { should have_db_column :updated_at }
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'Relations' do
|
2019-07-26 18:40:36 +08:00
|
|
|
it { should belong_to(:team).optional }
|
|
|
|
it { should belong_to(:object).optional }
|
2017-08-30 00:49:07 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
describe 'Should be a valid object' do
|
|
|
|
it { should validate_presence_of :estimated_size }
|
|
|
|
end
|
2019-04-19 21:24:54 +08:00
|
|
|
|
|
|
|
describe 'Methods' do
|
2019-08-13 17:04:19 +08:00
|
|
|
let(:user) { create :user }
|
2019-07-08 17:09:24 +08:00
|
|
|
let(:team) { create :team }
|
|
|
|
let(:my_module) { create :my_module }
|
|
|
|
let(:result) do
|
|
|
|
create :result, name: 'test result', my_module: my_module
|
|
|
|
end
|
|
|
|
|
2019-04-19 21:24:54 +08:00
|
|
|
let(:result_text) do
|
2019-07-08 17:09:24 +08:00
|
|
|
create :result_text, text: '<img data-mce-token=1 src=""/>', result: result
|
2019-04-19 21:24:54 +08:00
|
|
|
end
|
2019-07-08 17:09:24 +08:00
|
|
|
let(:image) { create :tiny_mce_asset, id: 1, team_id: team.id }
|
2019-04-19 21:24:54 +08:00
|
|
|
|
|
|
|
describe '#update_images' do
|
|
|
|
it 'save new image' do
|
2019-07-08 17:09:24 +08:00
|
|
|
create :protocol, my_module: result_text.result.my_module
|
|
|
|
result_text.result.my_module.protocol.update(team_id: image.team_id)
|
2019-08-13 17:04:19 +08:00
|
|
|
TinyMceAsset.update_images(result_text, [Base62.encode(image.id)].to_s, user)
|
2019-07-08 17:09:24 +08:00
|
|
|
updated_image = TinyMceAsset.find(image.id)
|
2019-04-19 21:24:54 +08:00
|
|
|
expect(updated_image.object_type).to eq 'ResultText'
|
2019-07-08 17:09:24 +08:00
|
|
|
expect(ResultText.find(result_text.id).text).not_to include 'fake-path'
|
2019-04-19 21:24:54 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#generate_url' do
|
|
|
|
it 'create new url' do
|
2019-07-26 18:40:36 +08:00
|
|
|
image.update(object: result_text)
|
|
|
|
expect(TinyMceAsset.generate_url(result_text.text, result_text)).to include 'test.jpg'
|
2019-04-19 21:24:54 +08:00
|
|
|
end
|
2019-07-08 17:09:24 +08:00
|
|
|
|
|
|
|
it 'restrict acces to image' do
|
2019-04-19 21:24:54 +08:00
|
|
|
image
|
2019-08-13 17:04:19 +08:00
|
|
|
expect(TinyMceAsset.generate_url(result_text.text, result_text)).not_to include 'test.jpg'
|
2019-04-19 21:24:54 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-04-23 21:16:40 +08:00
|
|
|
describe '#reassign_tiny_mce_image_references' do
|
|
|
|
it 'change image token in rich text field' do
|
2019-04-19 21:24:54 +08:00
|
|
|
new_result_text = result_text
|
2019-08-13 17:04:19 +08:00
|
|
|
TinyMceAsset.update_images(new_result_text, [Base62.encode(image.id)].to_s, user)
|
2019-04-19 21:24:54 +08:00
|
|
|
create :tiny_mce_asset, id: 2, object: new_result_text
|
2019-04-23 21:16:40 +08:00
|
|
|
new_result_text.reassign_tiny_mce_image_references([[1, 2]])
|
2019-04-19 21:24:54 +08:00
|
|
|
expect(ResultText.find(new_result_text.id).text).to include 'data-mce-token="2"'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2017-08-30 00:49:07 +08:00
|
|
|
end
|