2019-04-30 08:27:42 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2022-07-28 10:27:38 +08:00
|
|
|
RSpec.describe Admin::ThemesController do
|
2023-11-10 06:47:59 +08:00
|
|
|
fab!(:admin)
|
|
|
|
fab!(:moderator)
|
|
|
|
fab!(:user)
|
2018-06-11 12:54:16 +08:00
|
|
|
|
2022-11-02 00:33:17 +08:00
|
|
|
let! :repo do
|
|
|
|
setup_git_repo("about.json" => { name: "discourse-branch-header" }.to_json)
|
|
|
|
end
|
|
|
|
|
|
|
|
let! :repo_url do
|
|
|
|
MockGitImporter.register("https://github.com/discourse/discourse-brand-header.git", repo)
|
|
|
|
end
|
|
|
|
|
|
|
|
around(:each) { |group| MockGitImporter.with_mock { group.run } }
|
|
|
|
|
2018-06-11 12:54:16 +08:00
|
|
|
describe "#generate_key_pair" do
|
2022-11-03 11:42:44 +08:00
|
|
|
context "when logged in as an admin" do
|
|
|
|
before { sign_in(admin) }
|
|
|
|
|
|
|
|
it "can generate key pairs" do
|
|
|
|
post "/admin/themes/generate_key_pair.json"
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
json = response.parsed_body
|
|
|
|
expect(json["private_key"]).to eq(nil)
|
|
|
|
expect(json["public_key"]).to include("ssh-rsa ")
|
|
|
|
expect(Discourse.redis.get("ssh_key_#{json["public_key"]}")).not_to eq(nil)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
shared_examples "key pair generation not allowed" do
|
|
|
|
it "prevents key pair generation with a 404 response" do
|
|
|
|
post "/admin/themes/generate_key_pair.json"
|
|
|
|
|
|
|
|
expect(response.status).to eq(404)
|
|
|
|
expect(response.parsed_body["errors"]).to include(I18n.t("not_found"))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when logged in as a moderator" do
|
|
|
|
before { sign_in(moderator) }
|
|
|
|
|
|
|
|
include_examples "key pair generation not allowed"
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when logged in as a non-staff user" do
|
|
|
|
before { sign_in(user) }
|
|
|
|
|
|
|
|
include_examples "key pair generation not allowed"
|
2018-06-11 12:54:16 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "#upload_asset" do
|
2020-05-19 07:09:36 +08:00
|
|
|
let(:file) { file_from_fixtures("fake.woff2", "woff2") }
|
|
|
|
let(:filename) { File.basename(file) }
|
|
|
|
let(:upload) { Rack::Test::UploadedFile.new(file) }
|
2018-06-11 12:54:16 +08:00
|
|
|
|
2022-11-03 11:42:44 +08:00
|
|
|
context "when logged in as an admin" do
|
|
|
|
before { sign_in(admin) }
|
2018-06-11 12:54:16 +08:00
|
|
|
|
2022-11-03 11:42:44 +08:00
|
|
|
it "can create a theme upload" do
|
|
|
|
post "/admin/themes/upload_asset.json", params: { file: upload }
|
|
|
|
expect(response.status).to eq(201)
|
2018-06-11 12:54:16 +08:00
|
|
|
|
2022-11-03 11:42:44 +08:00
|
|
|
upload = Upload.find_by(original_filename: filename)
|
2019-11-28 05:32:17 +08:00
|
|
|
|
2022-11-03 11:42:44 +08:00
|
|
|
expect(upload.id).not_to be_nil
|
|
|
|
expect(response.parsed_body["upload_id"]).to eq(upload.id)
|
|
|
|
end
|
2019-11-28 05:32:17 +08:00
|
|
|
|
2022-11-03 11:42:44 +08:00
|
|
|
context "when trying to upload an existing file" do
|
|
|
|
let(:uploaded_file) { Upload.find_by(original_filename: filename) }
|
|
|
|
let(:response_json) { response.parsed_body }
|
|
|
|
|
|
|
|
it "reuses the original upload" do
|
2023-12-04 20:45:19 +08:00
|
|
|
post "/admin/themes/upload_asset.json", params: { file: upload }
|
2022-11-03 11:42:44 +08:00
|
|
|
expect(response.status).to eq(201)
|
|
|
|
expect(response_json["upload_id"]).to eq(uploaded_file.id)
|
|
|
|
end
|
2019-11-28 05:32:17 +08:00
|
|
|
end
|
2022-11-03 11:42:44 +08:00
|
|
|
end
|
2019-11-28 05:32:17 +08:00
|
|
|
|
2022-11-03 11:42:44 +08:00
|
|
|
shared_examples "theme asset upload not allowed" do
|
|
|
|
it "prevents theme asset upload with a 404 response" do
|
|
|
|
expect do
|
|
|
|
post "/admin/themes/upload_asset.json", params: { file: upload }
|
|
|
|
end.not_to change { Upload.count }
|
|
|
|
|
|
|
|
expect(response.status).to eq(404)
|
|
|
|
expect(response.parsed_body["errors"]).to include(I18n.t("not_found"))
|
2019-11-28 05:32:17 +08:00
|
|
|
end
|
|
|
|
end
|
2022-11-03 11:42:44 +08:00
|
|
|
|
|
|
|
context "when logged in as a moderator" do
|
|
|
|
before { sign_in(moderator) }
|
|
|
|
|
|
|
|
include_examples "theme asset upload not allowed"
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when logged in as a non-staff user" do
|
|
|
|
before { sign_in(user) }
|
|
|
|
|
|
|
|
include_examples "theme asset upload not allowed"
|
|
|
|
end
|
2018-06-11 12:54:16 +08:00
|
|
|
end
|
|
|
|
|
2019-01-23 22:40:21 +08:00
|
|
|
describe "#export" do
|
2022-11-03 11:42:44 +08:00
|
|
|
context "when logged in as an admin" do
|
|
|
|
before { sign_in(admin) }
|
|
|
|
|
|
|
|
it "exports correctly" do
|
|
|
|
theme = Fabricate(:theme, name: "Awesome Theme")
|
|
|
|
theme.set_field(target: :common, name: :scss, value: ".body{color: black;}")
|
|
|
|
theme.set_field(target: :desktop, name: :after_header, value: "<b>test</b>")
|
|
|
|
theme.set_field(
|
|
|
|
target: :extra_js,
|
|
|
|
name: "discourse/controller/blah",
|
|
|
|
value: 'console.log("test");',
|
|
|
|
)
|
|
|
|
theme.save!
|
|
|
|
|
|
|
|
get "/admin/customize/themes/#{theme.id}/export"
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
|
|
|
|
# Save the output in a temp file (automatically cleaned up)
|
|
|
|
file = Tempfile.new("archive.zip")
|
|
|
|
file.write(response.body)
|
|
|
|
file.rewind
|
|
|
|
uploaded_file = Rack::Test::UploadedFile.new(file.path, "application/zip")
|
|
|
|
|
|
|
|
# Now import it again
|
|
|
|
expect do
|
|
|
|
post "/admin/themes/import.json", params: { theme: uploaded_file }
|
|
|
|
expect(response.status).to eq(201)
|
2023-12-07 06:25:00 +08:00
|
|
|
end.to change { Theme.count }.by(1)
|
2018-06-11 12:54:16 +08:00
|
|
|
|
2022-11-03 11:42:44 +08:00
|
|
|
json = response.parsed_body
|
2018-06-11 12:54:16 +08:00
|
|
|
|
2022-11-03 11:42:44 +08:00
|
|
|
expect(json["theme"]["name"]).to eq("Awesome Theme")
|
|
|
|
expect(json["theme"]["theme_fields"].length).to eq(3)
|
|
|
|
end
|
|
|
|
end
|
2018-06-11 12:54:16 +08:00
|
|
|
|
2022-11-03 11:42:44 +08:00
|
|
|
shared_examples "theme export not allowed" do
|
|
|
|
it "prevents theme export with a 404 response" do
|
|
|
|
theme = Fabricate(:theme, name: "Awesome Theme")
|
|
|
|
|
|
|
|
get "/admin/customize/themes/#{theme.id}/export"
|
|
|
|
|
|
|
|
expect(response.status).to eq(404)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when logged in as a moderator" do
|
|
|
|
before { sign_in(moderator) }
|
|
|
|
|
|
|
|
include_examples "theme export not allowed"
|
|
|
|
end
|
2018-06-11 12:54:16 +08:00
|
|
|
|
2022-11-03 11:42:44 +08:00
|
|
|
context "when logged in as a non-staff user" do
|
|
|
|
before { sign_in(user) }
|
2018-06-11 12:54:16 +08:00
|
|
|
|
2022-11-03 11:42:44 +08:00
|
|
|
include_examples "theme export not allowed"
|
2019-01-23 22:40:21 +08:00
|
|
|
end
|
|
|
|
end
|
2018-06-11 12:54:16 +08:00
|
|
|
|
2019-01-23 22:40:21 +08:00
|
|
|
describe "#import" do
|
|
|
|
let(:theme_json_file) do
|
|
|
|
Rack::Test::UploadedFile.new(
|
|
|
|
file_from_fixtures("sam-s-simple-theme.dcstyle.json", "json"),
|
|
|
|
"application/json",
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
let(:theme_archive) do
|
2019-10-09 22:41:16 +08:00
|
|
|
Rack::Test::UploadedFile.new(
|
|
|
|
file_from_fixtures("discourse-test-theme.zip", "themes"),
|
|
|
|
"application/zip",
|
|
|
|
)
|
2019-01-23 22:40:21 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
let(:image) { file_from_fixtures("logo.png") }
|
2018-06-11 12:54:16 +08:00
|
|
|
|
2022-11-03 11:42:44 +08:00
|
|
|
context "when logged in as an admin" do
|
|
|
|
before { sign_in(admin) }
|
|
|
|
|
|
|
|
context "when theme allowlist mode is enabled" do
|
|
|
|
before do
|
|
|
|
global_setting :allowed_theme_repos,
|
|
|
|
"https://github.com/discourse/discourse-brand-header.git"
|
|
|
|
end
|
|
|
|
|
|
|
|
it "allows allowlisted imports" do
|
|
|
|
expect(Theme.allowed_remote_theme_ids.length).to eq(0)
|
|
|
|
|
|
|
|
post "/admin/themes/import.json",
|
|
|
|
params: {
|
|
|
|
remote: " https://github.com/discourse/discourse-brand-header.git ",
|
|
|
|
}
|
|
|
|
|
|
|
|
expect(Theme.allowed_remote_theme_ids.length).to eq(1)
|
|
|
|
expect(response.status).to eq(201)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "prevents adding disallowed themes" do
|
|
|
|
RemoteTheme.stubs(:import_theme)
|
|
|
|
remote = " https://bad.com/discourse/discourse-brand-header.git "
|
|
|
|
|
|
|
|
post "/admin/themes/import.json", params: { remote: remote }
|
2020-06-03 11:19:42 +08:00
|
|
|
|
2022-11-03 11:42:44 +08:00
|
|
|
expect(response.status).to eq(403)
|
|
|
|
expect(response.parsed_body["errors"]).to include(
|
|
|
|
I18n.t("themes.import_error.not_allowed_theme", { repo: remote.strip }),
|
|
|
|
)
|
|
|
|
end
|
2021-10-29 23:46:52 +08:00
|
|
|
|
2022-11-03 11:42:44 +08:00
|
|
|
it "bans json file import" do
|
|
|
|
post "/admin/themes/import.json", params: { theme: theme_json_file }
|
|
|
|
expect(response.status).to eq(403)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it "can import a theme from Git" do
|
|
|
|
RemoteTheme.stubs(:import_theme)
|
2020-06-03 11:19:42 +08:00
|
|
|
post "/admin/themes/import.json",
|
|
|
|
params: {
|
2021-10-29 23:46:52 +08:00
|
|
|
remote: " https://github.com/discourse/discourse-brand-header.git ",
|
2020-06-03 11:19:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
expect(response.status).to eq(201)
|
|
|
|
end
|
|
|
|
|
FEATURE: Theme settings migrations (#24071)
This commit introduces a new feature that allows theme developers to manage the transformation of theme settings over time. Similar to Rails migrations, the theme settings migration system enables developers to write and execute migrations for theme settings, ensuring a smooth transition when changes are required in the format or structure of setting values.
Example use cases for the theme settings migration system:
1. Renaming a theme setting.
2. Changing the data type of a theme setting (e.g., transforming a string setting containing comma-separated values into a proper list setting).
3. Altering the format of data stored in a theme setting.
All of these use cases and more are now possible while preserving theme setting values for sites that have already modified their theme settings.
Usage:
1. Create a top-level directory called `migrations` in your theme/component, and then within the `migrations` directory create another directory called `settings`.
2. Inside the `migrations/settings` directory, create a JavaScript file using the format `XXXX-some-name.js`, where `XXXX` is a unique 4-digit number, and `some-name` is a descriptor of your choice that describes the migration.
3. Within the JavaScript file, define and export (as the default) a function called `migrate`. This function will receive a `Map` object and must also return a `Map` object (it's acceptable to return the same `Map` object that the function received).
4. The `Map` object received by the `migrate` function will include settings that have been overridden or changed by site administrators. Settings that have never been changed from the default will not be included.
5. The keys and values contained in the `Map` object that the `migrate` function returns will replace all the currently changed settings of the theme.
6. Migrations are executed in numerical order based on the XXXX segment in the migration filenames. For instance, `0001-some-migration.js` will be executed before `0002-another-migration.js`.
Here's a complete example migration script that renames a setting from `setting_with_old_name` to `setting_with_new_name`:
```js
// File name: 0001-rename-setting.js
export default function migrate(settings) {
if (settings.has("setting_with_old_name")) {
settings.set("setting_with_new_name", settings.get("setting_with_old_name"));
}
return settings;
}
```
Internal topic: t/109980
2023-11-02 13:10:15 +08:00
|
|
|
it "responds with suitable error message when a migration fails" do
|
|
|
|
repo_path =
|
|
|
|
setup_git_repo(
|
|
|
|
"about.json" => { name: "test theme" }.to_json,
|
|
|
|
"settings.yaml" => "boolean_setting: true",
|
|
|
|
"migrations/settings/0001-some-migration.js" => <<~JS,
|
|
|
|
export default function migrate(settings) {
|
|
|
|
settings.set("unknown_setting", "dsad");
|
|
|
|
return settings;
|
|
|
|
}
|
|
|
|
JS
|
|
|
|
)
|
2024-01-11 14:04:02 +08:00
|
|
|
|
FEATURE: Theme settings migrations (#24071)
This commit introduces a new feature that allows theme developers to manage the transformation of theme settings over time. Similar to Rails migrations, the theme settings migration system enables developers to write and execute migrations for theme settings, ensuring a smooth transition when changes are required in the format or structure of setting values.
Example use cases for the theme settings migration system:
1. Renaming a theme setting.
2. Changing the data type of a theme setting (e.g., transforming a string setting containing comma-separated values into a proper list setting).
3. Altering the format of data stored in a theme setting.
All of these use cases and more are now possible while preserving theme setting values for sites that have already modified their theme settings.
Usage:
1. Create a top-level directory called `migrations` in your theme/component, and then within the `migrations` directory create another directory called `settings`.
2. Inside the `migrations/settings` directory, create a JavaScript file using the format `XXXX-some-name.js`, where `XXXX` is a unique 4-digit number, and `some-name` is a descriptor of your choice that describes the migration.
3. Within the JavaScript file, define and export (as the default) a function called `migrate`. This function will receive a `Map` object and must also return a `Map` object (it's acceptable to return the same `Map` object that the function received).
4. The `Map` object received by the `migrate` function will include settings that have been overridden or changed by site administrators. Settings that have never been changed from the default will not be included.
5. The keys and values contained in the `Map` object that the `migrate` function returns will replace all the currently changed settings of the theme.
6. Migrations are executed in numerical order based on the XXXX segment in the migration filenames. For instance, `0001-some-migration.js` will be executed before `0002-another-migration.js`.
Here's a complete example migration script that renames a setting from `setting_with_old_name` to `setting_with_new_name`:
```js
// File name: 0001-rename-setting.js
export default function migrate(settings) {
if (settings.has("setting_with_old_name")) {
settings.set("setting_with_new_name", settings.get("setting_with_old_name"));
}
return settings;
}
```
Internal topic: t/109980
2023-11-02 13:10:15 +08:00
|
|
|
repo_url = MockGitImporter.register("https://example.com/initial_repo.git", repo_path)
|
|
|
|
|
|
|
|
post "/admin/themes/import.json", params: { remote: repo_url }
|
|
|
|
|
|
|
|
expect(response.status).to eq(422)
|
|
|
|
expect(response.parsed_body["errors"]).to contain_exactly(
|
|
|
|
I18n.t(
|
|
|
|
"themes.import_error.migrations.unknown_setting_returned_by_migration",
|
|
|
|
name: "0001-some-migration",
|
|
|
|
setting_name: "unknown_setting",
|
|
|
|
),
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2022-11-04 03:02:26 +08:00
|
|
|
it "fails to import with a failing status" do
|
2023-06-14 04:02:21 +08:00
|
|
|
post "/admin/themes/import.json", params: { remote: "non-existent" }
|
2022-11-04 03:02:26 +08:00
|
|
|
|
|
|
|
expect(response.status).to eq(422)
|
|
|
|
end
|
|
|
|
|
2023-03-23 20:01:04 +08:00
|
|
|
it "fails to import with a failing status" do
|
|
|
|
post "/admin/themes/import.json", params: { remote: "https://#{"a" * 10_000}.com" }
|
|
|
|
|
|
|
|
expect(response.status).to eq(422)
|
|
|
|
end
|
|
|
|
|
2022-11-03 11:42:44 +08:00
|
|
|
it "can lookup a private key by public key" do
|
|
|
|
Discourse.redis.setex("ssh_key_abcdef", 1.hour, "rsa private key")
|
2021-04-20 19:28:59 +08:00
|
|
|
|
2022-11-03 11:42:44 +08:00
|
|
|
post "/admin/themes/import.json",
|
|
|
|
params: {
|
FEATURE: Theme settings migrations (#24071)
This commit introduces a new feature that allows theme developers to manage the transformation of theme settings over time. Similar to Rails migrations, the theme settings migration system enables developers to write and execute migrations for theme settings, ensuring a smooth transition when changes are required in the format or structure of setting values.
Example use cases for the theme settings migration system:
1. Renaming a theme setting.
2. Changing the data type of a theme setting (e.g., transforming a string setting containing comma-separated values into a proper list setting).
3. Altering the format of data stored in a theme setting.
All of these use cases and more are now possible while preserving theme setting values for sites that have already modified their theme settings.
Usage:
1. Create a top-level directory called `migrations` in your theme/component, and then within the `migrations` directory create another directory called `settings`.
2. Inside the `migrations/settings` directory, create a JavaScript file using the format `XXXX-some-name.js`, where `XXXX` is a unique 4-digit number, and `some-name` is a descriptor of your choice that describes the migration.
3. Within the JavaScript file, define and export (as the default) a function called `migrate`. This function will receive a `Map` object and must also return a `Map` object (it's acceptable to return the same `Map` object that the function received).
4. The `Map` object received by the `migrate` function will include settings that have been overridden or changed by site administrators. Settings that have never been changed from the default will not be included.
5. The keys and values contained in the `Map` object that the `migrate` function returns will replace all the currently changed settings of the theme.
6. Migrations are executed in numerical order based on the XXXX segment in the migration filenames. For instance, `0001-some-migration.js` will be executed before `0002-another-migration.js`.
Here's a complete example migration script that renames a setting from `setting_with_old_name` to `setting_with_new_name`:
```js
// File name: 0001-rename-setting.js
export default function migrate(settings) {
if (settings.has("setting_with_old_name")) {
settings.set("setting_with_new_name", settings.get("setting_with_old_name"));
}
return settings;
}
```
Internal topic: t/109980
2023-11-02 13:10:15 +08:00
|
|
|
remote: " #{repo_url} ",
|
2022-11-03 11:42:44 +08:00
|
|
|
public_key: "abcdef",
|
|
|
|
}
|
|
|
|
|
|
|
|
expect(RemoteTheme.last.private_key).to eq("rsa private key")
|
|
|
|
|
|
|
|
expect(response.status).to eq(201)
|
2020-06-03 11:19:42 +08:00
|
|
|
end
|
|
|
|
|
2022-11-03 11:42:44 +08:00
|
|
|
it "imports a theme" do
|
2020-06-03 11:19:42 +08:00
|
|
|
post "/admin/themes/import.json", params: { theme: theme_json_file }
|
2022-11-03 11:42:44 +08:00
|
|
|
expect(response.status).to eq(201)
|
|
|
|
|
|
|
|
json = response.parsed_body
|
|
|
|
|
|
|
|
expect(json["theme"]["name"]).to eq("Sam's Simple Theme")
|
|
|
|
expect(json["theme"]["theme_fields"].length).to eq(2)
|
|
|
|
expect(json["theme"]["auto_update"]).to eq(false)
|
|
|
|
expect(UserHistory.where(action: UserHistory.actions[:change_theme]).count).to eq(1)
|
2020-06-03 11:19:42 +08:00
|
|
|
end
|
|
|
|
|
2022-11-03 11:42:44 +08:00
|
|
|
it "can fail if theme is not accessible" do
|
|
|
|
post "/admin/themes/import.json",
|
|
|
|
params: {
|
|
|
|
remote: "git@github.com:discourse/discourse-inexistent-theme.git",
|
|
|
|
}
|
2018-12-17 22:27:49 +08:00
|
|
|
|
2022-11-03 11:42:44 +08:00
|
|
|
expect(response.status).to eq(422)
|
|
|
|
expect(response.parsed_body["errors"]).to contain_exactly(I18n.t("themes.import_error.git"))
|
|
|
|
end
|
2022-09-01 18:15:23 +08:00
|
|
|
|
2022-11-03 11:42:44 +08:00
|
|
|
it "can force install theme" do
|
|
|
|
post "/admin/themes/import.json",
|
|
|
|
params: {
|
|
|
|
remote: "git@github.com:discourse/discourse-inexistent-theme.git",
|
|
|
|
force: true,
|
|
|
|
}
|
2022-09-01 18:15:23 +08:00
|
|
|
|
2022-11-03 11:42:44 +08:00
|
|
|
expect(response.status).to eq(201)
|
|
|
|
expect(response.parsed_body["theme"]["name"]).to eq("discourse-inexistent-theme")
|
|
|
|
end
|
2022-09-01 18:15:23 +08:00
|
|
|
|
2022-11-03 11:42:44 +08:00
|
|
|
it "fails to import with an error if uploads are not allowed" do
|
|
|
|
SiteSetting.theme_authorized_extensions = "nothing"
|
2022-09-01 18:15:23 +08:00
|
|
|
|
2023-12-07 06:25:00 +08:00
|
|
|
expect do
|
|
|
|
post "/admin/themes/import.json", params: { theme: theme_archive }
|
|
|
|
end.not_to change { Theme.count }
|
2022-09-01 18:15:23 +08:00
|
|
|
|
2022-11-03 11:42:44 +08:00
|
|
|
expect(response.status).to eq(422)
|
|
|
|
end
|
2018-12-17 22:27:49 +08:00
|
|
|
|
2022-11-03 11:42:44 +08:00
|
|
|
it "imports a theme from an archive" do
|
|
|
|
_existing_theme = Fabricate(:theme, name: "Header Icons")
|
2022-09-30 02:00:20 +08:00
|
|
|
|
2022-11-03 11:42:44 +08:00
|
|
|
expect do post "/admin/themes/import.json", params: { theme: theme_archive } end.to change {
|
|
|
|
Theme.count
|
2023-12-07 06:25:00 +08:00
|
|
|
}.by(1)
|
2022-11-03 11:42:44 +08:00
|
|
|
expect(response.status).to eq(201)
|
|
|
|
json = response.parsed_body
|
2022-09-30 02:00:20 +08:00
|
|
|
|
2022-11-03 11:42:44 +08:00
|
|
|
expect(json["theme"]["name"]).to eq("Header Icons")
|
2024-01-11 14:04:02 +08:00
|
|
|
expect(json["theme"]["theme_fields"].length).to eq(6)
|
2022-11-03 11:42:44 +08:00
|
|
|
expect(json["theme"]["auto_update"]).to eq(false)
|
|
|
|
expect(UserHistory.where(action: UserHistory.actions[:change_theme]).count).to eq(1)
|
|
|
|
end
|
2018-06-11 12:54:16 +08:00
|
|
|
|
2022-11-03 11:42:44 +08:00
|
|
|
it "updates an existing theme from an archive by id" do
|
|
|
|
# Used by theme CLI
|
|
|
|
_existing_theme = Fabricate(:theme, name: "Header Icons")
|
|
|
|
other_existing_theme = Fabricate(:theme, name: "Some other name")
|
|
|
|
|
|
|
|
messages =
|
2024-01-11 14:04:02 +08:00
|
|
|
MessageBus.track_publish("/file-change") do
|
2022-11-03 11:42:44 +08:00
|
|
|
expect do
|
|
|
|
post "/admin/themes/import.json",
|
|
|
|
params: {
|
|
|
|
bundle: theme_archive,
|
|
|
|
theme_id: other_existing_theme.id,
|
|
|
|
}
|
2024-01-11 14:04:02 +08:00
|
|
|
|
|
|
|
expect(response.status).to eq(201)
|
2023-12-07 06:25:00 +08:00
|
|
|
end.not_to change { Theme.count }
|
2022-11-03 11:42:44 +08:00
|
|
|
end
|
2024-01-11 14:04:02 +08:00
|
|
|
|
2022-11-03 11:42:44 +08:00
|
|
|
json = response.parsed_body
|
|
|
|
|
|
|
|
# Ensure only one refresh message is sent.
|
|
|
|
# More than 1 is wasteful, and can trigger unusual race conditions in the client
|
|
|
|
# If this test fails, it probably means `theme.save` is being called twice - check any 'autosave' relations
|
2024-01-11 14:04:02 +08:00
|
|
|
expect(messages.count).to eq(1)
|
2022-11-03 11:42:44 +08:00
|
|
|
|
|
|
|
expect(json["theme"]["name"]).to eq("Some other name")
|
|
|
|
expect(json["theme"]["id"]).to eq(other_existing_theme.id)
|
2024-01-11 14:04:02 +08:00
|
|
|
expect(json["theme"]["theme_fields"].length).to eq(6)
|
2022-11-03 11:42:44 +08:00
|
|
|
expect(UserHistory.where(action: UserHistory.actions[:change_theme]).count).to eq(1)
|
|
|
|
end
|
2019-01-23 22:40:21 +08:00
|
|
|
|
2024-01-11 14:04:02 +08:00
|
|
|
it "does not run migrations when importing a theme from an archive and `skip_settings_migrations` params is present" do
|
|
|
|
other_existing_theme = Fabricate(:theme, name: "Some other name")
|
|
|
|
|
|
|
|
post "/admin/themes/import.json",
|
|
|
|
params: {
|
|
|
|
bundle: theme_archive,
|
|
|
|
theme_id: other_existing_theme.id,
|
|
|
|
skip_migrations: true,
|
|
|
|
}
|
|
|
|
|
|
|
|
expect(response.status).to eq(201)
|
|
|
|
expect(other_existing_theme.theme_settings_migrations.exists?).to eq(false)
|
|
|
|
end
|
|
|
|
|
2022-11-03 11:42:44 +08:00
|
|
|
it "creates a new theme when id specified as nil" do
|
|
|
|
# Used by theme CLI
|
|
|
|
existing_theme = Fabricate(:theme, name: "Header Icons")
|
2022-08-10 18:30:18 +08:00
|
|
|
|
2022-11-03 11:42:44 +08:00
|
|
|
expect do
|
|
|
|
post "/admin/themes/import.json", params: { bundle: theme_archive, theme_id: nil }
|
2023-12-07 06:25:00 +08:00
|
|
|
end.to change { Theme.count }.by(1)
|
2022-11-03 11:42:44 +08:00
|
|
|
expect(response.status).to eq(201)
|
|
|
|
json = response.parsed_body
|
|
|
|
|
|
|
|
expect(json["theme"]["name"]).to eq("Header Icons")
|
|
|
|
expect(json["theme"]["id"]).not_to eq(existing_theme.id)
|
2024-01-11 14:04:02 +08:00
|
|
|
expect(json["theme"]["theme_fields"].length).to eq(6)
|
2022-11-03 11:42:44 +08:00
|
|
|
expect(json["theme"]["auto_update"]).to eq(false)
|
|
|
|
expect(UserHistory.where(action: UserHistory.actions[:change_theme]).count).to eq(1)
|
|
|
|
end
|
2022-08-10 18:30:18 +08:00
|
|
|
end
|
|
|
|
|
2022-11-03 11:42:44 +08:00
|
|
|
shared_examples "theme import not allowed" do
|
|
|
|
it "prevents theme import with a 404 response" do
|
|
|
|
post "/admin/themes/import.json", params: { theme: theme_json_file }
|
2022-08-10 18:30:18 +08:00
|
|
|
|
2022-11-03 11:42:44 +08:00
|
|
|
expect(response.status).to eq(404)
|
|
|
|
expect(response.parsed_body["errors"]).to include(I18n.t("not_found"))
|
|
|
|
end
|
2022-08-10 18:30:18 +08:00
|
|
|
end
|
|
|
|
|
2022-11-03 11:42:44 +08:00
|
|
|
context "when logged in as a moderator" do
|
|
|
|
before { sign_in(moderator) }
|
2022-04-01 09:03:14 +08:00
|
|
|
|
2022-11-03 11:42:44 +08:00
|
|
|
include_examples "theme import not allowed"
|
|
|
|
end
|
2022-04-01 09:03:14 +08:00
|
|
|
|
2022-11-03 11:42:44 +08:00
|
|
|
context "when logged in as a non-staff user" do
|
|
|
|
before { sign_in(user) }
|
|
|
|
|
|
|
|
include_examples "theme import not allowed"
|
2022-04-01 09:03:14 +08:00
|
|
|
end
|
2022-11-03 11:42:44 +08:00
|
|
|
end
|
2022-04-01 09:03:14 +08:00
|
|
|
|
2022-11-03 11:42:44 +08:00
|
|
|
describe "#index" do
|
|
|
|
context "when logged in as an admin" do
|
|
|
|
before { sign_in(admin) }
|
2019-01-23 22:40:21 +08:00
|
|
|
|
2022-11-03 11:42:44 +08:00
|
|
|
it "correctly returns themes" do
|
|
|
|
ColorScheme.destroy_all
|
|
|
|
Theme.destroy_all
|
2019-01-23 22:40:21 +08:00
|
|
|
|
2022-11-03 11:42:44 +08:00
|
|
|
theme = Fabricate(:theme)
|
|
|
|
theme.set_field(target: :common, name: :scss, value: ".body{color: black;}")
|
|
|
|
theme.set_field(target: :desktop, name: :after_header, value: "<b>test</b>")
|
2023-01-09 19:18:21 +08:00
|
|
|
|
2024-01-11 14:04:02 +08:00
|
|
|
theme.set_field(
|
|
|
|
target: :migrations,
|
|
|
|
name: "0001-some-migration",
|
|
|
|
value: "export default function migrate(settings) { return settings; }",
|
|
|
|
)
|
|
|
|
|
2022-11-03 11:42:44 +08:00
|
|
|
theme.remote_theme =
|
|
|
|
RemoteTheme.new(
|
|
|
|
remote_url: "awesome.git",
|
|
|
|
remote_version: "7",
|
|
|
|
local_version: "8",
|
|
|
|
remote_updated_at: Time.zone.now,
|
|
|
|
)
|
2019-01-23 22:40:21 +08:00
|
|
|
|
2022-11-03 11:42:44 +08:00
|
|
|
theme.save!
|
2019-01-23 22:40:21 +08:00
|
|
|
|
2022-11-03 11:42:44 +08:00
|
|
|
# this will get serialized as well
|
|
|
|
ColorScheme.create_from_base(name: "test", colors: [])
|
2019-01-30 22:17:04 +08:00
|
|
|
|
2022-11-03 11:42:44 +08:00
|
|
|
get "/admin/themes.json"
|
2019-01-30 22:17:04 +08:00
|
|
|
|
2022-11-03 11:42:44 +08:00
|
|
|
expect(response.status).to eq(200)
|
2019-01-30 22:17:04 +08:00
|
|
|
|
2022-11-03 11:42:44 +08:00
|
|
|
json = response.parsed_body
|
2019-08-29 22:47:08 +08:00
|
|
|
|
2022-11-03 11:42:44 +08:00
|
|
|
expect(json["extras"]["color_schemes"].length).to eq(1)
|
|
|
|
theme_json = json["themes"].find { |t| t["id"] == theme.id }
|
2024-01-11 14:04:02 +08:00
|
|
|
expect(theme_json["theme_fields"].length).to eq(3)
|
|
|
|
|
|
|
|
expect(
|
|
|
|
theme_json["theme_fields"].find { |theme_field| theme_field["target"] == "migrations" }[
|
|
|
|
"migrated"
|
|
|
|
],
|
|
|
|
).to eq(false)
|
|
|
|
|
2022-11-03 11:42:44 +08:00
|
|
|
expect(theme_json["remote_theme"]["remote_version"]).to eq("7")
|
|
|
|
end
|
2019-01-30 22:17:04 +08:00
|
|
|
end
|
|
|
|
|
2024-05-06 12:27:30 +08:00
|
|
|
it "allows themes and components to be edited" do
|
|
|
|
sign_in(admin)
|
|
|
|
theme = Fabricate(:theme, name: "Awesome Theme")
|
|
|
|
component = Fabricate(:theme, name: "Awesome component", component: true)
|
|
|
|
|
|
|
|
get "/admin/customize/themes/#{theme.id}/common/scss/edit"
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
|
|
|
|
get "/admin/customize/components/#{component.id}/common/scss/edit"
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
end
|
|
|
|
|
2022-11-03 11:42:44 +08:00
|
|
|
shared_examples "themes inaccessible" do
|
|
|
|
it "denies access with a 404 response" do
|
|
|
|
get "/admin/themes.json"
|
2019-01-30 22:17:04 +08:00
|
|
|
|
2022-11-03 11:42:44 +08:00
|
|
|
expect(response.status).to eq(404)
|
|
|
|
expect(response.parsed_body["errors"]).to include(I18n.t("not_found"))
|
|
|
|
end
|
|
|
|
end
|
2019-01-30 22:17:04 +08:00
|
|
|
|
2022-11-03 11:42:44 +08:00
|
|
|
context "when logged in as a moderator" do
|
|
|
|
before { sign_in(moderator) }
|
|
|
|
|
|
|
|
include_examples "themes inaccessible"
|
2019-01-30 22:17:04 +08:00
|
|
|
end
|
2018-06-11 12:54:16 +08:00
|
|
|
|
2022-11-03 11:42:44 +08:00
|
|
|
context "when logged in as a non-staff user" do
|
|
|
|
before { sign_in(user) }
|
2018-06-11 12:54:16 +08:00
|
|
|
|
2022-11-03 11:42:44 +08:00
|
|
|
include_examples "themes inaccessible"
|
|
|
|
end
|
|
|
|
end
|
2018-06-11 12:54:16 +08:00
|
|
|
|
2022-11-03 11:42:44 +08:00
|
|
|
describe "#create" do
|
|
|
|
context "when logged in as an admin" do
|
|
|
|
before { sign_in(admin) }
|
2018-06-11 12:54:16 +08:00
|
|
|
|
2022-11-03 11:42:44 +08:00
|
|
|
it "creates a theme" do
|
|
|
|
post "/admin/themes.json",
|
|
|
|
params: {
|
|
|
|
theme: {
|
|
|
|
name: "my test name",
|
|
|
|
theme_fields: [name: "scss", target: "common", value: "body{color: red;}"],
|
|
|
|
},
|
|
|
|
}
|
2018-06-11 12:54:16 +08:00
|
|
|
|
2022-11-03 11:42:44 +08:00
|
|
|
expect(response.status).to eq(201)
|
2018-06-11 12:54:16 +08:00
|
|
|
|
2022-11-03 11:42:44 +08:00
|
|
|
json = response.parsed_body
|
2018-06-11 12:54:16 +08:00
|
|
|
|
2022-11-03 11:42:44 +08:00
|
|
|
expect(json["theme"]["theme_fields"].length).to eq(1)
|
|
|
|
expect(UserHistory.where(action: UserHistory.actions[:change_theme]).count).to eq(1)
|
|
|
|
end
|
|
|
|
end
|
2018-06-11 12:54:16 +08:00
|
|
|
|
2022-11-03 11:42:44 +08:00
|
|
|
shared_examples "theme creation not allowed" do
|
|
|
|
it "prevents creation with a 404 response" do
|
|
|
|
expect do
|
|
|
|
post "/admin/themes.json",
|
|
|
|
params: {
|
|
|
|
theme: {
|
|
|
|
name: "my test name",
|
|
|
|
theme_fields: [name: "scss", target: "common", value: "body{color: red;}"],
|
|
|
|
},
|
|
|
|
}
|
|
|
|
end.not_to change { Theme.count }
|
2018-06-11 12:54:16 +08:00
|
|
|
|
2022-11-03 11:42:44 +08:00
|
|
|
expect(response.status).to eq(404)
|
|
|
|
expect(response.parsed_body["errors"]).to include(I18n.t("not_found"))
|
|
|
|
end
|
2018-06-11 12:54:16 +08:00
|
|
|
end
|
|
|
|
|
2022-11-03 11:42:44 +08:00
|
|
|
context "when logged in as a moderator" do
|
|
|
|
before { sign_in(moderator) }
|
2018-06-11 12:54:16 +08:00
|
|
|
|
2022-11-03 11:42:44 +08:00
|
|
|
include_examples "theme creation not allowed"
|
|
|
|
end
|
2018-06-11 12:54:16 +08:00
|
|
|
|
2022-11-03 11:42:44 +08:00
|
|
|
context "when logged in as a non-staff user" do
|
|
|
|
before { sign_in(user) }
|
2018-06-11 12:54:16 +08:00
|
|
|
|
2022-11-03 11:42:44 +08:00
|
|
|
include_examples "theme creation not allowed"
|
2018-06-11 12:54:16 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "#update" do
|
2019-05-07 11:12:20 +08:00
|
|
|
let!(:theme) { Fabricate(:theme) }
|
2018-06-11 12:54:16 +08:00
|
|
|
|
2022-11-03 11:42:44 +08:00
|
|
|
context "when logged in as an admin" do
|
|
|
|
before { sign_in(admin) }
|
2019-04-11 13:56:43 +08:00
|
|
|
|
2022-11-03 11:42:44 +08:00
|
|
|
it "returns the right response when an invalid id is given" do
|
|
|
|
put "/admin/themes/99999.json"
|
2019-04-11 13:56:43 +08:00
|
|
|
|
2022-11-03 11:42:44 +08:00
|
|
|
expect(response.status).to eq(400)
|
|
|
|
end
|
2018-06-11 12:54:16 +08:00
|
|
|
|
2022-11-03 11:42:44 +08:00
|
|
|
it "can change default theme" do
|
|
|
|
SiteSetting.default_theme_id = -1
|
2018-06-11 12:54:16 +08:00
|
|
|
|
2022-11-03 11:42:44 +08:00
|
|
|
put "/admin/themes/#{theme.id}.json", params: { id: theme.id, theme: { default: true } }
|
2018-06-11 12:54:16 +08:00
|
|
|
|
2022-11-03 11:42:44 +08:00
|
|
|
expect(response.status).to eq(200)
|
|
|
|
expect(SiteSetting.default_theme_id).to eq(theme.id)
|
|
|
|
end
|
2018-06-11 12:54:16 +08:00
|
|
|
|
2022-11-03 11:42:44 +08:00
|
|
|
it "can unset default theme" do
|
|
|
|
SiteSetting.default_theme_id = theme.id
|
2018-06-11 12:54:16 +08:00
|
|
|
|
2022-11-03 11:42:44 +08:00
|
|
|
put "/admin/themes/#{theme.id}.json", params: { theme: { default: false } }
|
2018-06-11 12:54:16 +08:00
|
|
|
|
2022-11-03 11:42:44 +08:00
|
|
|
expect(response.status).to eq(200)
|
|
|
|
expect(SiteSetting.default_theme_id).to eq(-1)
|
2020-06-03 11:19:42 +08:00
|
|
|
end
|
|
|
|
|
2022-11-03 11:42:44 +08:00
|
|
|
context "when theme allowlist mode is enabled" do
|
|
|
|
before do
|
|
|
|
global_setting :allowed_theme_repos, " https://magic.com/repo.git, https://x.com/git"
|
|
|
|
end
|
|
|
|
|
|
|
|
it "unconditionally bans theme_fields from updating" do
|
|
|
|
r = RemoteTheme.create!(remote_url: "https://magic.com/repo.git")
|
|
|
|
theme.update!(remote_theme_id: r.id)
|
|
|
|
|
|
|
|
put "/admin/themes/#{theme.id}.json",
|
|
|
|
params: {
|
|
|
|
theme: {
|
|
|
|
name: "my test name",
|
|
|
|
theme_fields: [
|
|
|
|
{ name: "scss", target: "common", value: "" },
|
|
|
|
{ name: "scss", target: "desktop", value: "body{color: blue;}" },
|
|
|
|
],
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
expect(response.status).to eq(403)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it "updates a theme" do
|
|
|
|
theme.set_field(target: :common, name: :scss, value: ".body{color: black;}")
|
|
|
|
theme.save
|
|
|
|
|
|
|
|
child_theme = Fabricate(:theme, component: true)
|
|
|
|
|
|
|
|
upload =
|
|
|
|
UploadCreator.new(file_from_fixtures("logo.png"), "logo.png").create_for(
|
|
|
|
Discourse.system_user.id,
|
|
|
|
)
|
2023-01-09 19:18:21 +08:00
|
|
|
|
2020-06-03 11:19:42 +08:00
|
|
|
put "/admin/themes/#{theme.id}.json",
|
|
|
|
params: {
|
|
|
|
theme: {
|
2022-11-03 11:42:44 +08:00
|
|
|
child_theme_ids: [child_theme.id],
|
2020-06-03 11:19:42 +08:00
|
|
|
name: "my test name",
|
|
|
|
theme_fields: [
|
|
|
|
{ name: "scss", target: "common", value: "" },
|
|
|
|
{ name: "scss", target: "desktop", value: "body{color: blue;}" },
|
2022-11-03 11:42:44 +08:00
|
|
|
{ name: "bob", target: "common", value: "", type_id: 2, upload_id: upload.id },
|
2020-06-03 11:19:42 +08:00
|
|
|
],
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2022-11-03 11:42:44 +08:00
|
|
|
expect(response.status).to eq(200)
|
2020-06-03 11:19:42 +08:00
|
|
|
|
2022-11-03 11:42:44 +08:00
|
|
|
json = response.parsed_body
|
2018-06-11 12:54:16 +08:00
|
|
|
|
2023-04-24 22:30:51 +08:00
|
|
|
expect(json["theme"]["theme_fields"].length).to eq(2)
|
2022-11-03 11:42:44 +08:00
|
|
|
expect(json["theme"]["child_themes"].length).to eq(1)
|
|
|
|
expect(UserHistory.where(action: UserHistory.actions[:change_theme]).count).to eq(1)
|
|
|
|
end
|
2018-06-11 12:54:16 +08:00
|
|
|
|
2022-11-03 11:42:44 +08:00
|
|
|
it "prevents theme update when using ember css selectors" do
|
|
|
|
child_theme = Fabricate(:theme, component: true)
|
|
|
|
|
|
|
|
put "/admin/themes/#{theme.id}.json",
|
|
|
|
params: {
|
|
|
|
theme: {
|
|
|
|
child_theme_ids: [child_theme.id],
|
|
|
|
name: "my test name",
|
|
|
|
theme_fields: [
|
|
|
|
{ name: "scss", target: "common", value: "" },
|
|
|
|
{ name: "scss", target: "desktop", value: ".ember-view{color: blue;}" },
|
|
|
|
],
|
|
|
|
},
|
2018-06-11 12:54:16 +08:00
|
|
|
}
|
|
|
|
|
2022-11-03 11:42:44 +08:00
|
|
|
expect(response.status).to eq(200)
|
2018-06-11 12:54:16 +08:00
|
|
|
|
2022-11-03 11:42:44 +08:00
|
|
|
json = response.parsed_body
|
2018-06-11 12:54:16 +08:00
|
|
|
|
2022-11-03 11:42:44 +08:00
|
|
|
fields = json["theme"]["theme_fields"].sort { |a, b| a["value"] <=> b["value"] }
|
|
|
|
expect(fields[0]["error"]).to eq(I18n.t("themes.ember_selector_error"))
|
2018-06-11 12:54:16 +08:00
|
|
|
|
2022-11-03 11:42:44 +08:00
|
|
|
put "/admin/themes/#{theme.id}.json",
|
|
|
|
params: {
|
|
|
|
theme: {
|
|
|
|
child_theme_ids: [child_theme.id],
|
|
|
|
name: "my test name",
|
|
|
|
theme_fields: [
|
|
|
|
{ name: "scss", target: "common", value: "" },
|
|
|
|
{ name: "scss", target: "desktop", value: "#ember392{color: blue;}" },
|
|
|
|
],
|
|
|
|
},
|
|
|
|
}
|
2018-08-08 12:46:34 +08:00
|
|
|
|
2022-11-03 11:42:44 +08:00
|
|
|
expect(response.status).to eq(200)
|
|
|
|
json = response.parsed_body
|
2021-02-12 04:48:57 +08:00
|
|
|
|
2022-11-03 11:42:44 +08:00
|
|
|
fields = json["theme"]["theme_fields"].sort { |a, b| a["value"] <=> b["value"] }
|
|
|
|
expect(fields[0]["error"]).to eq(I18n.t("themes.ember_selector_error"))
|
|
|
|
end
|
2021-02-12 04:48:57 +08:00
|
|
|
|
2022-11-03 11:42:44 +08:00
|
|
|
it "blocks remote theme fields from being locally edited" do
|
|
|
|
r = RemoteTheme.create!(remote_url: "https://magic.com/repo.git")
|
|
|
|
theme.update!(remote_theme_id: r.id)
|
2021-02-12 04:48:57 +08:00
|
|
|
|
2022-11-03 11:42:44 +08:00
|
|
|
put "/admin/themes/#{theme.id}.json",
|
|
|
|
params: {
|
|
|
|
theme: {
|
|
|
|
theme_fields: [
|
|
|
|
{ name: "scss", target: "common", value: "" },
|
|
|
|
{ name: "header", target: "common", value: "filename.jpg", upload_id: 4 },
|
|
|
|
],
|
|
|
|
},
|
|
|
|
}
|
2021-02-12 04:48:57 +08:00
|
|
|
|
2022-11-03 11:42:44 +08:00
|
|
|
expect(response.status).to eq(403)
|
|
|
|
end
|
|
|
|
|
2023-06-14 01:07:47 +08:00
|
|
|
it "creates new theme fields" do
|
|
|
|
expect(theme.theme_fields.count).to eq(0)
|
|
|
|
|
|
|
|
put "/admin/themes/#{theme.id}.json",
|
|
|
|
params: {
|
|
|
|
theme: {
|
|
|
|
theme_fields: [{ name: "scss", target: "common", value: "test" }],
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
theme.reload
|
|
|
|
expect(theme.theme_fields.count).to eq(1)
|
|
|
|
theme_field = theme.theme_fields.first
|
|
|
|
expect(theme_field.name).to eq("scss")
|
|
|
|
expect(theme_field.target_id).to eq(Theme.targets[:common])
|
|
|
|
expect(theme_field.value).to eq("test")
|
|
|
|
end
|
|
|
|
|
|
|
|
it "doesn't create theme fields when they don't pass validation" do
|
|
|
|
expect(theme.theme_fields.count).to eq(0)
|
|
|
|
|
|
|
|
put "/admin/themes/#{theme.id}.json",
|
|
|
|
params: {
|
|
|
|
theme: {
|
|
|
|
theme_fields: [
|
|
|
|
{ name: "scss", target: "common", value: "Na " * 1024**2 + "Batman!" },
|
|
|
|
],
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
expect(response.status).to eq(422)
|
|
|
|
json = JSON.parse(response.body)
|
|
|
|
expect(json["errors"].first).to include("Value is too long")
|
|
|
|
end
|
|
|
|
|
2022-11-03 11:42:44 +08:00
|
|
|
it "allows zip-imported theme fields to be locally edited" do
|
|
|
|
r = RemoteTheme.create!(remote_url: "")
|
|
|
|
theme.update!(remote_theme_id: r.id)
|
2021-02-12 04:48:57 +08:00
|
|
|
|
2022-11-03 11:42:44 +08:00
|
|
|
put "/admin/themes/#{theme.id}.json",
|
|
|
|
params: {
|
|
|
|
theme: {
|
|
|
|
theme_fields: [
|
|
|
|
{ name: "scss", target: "common", value: "" },
|
|
|
|
{ name: "header", target: "common", value: "filename.jpg", upload_id: 4 },
|
|
|
|
],
|
|
|
|
},
|
2021-02-12 04:48:57 +08:00
|
|
|
}
|
|
|
|
|
2022-11-03 11:42:44 +08:00
|
|
|
expect(response.status).to eq(200)
|
|
|
|
end
|
2021-02-12 04:48:57 +08:00
|
|
|
|
2022-11-03 11:42:44 +08:00
|
|
|
it "updates a child theme" do
|
|
|
|
child_theme = Fabricate(:theme, component: true)
|
|
|
|
put "/admin/themes/#{child_theme.id}.json",
|
|
|
|
params: {
|
|
|
|
theme: {
|
|
|
|
parent_theme_ids: [theme.id],
|
|
|
|
},
|
|
|
|
}
|
|
|
|
expect(child_theme.parent_themes).to eq([theme])
|
|
|
|
end
|
2021-02-12 04:48:57 +08:00
|
|
|
|
2022-11-03 11:42:44 +08:00
|
|
|
it "can update translations" do
|
|
|
|
theme.set_field(
|
|
|
|
target: :translations,
|
|
|
|
name: :en,
|
|
|
|
value: { en: { somegroup: { somestring: "defaultstring" } } }.deep_stringify_keys.to_yaml,
|
|
|
|
)
|
|
|
|
theme.save!
|
2020-11-13 23:57:49 +08:00
|
|
|
|
2022-11-03 11:42:44 +08:00
|
|
|
put "/admin/themes/#{theme.id}.json",
|
|
|
|
params: {
|
|
|
|
theme: {
|
|
|
|
translations: {
|
|
|
|
"somegroup.somestring" => "overriddenstring",
|
2023-01-09 19:18:21 +08:00
|
|
|
},
|
|
|
|
},
|
2022-11-03 11:42:44 +08:00
|
|
|
}
|
2020-11-13 23:57:49 +08:00
|
|
|
|
2022-11-03 11:42:44 +08:00
|
|
|
# Response correct
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
json = response.parsed_body
|
|
|
|
expect(json["theme"]["translations"][0]["value"]).to eq("overriddenstring")
|
2020-11-13 23:57:49 +08:00
|
|
|
|
2022-11-03 11:42:44 +08:00
|
|
|
# Database correct
|
|
|
|
theme.reload
|
|
|
|
expect(theme.theme_translation_overrides.count).to eq(1)
|
|
|
|
expect(theme.theme_translation_overrides.first.translation_key).to eq(
|
|
|
|
"somegroup.somestring",
|
|
|
|
)
|
2020-12-10 03:41:42 +08:00
|
|
|
|
2022-11-03 11:42:44 +08:00
|
|
|
# Set back to default
|
|
|
|
put "/admin/themes/#{theme.id}.json",
|
|
|
|
params: {
|
|
|
|
theme: {
|
|
|
|
translations: {
|
|
|
|
"somegroup.somestring" => "defaultstring",
|
2023-01-09 19:18:21 +08:00
|
|
|
},
|
|
|
|
},
|
2022-11-03 11:42:44 +08:00
|
|
|
}
|
|
|
|
# Response correct
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
json = response.parsed_body
|
|
|
|
expect(json["theme"]["translations"][0]["value"]).to eq("defaultstring")
|
|
|
|
|
|
|
|
# Database correct
|
|
|
|
theme.reload
|
|
|
|
expect(theme.theme_translation_overrides.count).to eq(0)
|
|
|
|
end
|
2020-12-10 03:41:42 +08:00
|
|
|
|
2022-11-03 11:42:44 +08:00
|
|
|
it "checking for updates saves the remote_theme record" do
|
|
|
|
theme.remote_theme =
|
|
|
|
RemoteTheme.create!(
|
|
|
|
remote_url: "http://discourse.org",
|
|
|
|
remote_version: "a",
|
|
|
|
local_version: "a",
|
|
|
|
commits_behind: 0,
|
|
|
|
)
|
|
|
|
theme.save!
|
|
|
|
ThemeStore::GitImporter.any_instance.stubs(:import!)
|
|
|
|
ThemeStore::GitImporter.any_instance.stubs(:commits_since).returns(["b", 1])
|
2020-12-10 03:41:42 +08:00
|
|
|
|
2022-11-03 11:42:44 +08:00
|
|
|
put "/admin/themes/#{theme.id}.json", params: { theme: { remote_check: true } }
|
|
|
|
theme.reload
|
|
|
|
expect(theme.remote_theme.remote_version).to eq("b")
|
|
|
|
expect(theme.remote_theme.commits_behind).to eq(1)
|
|
|
|
end
|
2019-11-28 13:19:01 +08:00
|
|
|
|
2022-11-03 11:42:44 +08:00
|
|
|
it "can disable component" do
|
|
|
|
child = Fabricate(:theme, component: true)
|
2019-01-17 19:46:11 +08:00
|
|
|
|
2022-11-03 11:42:44 +08:00
|
|
|
put "/admin/themes/#{child.id}.json", params: { theme: { enabled: false } }
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
json = response.parsed_body
|
|
|
|
expect(json["theme"]["enabled"]).to eq(false)
|
|
|
|
expect(
|
|
|
|
UserHistory.where(
|
|
|
|
context: child.id.to_s,
|
|
|
|
action: UserHistory.actions[:disable_theme_component],
|
|
|
|
).size,
|
|
|
|
).to eq(1)
|
|
|
|
expect(json["theme"]["disabled_by"]["id"]).to eq(admin.id)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "enabling/disabling a component creates the correct staff action log" do
|
|
|
|
child = Fabricate(:theme, component: true)
|
|
|
|
UserHistory.destroy_all
|
|
|
|
|
|
|
|
put "/admin/themes/#{child.id}.json", params: { theme: { enabled: false } }
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
|
|
|
|
expect(
|
|
|
|
UserHistory.where(
|
|
|
|
context: child.id.to_s,
|
|
|
|
action: UserHistory.actions[:disable_theme_component],
|
|
|
|
).size,
|
|
|
|
).to eq(1)
|
|
|
|
expect(
|
|
|
|
UserHistory.where(
|
|
|
|
context: child.id.to_s,
|
|
|
|
action: UserHistory.actions[:enable_theme_component],
|
|
|
|
).size,
|
|
|
|
).to eq(0)
|
2023-01-09 19:18:21 +08:00
|
|
|
|
2022-11-03 11:42:44 +08:00
|
|
|
put "/admin/themes/#{child.id}.json", params: { theme: { enabled: true } }
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
json = response.parsed_body
|
|
|
|
|
|
|
|
expect(
|
|
|
|
UserHistory.where(
|
|
|
|
context: child.id.to_s,
|
|
|
|
action: UserHistory.actions[:disable_theme_component],
|
|
|
|
).size,
|
|
|
|
).to eq(1)
|
|
|
|
expect(
|
|
|
|
UserHistory.where(
|
|
|
|
context: child.id.to_s,
|
|
|
|
action: UserHistory.actions[:enable_theme_component],
|
|
|
|
).size,
|
|
|
|
).to eq(1)
|
|
|
|
|
|
|
|
expect(json["theme"]["disabled_by"]).to eq(nil)
|
|
|
|
expect(json["theme"]["enabled"]).to eq(true)
|
|
|
|
end
|
2019-02-05 21:49:16 +08:00
|
|
|
|
2022-11-03 11:42:44 +08:00
|
|
|
it "handles import errors on update" do
|
|
|
|
theme.create_remote_theme!(remote_url: "https://example.com/repository")
|
|
|
|
theme.save!
|
2019-08-30 05:32:54 +08:00
|
|
|
|
2022-11-03 11:42:44 +08:00
|
|
|
# RemoteTheme is extensively tested, and setting up the test scaffold is a large overhead
|
|
|
|
# So use a stub here to test the controller
|
|
|
|
RemoteTheme
|
|
|
|
.any_instance
|
|
|
|
.stubs(:update_from_remote)
|
|
|
|
.raises(RemoteTheme::ImportError.new("error message"))
|
|
|
|
put "/admin/themes/#{theme.id}.json", params: { theme: { remote_update: true } }
|
|
|
|
expect(response.status).to eq(422)
|
|
|
|
expect(response.parsed_body["errors"].first).to eq("error message")
|
|
|
|
end
|
2019-08-30 05:32:54 +08:00
|
|
|
|
2022-11-03 11:42:44 +08:00
|
|
|
it "returns the right error message" do
|
|
|
|
theme.update!(component: true)
|
2019-07-03 16:18:11 +08:00
|
|
|
|
2022-11-03 11:42:44 +08:00
|
|
|
put "/admin/themes/#{theme.id}.json", params: { theme: { default: true } }
|
2019-07-03 16:18:11 +08:00
|
|
|
|
2022-11-03 11:42:44 +08:00
|
|
|
expect(response.status).to eq(400)
|
|
|
|
expect(response.parsed_body["errors"].first).to include(
|
|
|
|
I18n.t("themes.errors.component_no_default"),
|
|
|
|
)
|
|
|
|
end
|
2019-07-03 16:18:11 +08:00
|
|
|
|
2022-11-03 11:42:44 +08:00
|
|
|
it "prevents converting the default theme to a component" do
|
|
|
|
SiteSetting.default_theme_id = theme.id
|
2019-07-03 16:18:11 +08:00
|
|
|
|
2022-11-03 11:42:44 +08:00
|
|
|
put "/admin/themes/#{theme.id}.json", params: { theme: { component: true } }
|
2019-01-17 19:46:11 +08:00
|
|
|
|
2022-11-03 11:42:44 +08:00
|
|
|
# should this error message be localized? InvalidParameters :component
|
|
|
|
expect(response.status).to eq(400)
|
|
|
|
expect(response.parsed_body["errors"].first).to include("component")
|
|
|
|
end
|
2019-01-17 19:46:11 +08:00
|
|
|
end
|
|
|
|
|
2022-11-03 11:42:44 +08:00
|
|
|
shared_examples "theme update not allowed" do
|
|
|
|
it "prevents updates with a 404 response" do
|
|
|
|
SiteSetting.default_theme_id = -1
|
2018-08-08 12:46:34 +08:00
|
|
|
|
2022-11-03 11:42:44 +08:00
|
|
|
put "/admin/themes/#{theme.id}.json", params: { id: theme.id, theme: { default: true } }
|
2018-08-08 12:46:34 +08:00
|
|
|
|
2022-11-03 11:42:44 +08:00
|
|
|
expect(response.status).to eq(404)
|
|
|
|
expect(response.parsed_body["errors"]).to include(I18n.t("not_found"))
|
|
|
|
expect(SiteSetting.default_theme_id).not_to eq(theme.id)
|
|
|
|
end
|
2018-08-08 12:46:34 +08:00
|
|
|
end
|
2021-01-12 06:53:04 +08:00
|
|
|
|
2022-11-03 11:42:44 +08:00
|
|
|
context "when logged in as a moderator" do
|
|
|
|
before { sign_in(moderator) }
|
2021-01-12 06:53:04 +08:00
|
|
|
|
2022-11-03 11:42:44 +08:00
|
|
|
include_examples "theme update not allowed"
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when logged in as a non-staff user" do
|
|
|
|
before { sign_in(user) }
|
2021-01-12 06:53:04 +08:00
|
|
|
|
2022-11-03 11:42:44 +08:00
|
|
|
include_examples "theme update not allowed"
|
2021-01-12 06:53:04 +08:00
|
|
|
end
|
2018-06-11 12:54:16 +08:00
|
|
|
end
|
2018-10-15 12:55:23 +08:00
|
|
|
|
|
|
|
describe "#destroy" do
|
2019-05-07 11:12:20 +08:00
|
|
|
let!(:theme) { Fabricate(:theme) }
|
2018-10-15 12:55:23 +08:00
|
|
|
|
2022-11-03 11:42:44 +08:00
|
|
|
context "when logged in as an admin" do
|
|
|
|
before { sign_in(admin) }
|
|
|
|
|
|
|
|
it "returns the right response when an invalid id is given" do
|
|
|
|
delete "/admin/themes/9999.json"
|
|
|
|
|
|
|
|
expect(response.status).to eq(400)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "deletes the field's javascript cache" do
|
|
|
|
theme.set_field(
|
|
|
|
target: :common,
|
|
|
|
name: :header,
|
|
|
|
value: '<script>console.log("test")</script>',
|
|
|
|
)
|
|
|
|
theme.save!
|
|
|
|
|
|
|
|
javascript_cache =
|
|
|
|
theme
|
|
|
|
.theme_fields
|
|
|
|
.find_by(target_id: Theme.targets[:common], name: :header)
|
|
|
|
.javascript_cache
|
|
|
|
expect(javascript_cache).to_not eq(nil)
|
|
|
|
|
|
|
|
delete "/admin/themes/#{theme.id}.json"
|
2019-04-11 13:56:43 +08:00
|
|
|
|
2022-11-03 11:42:44 +08:00
|
|
|
expect(response.status).to eq(204)
|
|
|
|
expect { theme.reload }.to raise_error(ActiveRecord::RecordNotFound)
|
|
|
|
expect { javascript_cache.reload }.to raise_error(ActiveRecord::RecordNotFound)
|
|
|
|
end
|
2019-04-11 13:56:43 +08:00
|
|
|
end
|
|
|
|
|
2022-11-03 11:42:44 +08:00
|
|
|
shared_examples "theme deletion not allowed" do
|
|
|
|
it "prevent deletion with a 404 response" do
|
|
|
|
delete "/admin/themes/#{theme.id}.json"
|
2018-10-15 12:55:23 +08:00
|
|
|
|
2022-11-03 11:42:44 +08:00
|
|
|
expect(response.status).to eq(404)
|
|
|
|
expect(response.parsed_body["errors"]).to include(I18n.t("not_found"))
|
|
|
|
expect(theme.reload).to be_present
|
|
|
|
end
|
|
|
|
end
|
2018-10-15 12:55:23 +08:00
|
|
|
|
2022-11-03 11:42:44 +08:00
|
|
|
context "when logged in as a moderator" do
|
|
|
|
before { sign_in(moderator) }
|
2018-10-15 12:55:23 +08:00
|
|
|
|
2022-11-03 11:42:44 +08:00
|
|
|
include_examples "theme deletion not allowed"
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when logged in as a non-staff user" do
|
|
|
|
before { sign_in(user) }
|
|
|
|
|
|
|
|
include_examples "theme deletion not allowed"
|
2018-10-15 12:55:23 +08:00
|
|
|
end
|
|
|
|
end
|
2019-04-11 13:56:43 +08:00
|
|
|
|
|
|
|
describe "#preview" do
|
2022-11-03 11:42:44 +08:00
|
|
|
context "when logged in as an admin" do
|
|
|
|
before { sign_in(admin) }
|
|
|
|
|
|
|
|
it "should return the right response when an invalid id is given" do
|
|
|
|
get "/admin/themes/9999/preview.json"
|
|
|
|
|
|
|
|
expect(response.status).to eq(400)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
shared_examples "theme previews inaccessible" do
|
|
|
|
it "denies access with a 404 response" do
|
|
|
|
theme = Fabricate(:theme)
|
2019-04-11 13:56:43 +08:00
|
|
|
|
2022-11-03 11:42:44 +08:00
|
|
|
get "/admin/themes/#{theme.id}/preview.json"
|
|
|
|
|
|
|
|
expect(response.status).to eq(404)
|
|
|
|
expect(response.parsed_body["errors"]).to include(I18n.t("not_found"))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when logged in as a moderator" do
|
|
|
|
before { sign_in(moderator) }
|
|
|
|
|
|
|
|
include_examples "theme previews inaccessible"
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when logged in as a non-staff user" do
|
|
|
|
before { sign_in(user) }
|
|
|
|
|
|
|
|
include_examples "theme previews inaccessible"
|
2019-04-11 13:56:43 +08:00
|
|
|
end
|
|
|
|
end
|
2019-05-03 09:43:54 +08:00
|
|
|
|
2019-06-22 01:49:14 +08:00
|
|
|
describe "#update_single_setting" do
|
2024-03-11 08:42:12 +08:00
|
|
|
fab!(:theme)
|
2019-06-22 01:49:14 +08:00
|
|
|
|
|
|
|
before do
|
|
|
|
theme.set_field(target: :settings, name: :yaml, value: "bg: red")
|
|
|
|
theme.save!
|
|
|
|
end
|
|
|
|
|
2022-11-03 11:42:44 +08:00
|
|
|
context "when logged in as an admin" do
|
|
|
|
before { sign_in(admin) }
|
|
|
|
|
|
|
|
it "should update a theme setting" do
|
|
|
|
put "/admin/themes/#{theme.id}/setting.json", params: { name: "bg", value: "green" }
|
|
|
|
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
expect(response.parsed_body["bg"]).to eq("green")
|
|
|
|
|
|
|
|
theme.reload
|
|
|
|
expect(theme.cached_settings[:bg]).to eq("green")
|
|
|
|
user_history = UserHistory.last
|
|
|
|
|
|
|
|
expect(user_history.action).to eq(UserHistory.actions[:change_theme_setting])
|
|
|
|
end
|
|
|
|
|
2024-04-02 11:55:51 +08:00
|
|
|
it "should return the right error when value used to update a theme setting of `objects` typed is invalid" do
|
|
|
|
field =
|
|
|
|
theme.set_field(
|
|
|
|
target: :settings,
|
|
|
|
name: "yaml",
|
|
|
|
value: File.read("#{Rails.root}/spec/fixtures/theme_settings/objects_settings.yaml"),
|
|
|
|
)
|
|
|
|
|
|
|
|
theme.save!
|
|
|
|
|
|
|
|
put "/admin/themes/#{theme.id}/setting.json",
|
|
|
|
params: {
|
|
|
|
name: "objects_setting",
|
|
|
|
value: [
|
|
|
|
{ name: "new_section", links: [{ name: "a" * 21, url: "https://some.url.com" }] },
|
|
|
|
].to_json,
|
|
|
|
}
|
|
|
|
|
|
|
|
expect(response.status).to eq(422)
|
|
|
|
|
|
|
|
expect(response.parsed_body["errors"]).to eq(
|
|
|
|
["The property at JSON Pointer '/0/links/0/name' must be at most 20 characters long."],
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2024-03-11 08:42:12 +08:00
|
|
|
it "should be able to update a theme setting of `objects` typed" do
|
|
|
|
field =
|
|
|
|
theme.set_field(
|
|
|
|
target: :settings,
|
|
|
|
name: "yaml",
|
|
|
|
value: File.read("#{Rails.root}/spec/fixtures/theme_settings/objects_settings.yaml"),
|
|
|
|
)
|
|
|
|
|
|
|
|
theme.save!
|
|
|
|
|
|
|
|
put "/admin/themes/#{theme.id}/setting.json",
|
|
|
|
params: {
|
|
|
|
name: "objects_setting",
|
|
|
|
value: [
|
|
|
|
{ name: "new_section", links: [{ name: "new link", url: "https://some.url.com" }] },
|
|
|
|
].to_json,
|
|
|
|
}
|
|
|
|
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
|
|
|
|
expect(theme.settings[:objects_setting].value).to eq(
|
|
|
|
[
|
|
|
|
{
|
|
|
|
"name" => "new_section",
|
|
|
|
"links" => [{ "name" => "new link", "url" => "https://some.url.com" }],
|
|
|
|
},
|
|
|
|
],
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2022-11-03 11:42:44 +08:00
|
|
|
it "should clear a theme setting" do
|
|
|
|
put "/admin/themes/#{theme.id}/setting.json", params: { name: "bg" }
|
|
|
|
theme.reload
|
2019-06-22 01:49:14 +08:00
|
|
|
|
2022-11-03 11:42:44 +08:00
|
|
|
expect(response.status).to eq(200)
|
|
|
|
expect(theme.cached_settings[:bg]).to eq("")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
shared_examples "theme update not allowed" do
|
|
|
|
it "prevents updates with a 404 response" do
|
|
|
|
put "/admin/themes/#{theme.id}/setting.json", params: { name: "bg", value: "green" }
|
|
|
|
|
|
|
|
expect(response.status).to eq(404)
|
|
|
|
expect(response.parsed_body["errors"]).to include(I18n.t("not_found"))
|
|
|
|
|
|
|
|
theme.reload
|
|
|
|
expect(theme.cached_settings[:bg]).to eq("red")
|
|
|
|
end
|
|
|
|
end
|
2019-06-22 01:49:14 +08:00
|
|
|
|
2022-11-03 11:42:44 +08:00
|
|
|
context "when logged in as a moderator" do
|
|
|
|
before { sign_in(moderator) }
|
2019-06-22 01:49:14 +08:00
|
|
|
|
2022-11-03 11:42:44 +08:00
|
|
|
include_examples "theme update not allowed"
|
2019-06-22 01:49:14 +08:00
|
|
|
end
|
|
|
|
|
2022-11-03 11:42:44 +08:00
|
|
|
context "when logged in as a non-staff user" do
|
|
|
|
before { sign_in(user) }
|
2019-06-22 01:49:14 +08:00
|
|
|
|
2022-11-03 11:42:44 +08:00
|
|
|
include_examples "theme update not allowed"
|
2019-06-22 01:49:14 +08:00
|
|
|
end
|
|
|
|
end
|
2023-10-09 05:35:53 +08:00
|
|
|
|
2024-03-19 00:00:28 +08:00
|
|
|
describe "#update_translations" do
|
|
|
|
fab!(:theme)
|
|
|
|
|
|
|
|
before do
|
|
|
|
theme.set_field(
|
|
|
|
target: :translations,
|
|
|
|
name: :en,
|
|
|
|
value: { en: { group: { hello: "Hello there!" } } }.deep_stringify_keys.to_yaml,
|
|
|
|
)
|
|
|
|
theme.set_field(
|
|
|
|
target: :translations,
|
|
|
|
name: :fr,
|
|
|
|
value: { fr: { group: { hello: "Bonjour Mes Amis!" } } }.deep_stringify_keys.to_yaml,
|
|
|
|
)
|
|
|
|
theme.save!
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when logged in as an admin" do
|
|
|
|
before { sign_in(admin) }
|
|
|
|
|
|
|
|
it "should update a theme translation" do
|
|
|
|
put "/admin/themes/#{theme.id}.json",
|
|
|
|
params: {
|
|
|
|
theme: {
|
|
|
|
translations: {
|
|
|
|
"group.hello" => "Hello there! updated",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
theme.reload.translations.map { |t| expect(t.value).to eq("Hello there! updated") }
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should update a theme translation with locale" do
|
|
|
|
put "/admin/themes/#{theme.id}.json",
|
|
|
|
params: {
|
|
|
|
theme: {
|
|
|
|
translations: {
|
|
|
|
"group.hello" => "Hello there! updated",
|
|
|
|
},
|
|
|
|
locale: "en",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
theme.reload.translations.map { |t| expect(t.value).to eq("Hello there! updated") }
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should fail update a theme translation when locale is wrong" do
|
|
|
|
put "/admin/themes/#{theme.id}.json",
|
|
|
|
params: {
|
|
|
|
theme: {
|
|
|
|
translations: {
|
|
|
|
"group.hello" => "Hello there! updated",
|
|
|
|
},
|
|
|
|
locale: "foo",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
expect(response.status).to eq(400)
|
|
|
|
expect(response.parsed_body["errors"]).to include(
|
|
|
|
I18n.t("invalid_params", message: :locale),
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should update other locale and do not change current one" do
|
|
|
|
put "/admin/themes/#{theme.id}.json",
|
|
|
|
params: {
|
|
|
|
theme: {
|
|
|
|
translations: {
|
|
|
|
"group.hello" => "Bonjour Mes Amis! updated",
|
|
|
|
},
|
|
|
|
locale: "fr",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
theme.reload.translations.map { |t| expect(t.value).to eq("Hello there!") }
|
|
|
|
|
|
|
|
get "/admin/themes/#{theme.id}/translations/fr.json"
|
|
|
|
translations = response.parsed_body["translations"]
|
|
|
|
expect(translations.first["value"]).to eq("Bonjour Mes Amis! updated")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
shared_examples "theme update not allowed" do
|
|
|
|
it "prevents updates with a 404 response" do
|
|
|
|
put "/admin/themes/#{theme.id}.json",
|
|
|
|
params: {
|
|
|
|
theme: {
|
|
|
|
translations: {
|
|
|
|
"group.hello" => "Bonjour Mes Amis! updated",
|
|
|
|
},
|
|
|
|
locale: "fr",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
expect(response.status).to eq(404)
|
|
|
|
expect(response.parsed_body["errors"]).to include(I18n.t("not_found"))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when logged in as a moderator" do
|
|
|
|
before { sign_in(moderator) }
|
|
|
|
|
|
|
|
include_examples "theme update not allowed"
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when logged in as a non-staff user" do
|
|
|
|
before { sign_in(user) }
|
|
|
|
|
|
|
|
include_examples "theme update not allowed"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "#get_translations" do
|
|
|
|
fab!(:theme)
|
|
|
|
|
|
|
|
before do
|
|
|
|
theme.set_field(
|
|
|
|
target: :translations,
|
|
|
|
name: :en,
|
|
|
|
value: { en: { group: { hello: "Hello there!" } } }.deep_stringify_keys.to_yaml,
|
|
|
|
)
|
|
|
|
theme.save!
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when logged in as an admin" do
|
|
|
|
before { sign_in(admin) }
|
|
|
|
|
|
|
|
it "get translations from theme" do
|
|
|
|
get "/admin/themes/#{theme.id}/translations/en.json"
|
|
|
|
translations = response.parsed_body["translations"]
|
|
|
|
expect(translations.first["value"]).to eq("Hello there!")
|
|
|
|
end
|
|
|
|
|
|
|
|
it "fail if get translations from theme with wrong locale" do
|
|
|
|
get "/admin/themes/#{theme.id}/translations/foo.json"
|
|
|
|
expect(response.status).to eq(400)
|
|
|
|
expect(response.parsed_body["errors"]).to include(
|
|
|
|
I18n.t("invalid_params", message: :locale),
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
shared_examples "get theme translations not allowed" do
|
|
|
|
it "prevents updates with a 404 response" do
|
|
|
|
get "/admin/themes/#{theme.id}/translations/en.json"
|
|
|
|
expect(response.status).to eq(404)
|
|
|
|
expect(response.parsed_body["errors"]).to include(I18n.t("not_found"))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when logged in as a moderator" do
|
|
|
|
before { sign_in(moderator) }
|
|
|
|
|
|
|
|
include_examples "get theme translations not allowed"
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when logged in as a non-staff user" do
|
|
|
|
before { sign_in(user) }
|
|
|
|
|
|
|
|
include_examples "get theme translations not allowed"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-10-09 05:35:53 +08:00
|
|
|
describe "#bulk_destroy" do
|
|
|
|
fab!(:theme) { Fabricate(:theme, name: "Awesome Theme") }
|
|
|
|
fab!(:theme_2) { Fabricate(:theme, name: "Another awesome Theme") }
|
|
|
|
let(:theme_ids) { [theme.id, theme_2.id] }
|
|
|
|
|
|
|
|
before { sign_in(admin) }
|
|
|
|
|
|
|
|
it "destroys all selected the themes" do
|
|
|
|
expect do
|
|
|
|
delete "/admin/themes/bulk_destroy.json", params: { theme_ids: theme_ids }
|
|
|
|
end.to change { Theme.count }.by(-2)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "logs the theme destroy action for each theme" do
|
|
|
|
StaffActionLogger.any_instance.expects(:log_theme_destroy).twice
|
|
|
|
delete "/admin/themes/bulk_destroy.json", params: { theme_ids: theme_ids }
|
|
|
|
end
|
|
|
|
end
|
2024-03-26 14:02:05 +08:00
|
|
|
|
|
|
|
describe "#objects_setting_metadata" do
|
|
|
|
fab!(:theme)
|
|
|
|
|
|
|
|
let(:theme_setting) do
|
|
|
|
yaml = File.read("#{Rails.root}/spec/fixtures/theme_settings/objects_settings.yaml")
|
|
|
|
field = theme.set_field(target: :settings, name: "yaml", value: yaml)
|
|
|
|
theme.save!
|
|
|
|
theme.settings
|
|
|
|
end
|
|
|
|
|
|
|
|
it "returns 404 if user is not an admin" do
|
|
|
|
get "/admin/themes/#{theme.id}/objects_setting_metadata/objects_with_categories.json"
|
|
|
|
|
|
|
|
expect(response.status).to eq(404)
|
|
|
|
|
|
|
|
sign_in(user)
|
|
|
|
|
|
|
|
get "/admin/themes/#{theme.id}/objects_setting_metadata/objects_with_categories.json"
|
|
|
|
|
|
|
|
expect(response.status).to eq(404)
|
|
|
|
|
|
|
|
sign_in(moderator)
|
|
|
|
|
|
|
|
get "/admin/themes/#{theme.id}/objects_setting_metadata/objects_with_categories.json"
|
|
|
|
|
|
|
|
expect(response.status).to eq(404)
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when user is an admin" do
|
|
|
|
before { sign_in(admin) }
|
|
|
|
|
|
|
|
it "returns 400 if the `id` param is not the id of a valid theme" do
|
|
|
|
get "/admin/themes/some_invalid_id/objects_setting_metadata/objects_with_categories.json"
|
|
|
|
|
|
|
|
expect(response.status).to eq(400)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "returns 400 if the `setting_name` param does not match a valid setting" do
|
|
|
|
get "/admin/themes/#{theme.id}/objects_setting_metadata/some_invalid_setting_name.json"
|
|
|
|
|
|
|
|
expect(response.status).to eq(400)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "returns 200 with the right `property_descriptions` attributes" do
|
|
|
|
theme.set_field(
|
|
|
|
target: :translations,
|
|
|
|
name: "en",
|
|
|
|
value: File.read("#{Rails.root}/spec/fixtures/theme_locales/objects_settings/en.yaml"),
|
|
|
|
)
|
|
|
|
|
|
|
|
theme.save!
|
|
|
|
|
|
|
|
theme_setting
|
|
|
|
|
|
|
|
get "/admin/themes/#{theme.id}/objects_setting_metadata/objects_setting.json"
|
|
|
|
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
|
|
|
|
expect(response.parsed_body["property_descriptions"]).to eq(
|
|
|
|
{
|
2024-03-28 10:53:51 +08:00
|
|
|
"links.name.description" => "Name of the link",
|
|
|
|
"links.name.label" => "Name",
|
|
|
|
"links.url.description" => "URL of the link",
|
|
|
|
"links.url.label" => "URL",
|
|
|
|
"name.description" => "Section Name",
|
|
|
|
"name.label" => "Name",
|
2024-03-26 14:02:05 +08:00
|
|
|
},
|
|
|
|
)
|
|
|
|
end
|
2024-03-27 10:54:30 +08:00
|
|
|
|
|
|
|
it "returns 200 with the right `categories` attribute for a theme setting with categories propertoes" do
|
|
|
|
category_1 = Fabricate(:category)
|
|
|
|
category_2 = Fabricate(:category)
|
|
|
|
category_3 = Fabricate(:category)
|
|
|
|
|
|
|
|
theme_setting[:objects_with_categories].value = [
|
|
|
|
{
|
|
|
|
"category_ids" => [category_1.id, category_2.id],
|
|
|
|
"child_categories" => [{ "category_ids" => [category_3.id] }],
|
|
|
|
},
|
|
|
|
]
|
|
|
|
|
|
|
|
get "/admin/themes/#{theme.id}/objects_setting_metadata/objects_with_categories.json"
|
|
|
|
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
|
|
|
|
categories = response.parsed_body["categories"]
|
|
|
|
|
|
|
|
expect(categories.keys.map(&:to_i)).to contain_exactly(
|
|
|
|
category_1.id,
|
|
|
|
category_2.id,
|
|
|
|
category_3.id,
|
|
|
|
)
|
|
|
|
|
|
|
|
expect(categories[category_1.id.to_s]["name"]).to eq(category_1.name)
|
|
|
|
expect(categories[category_2.id.to_s]["name"]).to eq(category_2.name)
|
|
|
|
expect(categories[category_3.id.to_s]["name"]).to eq(category_3.name)
|
|
|
|
end
|
2024-03-26 14:02:05 +08:00
|
|
|
end
|
|
|
|
end
|
2024-04-24 15:34:57 +08:00
|
|
|
|
|
|
|
describe "#schema" do
|
|
|
|
fab!(:theme)
|
|
|
|
fab!(:theme_component) { Fabricate(:theme, component: true) }
|
|
|
|
|
|
|
|
before { sign_in(admin) }
|
|
|
|
|
|
|
|
it "returns 200 when customizing a theme's setting of objects type" do
|
|
|
|
get "/admin/customize/themes/#{theme.id}/schema/some_setting_name"
|
|
|
|
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "returns 200 when customizing a theme component's setting of objects type" do
|
|
|
|
get "/admin/customize/components/#{theme_component.id}/schema/some_setting_name"
|
|
|
|
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
end
|
|
|
|
end
|
2018-06-11 12:54:16 +08:00
|
|
|
end
|