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,30 +1055,34 @@
(fx/defn upsert
[{{:mailserver.edit/keys [mailserver] :keys [multiaccount] :as db} :db
random-id-generator :random-id-generator :as cofx}]
(let [{:keys [name url id]} mailserver
current-fleet (node/current-fleet-key db)
mailserver (build
(or (:value id)
(keyword (string/replace (random-id-generator) "-" "")))
(:value name)
(:value url))
current (connected? db (:id mailserver))]
{:db (-> db
(dissoc :mailserver.edit/mailserver)
(assoc-in [:mailserver/mailservers current-fleet (:id mailserver)]
mailserver))
::json-rpc/call
[{:method "mailservers_addMailserver"
:params [(mailserver->rpc mailserver current-fleet)]
:on-success (fn []
;; we naively logout if the user is connected to
;; the edited mailserver
(when current
(re-frame/dispatch
[:multiaccounts.logout.ui/logout-confirmed]))
(log/debug "saved mailserver" id "successfuly"))
:on-failure #(log/error "failed to save mailserver" id %)}]
:dispatch [:navigate-back]}))
current-fleet (node/current-fleet-key db)]
(when (and (not (string/blank? (:value name)))
(valid-enode-url? (:value url)))
(let [mailserver (build
(or (:value id)
(keyword (string/replace (random-id-generator) "-" "")))
(:value name)
(:value url))
current (connected? db (:id mailserver))]
{:db (-> db
(dissoc :mailserver.edit/mailserver)
(assoc-in [:mailserver/mailservers current-fleet (:id mailserver)]
mailserver))
::json-rpc/call
[{:method "mailservers_addMailserver"
:params [(mailserver->rpc mailserver current-fleet)]
:on-success (fn []
;; we naively logout if the user is connected to
;; the edited mailserver
(when current
(re-frame/dispatch
[:multiaccounts.logout.ui/logout-confirmed]))
(log/debug "saved mailserver" id "successfuly"))
:on-failure #(log/error "failed to save mailserver" id %)}]
:dispatch [:navigate-back]}))))
(defn can-delete?
[db id]

View File

@ -48,7 +48,9 @@
(let [url (get-in mailserver [:url :value])
id (get-in mailserver [:id :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)]
[react/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.plus_button.click()
server_name = 'test'
# TODO: blocked due to issue 7333
# profile_1.save_button.click()
# 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')
profile_1.save_button.click()
if profile_1.element_by_text(mailserver_ams).is_element_displayed():
self.errors.append('Could add custom mailserver with empty address and name')
profile_1.specify_name_input.set_value(server_name)
profile_1.mail_server_address_input.set_value(mailserver_address[:-3])
profile_1.save_button.click()

View File

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