Skip to content

Commit

Permalink
Merge pull request #28 from OpenSourcePolitics/develop
Browse files Browse the repository at this point in the history
Bump master
  • Loading branch information
armandfardeau authored Jun 22, 2023
2 parents 337ccdc + 8d00e39 commit 2bbc528
Show file tree
Hide file tree
Showing 8 changed files with 148 additions and 153 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci_cd.yml
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ jobs:
namespace: ${{ vars.REGISTRY_NAMESPACE }}
password: ${{ secrets.TOKEN }}
image_name: ${{ vars.IMAGE_NAME }}
tag: "develop-${{ github.sha }}"
tag: "develop"
generate_release:
name: Generate release
needs: [ lint, tests, test_build ]
Expand Down
5 changes: 0 additions & 5 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,6 @@ en:
destroy:
error: Error
success: Success
admin:
participatory_space_private_users:
create:
error: Error
success: Success
amendments:
emendation:
announcement:
Expand Down
5 changes: 0 additions & 5 deletions config/locales/fr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,6 @@ fr:
destroy:
error: Votre compte a été supprimé avec succès.
success: Il y a eu un problème lors de la suppression de votre compte.
admin:
participatory_space_private_users:
create:
error: Erreur
success: Succès
amendments:
emendation:
announcement:
Expand Down
75 changes: 75 additions & 0 deletions lib/tasks/repair_data.rake
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
71 changes: 0 additions & 71 deletions lib/tasks/repare_data.rake

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ module Proposals

put :withdraw, params: params.merge(id: proposal.id)

expect(flash[:alert]).to eq("You are not authorized to perform this action")
expect(flash[:alert]).to eq("Unauthorized")
expect(response).to have_http_status(:found)
proposal.reload
expect(proposal.withdrawn?).to be false
Expand Down
71 changes: 71 additions & 0 deletions spec/lib/tasks/repair_data_spec.rb
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
70 changes: 0 additions & 70 deletions spec/lib/tasks/repare_data_spec.rb

This file was deleted.

0 comments on commit 2bbc528

Please sign in to comment.