-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #28 from OpenSourcePolitics/develop
Bump master
- Loading branch information
Showing
8 changed files
with
148 additions
and
153 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
# frozen_string_literal: true | ||
|
||
namespace :decidim do | ||
namespace :repair do | ||
desc "Check for nicknames that doesn't respect valid format and update them, if needed force update with REPAIR_NICKNAME_FORCE=1" | ||
# TODO: Extract to a lib | ||
task nickname: :environment do | ||
logger = Logger.new($stdout) | ||
logger.info("[decidim:repair:nickname] :: Checking all nicknames...") | ||
invalid_users = Decidim::User.where.not("nickname ~* ?", "^[\\w-]+$") | ||
|
||
if invalid_users.blank? | ||
logger.info("[decidim:repair:nickname] :: All nicknames seems to be valid") | ||
logger.info("[decidim:repair:nickname] :: Operation terminated") | ||
else | ||
logger.info("[decidim:repair:nickname] :: Found #{invalid_users.count} invalids nicknames") | ||
logger.info("[decidim:repair:nickname] :: Invalid user IDs : [#{invalid_users.map(&:id).join(", ")}]") | ||
|
||
updated_users = [] | ||
invalid_users.each do |user| | ||
chars = [] | ||
|
||
user.nickname.codepoints.each do |ascii_code| | ||
char = ascii_to_valid_char(ascii_code) | ||
chars << char if char.present? | ||
end | ||
|
||
new_nickname = chars.join.downcase | ||
logger.info("[decidim:repair:nickname] :: User (##{user.id}) renaming nickname from '#{user.nickname}' to '#{new_nickname}'") | ||
user.nickname = new_nickname | ||
|
||
updated_users << user | ||
end | ||
|
||
if ask_for_permission(logger, updated_users.count) | ||
logger.info("[decidim:repair:nickname] :: Updating users...") | ||
updated_users.each do |user| | ||
user.save! | ||
logger.info("[decidim:repair:nickname] :: User (##{user.id}) successfully updated") | ||
rescue ActiveRecord::RecordInvalid => e | ||
logger.error("[decidim:repair:nickname] :: User (##{user.id}) failed to update : #{e.message}") | ||
logger.error("[decidim:repair:nickname] :: Trying with a another nickname: #{user.nickname}-#{user.id}") | ||
user.nickname = "#{user.nickname}#{user.id}" | ||
user.save! | ||
rescue StandardError => e | ||
logger.error("[decidim:repair:nickname] :: User (##{user.id}) an error occured") | ||
logger.error("[decidim:repair:nickname] :: #{e}") | ||
end | ||
else | ||
logger.info("[decidim:repair:nickname] :: Operation terminated") | ||
end | ||
end | ||
end | ||
end | ||
end | ||
|
||
def ask_for_permission(logger, users_count) | ||
logger.info("[decidim:repair:nickname] :: Do you want to update these #{users_count} users ?") | ||
return true if ENV["REPAIR_NICKNAME_FORCE"] == "1" | ||
|
||
logger.info("[decidim:repair:nickname] :: prepend REPAIR_NICKNAME_FORCE=1 to your command to update") | ||
|
||
false | ||
end | ||
|
||
def ascii_to_valid_char(id) | ||
letters = ("A".."Z").to_a.join("").codepoints | ||
letters += ("a".."z").to_a.join("").codepoints | ||
digits = ("0".."9").to_a.join("").codepoints | ||
special_chars = %w(- _).join("").codepoints | ||
|
||
valid_ascii_code = letters + digits + special_chars | ||
|
||
valid_ascii_code.include?(id) ? id.chr : "" | ||
end |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
# frozen_string_literal: true | ||
|
||
require "spec_helper" | ||
|
||
describe "rake decidim:repair:nickname", type: :task do | ||
let!(:organization) { create(:organization) } | ||
let(:task_cmd) { :"decidim:repair:nickname" } | ||
|
||
let!(:valid_user) { create(:user, nickname: "Azerty_Uiop123", organization: organization) } | ||
let(:invalid_user_1) { build(:user, nickname: "Foo bar", organization: organization) } | ||
let(:invalid_user_2) { build(:user, nickname: "Foo M. bar", organization: organization) } | ||
let(:invalid_user_3) { build(:user, nickname: "Foo-Bar_fooo$", organization: organization) } | ||
let(:invalid_user_4) { build(:user, nickname: "foo.bar.foo", organization: organization) } | ||
let(:invalid_user_5) { build(:user, nickname: ".foobar.foo_bar.", organization: organization) } | ||
let(:invalid_user_6) { build(:user, nickname: "Foo bar", organization: organization) } | ||
let(:invalid_user_7) { build(:user, nickname: "Foo bar", organization: organization) } | ||
|
||
let(:repair_nick_name_force) { "1" } | ||
|
||
let(:environment) do | ||
{ | ||
"REPAIR_NICKNAME_FORCE" => repair_nick_name_force | ||
} | ||
end | ||
|
||
before do | ||
invalid_user_1.save(validate: false) | ||
invalid_user_2.save(validate: false) | ||
invalid_user_3.save(validate: false) | ||
invalid_user_4.save(validate: false) | ||
invalid_user_5.save(validate: false) | ||
invalid_user_6.save(validate: false) | ||
invalid_user_7.save(validate: false) | ||
|
||
Rake::Task[task_cmd].reenable | ||
end | ||
|
||
it "updates invalid nicknames" do | ||
with_modified_env(environment) do | ||
Rake::Task[task_cmd].invoke | ||
|
||
expect(invalid_user_1.reload.nickname).to eq("foobar") | ||
expect(invalid_user_2.reload.nickname).to eq("foombar") | ||
expect(invalid_user_3.reload.nickname).to eq("foo-bar_fooo") | ||
expect(invalid_user_4.reload.nickname).to eq("foobarfoo") | ||
expect(invalid_user_5.reload.nickname).to eq("foobarfoo_bar") | ||
expect(invalid_user_6.reload.nickname).to eq("foobar#{invalid_user_6.id}") | ||
expect(invalid_user_7.reload.nickname).to eq("foobar#{invalid_user_7.id}") | ||
expect(valid_user.reload.nickname).to eq("Azerty_Uiop123") | ||
end | ||
end | ||
|
||
context "when user refuses update" do | ||
let(:repair_nick_name_force) { "0" } | ||
|
||
it "updates invalid nicknames" do | ||
with_modified_env(environment) do | ||
Rake::Task[task_cmd].invoke | ||
|
||
expect(invalid_user_1.reload.nickname).to eq("Foo bar") | ||
expect(invalid_user_2.reload.nickname).to eq("Foo M. bar") | ||
expect(invalid_user_3.reload.nickname).to eq("Foo-Bar_fooo$") | ||
expect(invalid_user_4.reload.nickname).to eq("foo.bar.foo") | ||
expect(invalid_user_5.reload.nickname).to eq(".foobar.foo_bar.") | ||
expect(invalid_user_6.reload.nickname).to eq("Foo bar") | ||
expect(invalid_user_7.reload.nickname).to eq("Foo bar") | ||
expect(valid_user.reload.nickname).to eq("Azerty_Uiop123") | ||
end | ||
end | ||
end | ||
end |
This file was deleted.
Oops, something went wrong.