2019-04-29 20:27:42 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2019-01-23 09:40:21 -05:00
|
|
|
require 'rails_helper'
|
|
|
|
require 'theme_store/tgz_exporter'
|
|
|
|
|
|
|
|
describe ThemeStore::TgzExporter do
|
2019-05-06 23:12:20 -04:00
|
|
|
let!(:theme) do
|
2019-01-23 09:40:21 -05:00
|
|
|
Fabricate(:theme, name: "Header Icons").tap do |theme|
|
|
|
|
theme.set_field(target: :common, name: :body_tag, value: "<b>testtheme1</b>")
|
|
|
|
theme.set_field(target: :settings, name: :yaml, value: "somesetting: test")
|
|
|
|
theme.set_field(target: :mobile, name: :scss, value: 'body {background-color: $background_color; font-size: $font-size}')
|
|
|
|
theme.set_field(target: :translations, name: :en, value: { en: { key: "value" } }.deep_stringify_keys.to_yaml)
|
|
|
|
image = file_from_fixtures("logo.png")
|
2019-02-27 04:45:22 -05:00
|
|
|
upload = UploadCreator.new(image, "logo.png").create_for(Discourse::SYSTEM_USER_ID)
|
2019-01-23 09:40:21 -05:00
|
|
|
theme.set_field(target: :common, name: :logo, upload_id: upload.id, type: :theme_upload_var)
|
2019-02-27 04:45:22 -05:00
|
|
|
image = file_from_fixtures("logo.png")
|
|
|
|
other_upload = UploadCreator.new(image, "logo.png").create_for(Discourse::SYSTEM_USER_ID)
|
|
|
|
theme.set_field(target: :common, name: "other_logo", upload_id: upload.id, type: :theme_upload_var)
|
2019-01-25 09:19:01 -05:00
|
|
|
theme.build_remote_theme(remote_url: "", about_url: "abouturl", license_url: "licenseurl",
|
|
|
|
authors: "David Taylor", theme_version: "1.0", minimum_discourse_version: "1.0.0",
|
|
|
|
maximum_discourse_version: "3.0.0.beta1")
|
2019-01-23 09:40:21 -05:00
|
|
|
|
|
|
|
cs1 = Fabricate(:color_scheme, name: 'Orphan Color Scheme', color_scheme_colors: [
|
|
|
|
Fabricate(:color_scheme_color, name: 'header_primary', hex: 'F0F0F0'),
|
|
|
|
Fabricate(:color_scheme_color, name: 'header_background', hex: '1E1E1E'),
|
|
|
|
Fabricate(:color_scheme_color, name: 'tertiary', hex: '858585')
|
|
|
|
])
|
|
|
|
|
|
|
|
cs2 = Fabricate(:color_scheme, name: 'Theme Color Scheme', color_scheme_colors: [
|
|
|
|
Fabricate(:color_scheme_color, name: 'header_primary', hex: 'F0F0F0'),
|
|
|
|
Fabricate(:color_scheme_color, name: 'header_background', hex: '1E1E1E'),
|
|
|
|
Fabricate(:color_scheme_color, name: 'tertiary', hex: '858585')
|
|
|
|
])
|
|
|
|
|
|
|
|
theme.color_scheme = cs1
|
|
|
|
cs2.update(theme_id: theme.id)
|
|
|
|
|
|
|
|
theme.save!
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
let(:dir) do
|
|
|
|
tmpdir = Dir.tmpdir
|
|
|
|
dir = "#{tmpdir}/#{SecureRandom.hex}"
|
|
|
|
FileUtils.mkdir(dir)
|
|
|
|
dir
|
|
|
|
end
|
|
|
|
|
|
|
|
after do
|
|
|
|
FileUtils.rm_rf(dir)
|
|
|
|
end
|
|
|
|
|
|
|
|
let(:package) do
|
|
|
|
exporter = ThemeStore::TgzExporter.new(theme)
|
|
|
|
filename = exporter.package_filename
|
|
|
|
FileUtils.cp(filename, dir)
|
|
|
|
exporter.cleanup!
|
2019-07-18 08:34:48 -04:00
|
|
|
"#{dir}/discourse-header-icons.zip"
|
2019-01-23 09:40:21 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it "exports the theme correctly" do
|
|
|
|
package
|
2019-07-18 08:34:48 -04:00
|
|
|
file = 'discourse-header-icons.zip'
|
|
|
|
dest = 'discourse-header-icons'
|
|
|
|
Dir.chdir(dir) do
|
2019-07-23 06:57:06 -04:00
|
|
|
ImportExport::ZipUtils.new.unzip_directory(dir, file, allow_non_root_folder: true)
|
2019-07-18 08:34:48 -04:00
|
|
|
`rm #{file}`
|
|
|
|
|
2019-01-23 09:40:21 -05:00
|
|
|
folders = Dir.glob("**/*").reject { |f| File.file?(f) }
|
|
|
|
expect(folders).to contain_exactly("assets", "common", "locales", "mobile")
|
|
|
|
|
|
|
|
files = Dir.glob("**/*").reject { |f| File.directory?(f) }
|
2019-02-27 04:45:22 -05:00
|
|
|
expect(files).to contain_exactly("about.json", "assets/logo.png", "assets/other_logo.png", "common/body_tag.html", "locales/en.yml", "mobile/mobile.scss", "settings.yml")
|
2019-01-23 09:40:21 -05:00
|
|
|
|
|
|
|
expect(JSON.parse(File.read('about.json')).deep_symbolize_keys).to eq(
|
|
|
|
"name": "Header Icons",
|
|
|
|
"about_url": "abouturl",
|
|
|
|
"license_url": "licenseurl",
|
|
|
|
"component": false,
|
|
|
|
"assets": {
|
2019-02-27 04:45:22 -05:00
|
|
|
"logo": "assets/logo.png",
|
|
|
|
"other_logo": "assets/other_logo.png"
|
2019-01-23 09:40:21 -05:00
|
|
|
},
|
2019-01-25 09:19:01 -05:00
|
|
|
"authors": "David Taylor",
|
|
|
|
"minimum_discourse_version": "1.0.0",
|
|
|
|
"maximum_discourse_version": "3.0.0.beta1",
|
|
|
|
"theme_version": "1.0",
|
2019-01-23 09:40:21 -05:00
|
|
|
"color_schemes": {
|
|
|
|
"Orphan Color Scheme": {
|
|
|
|
"header_primary": "F0F0F0",
|
|
|
|
"header_background": "1E1E1E",
|
|
|
|
"tertiary": "858585"
|
|
|
|
},
|
|
|
|
"Theme Color Scheme": {
|
|
|
|
"header_primary": "F0F0F0",
|
|
|
|
"header_background": "1E1E1E",
|
|
|
|
"tertiary": "858585"
|
|
|
|
}
|
2019-01-28 06:34:11 -05:00
|
|
|
},
|
2019-01-28 11:00:33 -05:00
|
|
|
"learn_more": "https://meta.discourse.org/t/beginners-guide-to-using-discourse-themes/91966"
|
2019-01-23 09:40:21 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
expect(File.read("common/body_tag.html")).to eq("<b>testtheme1</b>")
|
|
|
|
expect(File.read("mobile/mobile.scss")).to eq("body {background-color: $background_color; font-size: $font-size}")
|
|
|
|
expect(File.read("settings.yml")).to eq("somesetting: test")
|
|
|
|
expect(File.read("locales/en.yml")).to eq({ en: { key: "value" } }.deep_stringify_keys.to_yaml)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it "has safeguards to prevent writing outside the temp directory" do
|
|
|
|
# Theme field names should be sanitized before writing to the database,
|
|
|
|
# but protection is in place 'just in case'
|
|
|
|
expect do
|
2019-05-15 17:43:00 -04:00
|
|
|
theme.set_field(target: :translations, name: SiteSetting.default_locale, value: "hacked")
|
2019-04-12 06:36:08 -04:00
|
|
|
ThemeField.any_instance.stubs(:file_path).returns("../../malicious")
|
2019-01-23 09:40:21 -05:00
|
|
|
theme.save!
|
|
|
|
package
|
|
|
|
end.to raise_error(RuntimeError)
|
|
|
|
end
|
|
|
|
|
2019-01-28 06:29:29 -05:00
|
|
|
it "doesn't prepend 'discourse' to filename if already there" do
|
|
|
|
theme.update!(name: "Discourse Header Icons")
|
|
|
|
exporter = ThemeStore::TgzExporter.new(theme)
|
|
|
|
filename = exporter.package_filename
|
|
|
|
exporter.cleanup!
|
2019-07-18 08:34:48 -04:00
|
|
|
expect(filename).to end_with "/discourse-header-icons.zip"
|
2019-01-28 06:29:29 -05:00
|
|
|
end
|
|
|
|
|
2019-01-23 09:40:21 -05:00
|
|
|
end
|