Validate enode url when blank

Fixes: #7333

Validates enode url if blank as well, so `Save` button is disabled.
Handles as well if for any reason invalid values are passed.

Signed-off-by: Andrea Maria Piana <andrea.maria.piana@gmail.com>
This commit is contained in:
Andrea Maria Piana 2020-04-03 09:18:10 +02:00
parent 5eea4ba218
commit e7a213fe95
No known key found for this signature in database
GPG Key ID: AA6CCA6DE0E06424
4 changed files with 42 additions and 35 deletions

View File

@ -1055,9 +1055,13 @@
(fx/defn upsert (fx/defn upsert
[{{:mailserver.edit/keys [mailserver] :keys [multiaccount] :as db} :db [{{:mailserver.edit/keys [mailserver] :keys [multiaccount] :as db} :db
random-id-generator :random-id-generator :as cofx}] random-id-generator :random-id-generator :as cofx}]
(let [{:keys [name url id]} mailserver (let [{:keys [name url id]} mailserver
current-fleet (node/current-fleet-key db) current-fleet (node/current-fleet-key db)]
mailserver (build (when (and (not (string/blank? (:value name)))
(valid-enode-url? (:value url)))
(let [mailserver (build
(or (:value id) (or (:value id)
(keyword (string/replace (random-id-generator) "-" ""))) (keyword (string/replace (random-id-generator) "-" "")))
(:value name) (:value name)
@ -1078,7 +1082,7 @@
[:multiaccounts.logout.ui/logout-confirmed])) [:multiaccounts.logout.ui/logout-confirmed]))
(log/debug "saved mailserver" id "successfuly")) (log/debug "saved mailserver" id "successfuly"))
:on-failure #(log/error "failed to save mailserver" id %)}] :on-failure #(log/error "failed to save mailserver" id %)}]
:dispatch [:navigate-back]})) :dispatch [:navigate-back]}))))
(defn can-delete? (defn can-delete?
[db id] [db id]

View File

@ -48,7 +48,9 @@
(let [url (get-in mailserver [:url :value]) (let [url (get-in mailserver [:url :value])
id (get-in mailserver [:id :value]) id (get-in mailserver [:id :value])
name (get-in mailserver [:name :value]) name (get-in mailserver [:name :value])
is-valid? (empty? validation-errors) is-valid? (and (not (string/blank? url))
(not (string/blank? name))
(empty? validation-errors))
invalid-url? (contains? validation-errors :url)] invalid-url? (contains? validation-errors :url)]
[react/view components.styles/flex [react/view components.styles/flex
[react/keyboard-avoiding-view components.styles/flex [react/keyboard-avoiding-view components.styles/flex

View File

@ -671,10 +671,9 @@ class TestProfileMultipleDevice(MultipleDeviceTestCase):
profile_1.just_fyi('add custom mailserver (check address/name validation) and connect to it') profile_1.just_fyi('add custom mailserver (check address/name validation) and connect to it')
profile_1.plus_button.click() profile_1.plus_button.click()
server_name = 'test' server_name = 'test'
# TODO: blocked due to issue 7333 profile_1.save_button.click()
# profile_1.save_button.click() if profile_1.element_by_text(mailserver_ams).is_element_displayed():
# if profile_1.element_by_text(mailserver_staging_ams_1).is_element_displayed(): self.errors.append('Could add custom mailserver with empty address and name')
# self.errors.append('Could add custom mailserver with empty address and name')
profile_1.specify_name_input.set_value(server_name) profile_1.specify_name_input.set_value(server_name)
profile_1.mail_server_address_input.set_value(mailserver_address[:-3]) profile_1.mail_server_address_input.set_value(mailserver_address[:-3])
profile_1.save_button.click() profile_1.save_button.click()

View File

@ -1,5 +1,6 @@
(ns status-im.test.mailserver.core (ns status-im.test.mailserver.core
(:require [cljs.test :refer-macros [deftest is testing]] (:require [cljs.test :refer-macros [deftest is testing]]
[clojure.string :as string]
[status-im.ethereum.json-rpc :as json-rpc] [status-im.ethereum.json-rpc :as json-rpc]
[status-im.transport.utils :as utils] [status-im.transport.utils :as utils]
[status-im.mailserver.core :as mailserver] [status-im.mailserver.core :as mailserver]
@ -287,14 +288,14 @@
(testing "new mailserver" (testing "new mailserver"
(let [cofx {:random-id-generator (constantly "random-id") (let [cofx {:random-id-generator (constantly "random-id")
:db {:mailserver.edit/mailserver {:name {:value "test-name"} :db {:mailserver.edit/mailserver {:name {:value "test-name"}
:url {:value "enode://test-id:test-password@url:port"}} :url {:value valid-enode-url}}
:mailserver/mailservers {}}} :mailserver/mailservers {}}}
actual (mailserver/upsert cofx)] actual (mailserver/upsert cofx)]
(testing "it adds the enode to mailserver/mailservers" (testing "it adds the enode to mailserver/mailservers"
(is (= {:eth.staging {:randomid {:password "test-password" (is (= {:eth.staging {:randomid {:password password
:address "enode://test-id@url:port" :address valid-enode-address
:name "test-name" :name "test-name"
:id :randomid :id :randomid
:user-defined true}}} :user-defined true}}}
@ -305,21 +306,22 @@
(testing "it stores it in the db" (testing "it stores it in the db"
(is (= 1 (count (::json-rpc/call actual))))))) (is (= 1 (count (::json-rpc/call actual)))))))
(testing "existing mailserver" (testing "existing mailserver"
(let [cofx {:random-id-generator (constantly "random-id") (let [new-enode-url (string/replace valid-enode-url "password" "new-password")
cofx {:random-id-generator (constantly "random-id")
:db {:mailserver.edit/mailserver {:id {:value :a} :db {:mailserver.edit/mailserver {:id {:value :a}
:name {:value "new-name"} :name {:value "new-name"}
:url {:value "enode://new-id:new-password@url:port"}} :url {:value new-enode-url}}
:mailserver/mailservers {:eth.staging {:a {:id :a :mailserver/mailservers {:eth.staging {:a {:id :a
:name "old-name" :name "old-name"
:address "enode://old-id:old-password@url:port"}}}}} :address valid-enode-address}}}}}
actual (mailserver/upsert cofx)] actual (mailserver/upsert cofx)]
(testing "it navigates back" (testing "it navigates back"
(is (= [:navigate-back] (is (= [:navigate-back]
(:dispatch actual)))) (:dispatch actual))))
(testing "it updates the enode to mailserver/mailservers" (testing "it updates the enode to mailserver/mailservers"
(is (= {:eth.staging {:a {:password "new-password" (is (= {:eth.staging {:a {:password "new-password"
:address "enode://new-id@url:port" :address valid-enode-address
:name "new-name" :name "new-name"
:id :a :id :a
:user-defined true}}} :user-defined true}}}