From c619d39bde95ee8ea58fa19983f0ccfe4b99cc74 Mon Sep 17 00:00:00 2001 From: Colin Darie Date: Tue, 3 Jul 2018 17:38:06 +0200 Subject: [PATCH] Adapt new check form to predefined kind, more i18n --- app/controllers/application_controller.rb | 10 ++++ app/controllers/checks_controller.rb | 12 +++-- app/views/checks/_form.html.erb | 7 ++- app/views/checks/index.html.erb | 4 +- app/views/checks/new.html.erb | 2 +- app/views/shared/_navbar.html.erb | 7 ++- config/locales/en.yml | 22 +++++++++ config/locales/fr.yml | 36 +++++++++++++- test/controllers/checks_controller_test.rb | 33 +++++++++++-- test/system/checks_test.rb | 57 ++++++++++++++++------ 10 files changed, 161 insertions(+), 29 deletions(-) diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 5127212..86a4a95 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -20,4 +20,14 @@ class ApplicationController < ActionController::Base def set_locale I18n.locale = current_user.try(:locale) || I18n.default_locale end + + def not_found + fail ActionController::RoutingError, "Not Found" + rescue StandardError + render_404 + end + + def render_404 + render file: "#{Rails.root}/public/404", status: :not_found + end end diff --git a/app/controllers/checks_controller.rb b/app/controllers/checks_controller.rb index b2566a6..d9fc444 100644 --- a/app/controllers/checks_controller.rb +++ b/app/controllers/checks_controller.rb @@ -10,8 +10,14 @@ class ChecksController < ApplicationController def new @check = Check.new - build_empty_notification authorize @check + + if params[:kind].present? + return not_found unless Check.kinds.key?(params[:kind]) + @check.kind = params[:kind] + end + + build_empty_notification end def create @@ -20,10 +26,10 @@ class ChecksController < ApplicationController authorize @check if @check.save - flash[:notice] = "Your check has been saved." + flash[:notice] = t(".saved") redirect_to checks_path else - flash.now[:alert] = "An error occured." + flash.now[:alert] = t(".invalid") render :new end end diff --git a/app/views/checks/_form.html.erb b/app/views/checks/_form.html.erb index e4dd82f..959d5b0 100644 --- a/app/views/checks/_form.html.erb +++ b/app/views/checks/_form.html.erb @@ -1,8 +1,11 @@ <%= simple_form_for(check) do |f| %> - <%= f.input :domain, autofocus: true, input_html: { autocapitalize: :none, autocorrect: :off } %> + <%= f.input :domain, + autofocus: true, + input_html: { autocapitalize: :none, autocorrect: :off }, + label: t(".#{check.kind || "generic" }.domain") %> <% if check.new_record? %> - <%= f.input :kind, as: :radio_buttons, collection: Check.kinds.keys if check.new_record? %> + <%= f.input :kind, as: check.kind.present? ? :hidden : :radio_buttons, collection: Check.kinds.keys %> <% end %> <%= f.input :comment %> diff --git a/app/views/checks/index.html.erb b/app/views/checks/index.html.erb index d425008..f823b6f 100644 --- a/app/views/checks/index.html.erb +++ b/app/views/checks/index.html.erb @@ -3,10 +3,10 @@
<% if @checks.empty? %>
- <%= t(".no_check_yet_html", new_domain_path: new_check_path, new_ssl_path: new_check_path) %> + <%= t(".no_check_yet_html", new_domain_path: new_check_path(kind: :domain), new_ssl_path: new_check_path(kind: :ssl)) %>
<% else %> -

List of your checks

+

<%= t(".title") %>

<%= render "table", checks: @checks %> <% end %>
diff --git a/app/views/checks/new.html.erb b/app/views/checks/new.html.erb index 97b34b8..3a662a7 100644 --- a/app/views/checks/new.html.erb +++ b/app/views/checks/new.html.erb @@ -1,7 +1,7 @@
-

Create a new check

+

<%= t(".#{@check.kind}.title") %>

<%= render "form", check: @check %>
diff --git a/app/views/shared/_navbar.html.erb b/app/views/shared/_navbar.html.erb index 02f3c53..fccb7eb 100644 --- a/app/views/shared/_navbar.html.erb +++ b/app/views/shared/_navbar.html.erb @@ -8,10 +8,13 @@ diff --git a/config/locales/en.yml b/config/locales/en.yml index 9a34ad9..5bde33f 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -53,6 +53,9 @@ en: en: English fr: French navbar: + my_checks: "My checks" + new_domain_check: "New domain check" + new_ssl_check: "New SSL check" sign_up: "Sign up" sign_in: "Sign in" sign_out: "Sign out" @@ -60,11 +63,30 @@ en: checks: index: + title: List of your checks no_check_yet_html: | You have not set up a check yet. Please add a domain or a ssl ! + + new: + title: New check + domain: + title: New domain check + ssl: + title: New SSL check + + create: + saved: "Your check has been saved." + invalid: "Please check the form." + form: + generic: + domain: Domain + domain: + domain: Domain name + ssl: + domain: Hostname notifications_hint: | Receive notifications to warn you when our system detects that the expiration date is coming. The time is set in number of days. diff --git a/config/locales/fr.yml b/config/locales/fr.yml index ed5926f..2477a7f 100644 --- a/config/locales/fr.yml +++ b/config/locales/fr.yml @@ -2,8 +2,15 @@ fr: activerecord: attributes: check: + domain: "Domaine" + comment: "Commentaire" + vendor: "Fournisseur" + kind: Type domain_created_at: "Date de création" domain_updated_at: "Date de modification" + notification: + interval: Délai + recipient: Destinataire user: tos_accepted: "Conditions d'utilisation" notifications_enabled: "Notifications activées" @@ -11,6 +18,11 @@ fr: models: check: past: "ne peut être dans le futur" + helpers: + submit: + check: + create: "Créer" + update: "Valider" time: am: am @@ -61,6 +73,9 @@ fr: en: Anglais fr: Français navbar: + my_checks: "Mes vérifications" + new_domain_check: "Nouveau nom de domaine" + new_ssl_check: "Nouveau certificat SSL" sign_up: "Enregistrement" sign_in: "Connexion" sign_out: "Déconnexion" @@ -68,11 +83,30 @@ fr: checks: index: + title: "Liste de vos vérifications" no_check_yet_html: | Vous n'avez pas encore créé de vérification. Vous pouvez en ajouter pour un domaine ou un SSL ! + + new: + title: Nouvelle vérification + domain: + title: Nouvelle vérification d'un nom de domaine + ssl: + title: Nouvelle vérification d'un certificat SSL + + create: + saved: La vérification est enregistrée. + invalid: Veuillez vérifier le formulaire. + form: + generic: + domain: Domaine + domain: + domain: Nom de domaine + ssl: + domain: Nom d'hôte notifications_hint: | Recevez des notifications pour vous avertir lorsque notre système détecte - que la date d'expiration approche. Le délais est indiqué ennombre de jours. + que la date d'expiration approche. Le délai est indiqué ennombre de jours. diff --git a/test/controllers/checks_controller_test.rb b/test/controllers/checks_controller_test.rb index 77435b7..98035cf 100644 --- a/test/controllers/checks_controller_test.rb +++ b/test/controllers/checks_controller_test.rb @@ -1,7 +1,34 @@ require "test_helper" class ChecksControllerTest < ActionDispatch::IntegrationTest - # test "the truth" do - # assert true - # end + setup do + @user = create(:user) + login_as(@user) + end + + test "no logged users are redirected to signin form" do + logout + get new_check_path + assert_redirected_to new_user_session_path + end + + test "new without kind does not trigger an error" do + get new_check_path + assert_response :success + end + + test "new with kind domain does not trigger an error" do + get new_check_path(kind: :domain) + assert_response :success + end + + test "new with kind ssl does not trigger an error" do + get new_check_path(kind: :ssl) + assert_response :success + end + + test "new with an invalid kind returns an error" do + get new_check_path(kind: :invalid) + assert_response :not_found + end end diff --git a/test/system/checks_test.rb b/test/system/checks_test.rb index 1fefa51..e7be944 100644 --- a/test/system/checks_test.rb +++ b/test/system/checks_test.rb @@ -8,29 +8,28 @@ class ChecksTest < ApplicationSystemTestCase @check = create(:check, :with_notifications, user: @user) end - test "create a check and a notification" do + test "create a check and a notification without kind" do visit new_check_path - domain = "domain-test.fr" - fill_in("check[domain]", with: domain) choose "domain" - recipient = "recipient@example.org" - fill_in("check[notifications_attributes][0][recipient]", with: recipient) - fill_in("check[notifications_attributes][0][interval]", with: 30) + fill_and_valid_new_check + end - click_button + test "create a predefined domain check" do + visit new_check_path(kind: :domain) - assert_equal checks_path, page.current_path + refute page.has_css? "domain[kind]" - assert page.has_css?(".alert-success") - assert page.has_content?(domain) + fill_and_valid_new_check + end - notification = Notification.last - assert_equal recipient, notification.recipient - assert_equal 30, notification.interval - assert notification.email? - assert notification.pending? + test "create a predefined ssl check" do + visit new_check_path(kind: :ssl) + + refute page.has_css? "domain[kind]" + + fill_and_valid_new_check end test "remove a notification" do @@ -83,4 +82,32 @@ class ChecksTest < ApplicationSystemTestCase assert notification.email? assert notification.pending? end + + private + + # rubocop:disable Metrics/AbcSize + # rubocop:disable Metrics/MethodLength + def fill_and_valid_new_check + domain = "domain-test.fr" + fill_in("check[domain]", with: domain) + + recipient = "recipient@example.org" + fill_in("check[notifications_attributes][0][recipient]", with: recipient) + fill_in("check[notifications_attributes][0][interval]", with: 30) + + click_button + + assert_equal checks_path, page.current_path + + assert page.has_css?(".alert-success") + assert page.has_content?(domain) + + notification = Notification.last + assert_equal recipient, notification.recipient + assert_equal 30, notification.interval + assert notification.email? + assert notification.pending? + end + # rubocop:enable Metrics/AbcSize + # rubocop:enable Metrics/MethodLength end