Skip to content

Commit

Permalink
Improved login behavior (#61)
Browse files Browse the repository at this point in the history
* Correctly identify if we're logging in with the same username

Looks like this was broken with the SSO login changes.

* Provide meaningful message when there are no orgs/spaces found during login
  • Loading branch information
Chuck Gala authored and bruceadams committed Aug 26, 2016
1 parent 08145b7 commit 13cc2c9
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/kale/login.clj
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@
;; This assumes the user hasn't changed the name of their local org
default (or (cf/find-entity orgs username)
(first orgs))]
(when (nil? default)
(fail (get-msg :no-orgs-in-region)))
(or attempt
(do (if org-name
(println (get-msg :alternative-org
Expand All @@ -97,6 +99,8 @@
(let [spaces (cf/get-spaces cf-auth org-guid)
attempt (cf/find-entity spaces space-name)
default (first spaces)]
(when (nil? default)
(fail (get-msg :no-spaces-in-org)))
(or attempt
(do (if space-name
(println (get-msg :alternative-space
Expand Down Expand Up @@ -147,8 +151,8 @@
(get-password))
prev-endpoint? (= endpoint (-> state :login :endpoint))
prev-username? (if (nil? (options :sso))
(= username (-> state :login :username)
true))
(= username (-> state :login :username))
true)

{:keys [access_token]} (if (some? (options :sso))
(cf/get-oauth-tokens-sso endpoint)
Expand Down
4 changes: 4 additions & 0 deletions src/kale/messages/en.clj
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,10 @@ these services will be provisioned using the 'standard' plan.
:using-password "Using password from environment variable 'KALE_PASSWORD'"
:prompt-password "Password? "

:no-orgs-in-region
"Unable to find any available orgs for the given endpoint."
:no-spaces-in-org
"Unable to find any available spaces for the org."
:alternative-org "Unable to find org '%s', using org '%s' instead"
:using-org "Using org '%s'"
:alternative-space "Unable to find space '%s', using space '%s' instead"
Expand Down
47 changes: 47 additions & 0 deletions test/kale/login_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,13 @@
"bad-org"
"redshirt"))))))))

(t/deftest no-available-orgs
(with-redefs [cf/get-organizations (fn [_] [])]
(t/is (thrown+-with-msg?
[:type :kale.common/fail]
#"Unable to find any available orgs for the given endpoint."
(sut/attempt-to-get-org cf-auth "bad-org" "redshirt")))))

(t/deftest get-existing-space
(with-fake-routes-in-isolation
{(cf-url "/v2/organizations/ORG_GUID/spaces")
Expand Down Expand Up @@ -174,6 +181,13 @@
(sut/attempt-to-get-space
cf-auth "ORG_GUID" "bad-space"))))))))

(t/deftest no-available-spaces
(with-redefs [cf/get-spaces (fn [_ _] [])]
(t/is (thrown+-with-msg?
[:type :kale.common/fail]
#"Unable to find any available spaces for the org."
(sut/attempt-to-get-space cf-auth "ORG_GUID" "bad-space")))))

(t/deftest get-org-space
(with-redefs [sut/attempt-to-get-org
(fn [_ _ _] (org-entity "ORG_GUID1" "org-name"))
Expand Down Expand Up @@ -257,6 +271,39 @@
:org-space {:org "org-name"
:space "space-name"}})))))

(t/deftest preserve-org-space-with-same-username
(let [prev-state {:login {:username "redshirt"
:endpoint "https://api.endpoint.net"}
:org-space {:org "org-name"
:space "space-name"}}]
(with-redefs [sut/get-username (fn [_ _] "redshirt")
sut/get-endpoint (fn [_ _] "https://api.endpoint.net")
sut/get-password (fn [] "scotty")
sut/load-user-info (fn [_ _ _ state]
(t/is (= state prev-state))
user-info)
cf/get-oauth-tokens (fn [_ _ _] {:access_token "TOKEN"})
cf/get-user-data (fn [_] {"user_name" "redshirt"})
write-state (fn [_])]
(with-out-str (sut/login prev-state ["login"] [])))))

(t/deftest clear-org-space-with-new-username
(let [prev-state {:login {:username "redshirt"
:endpoint "https://api.endpoint.net"}
:org-space {:org "org-name"
:space "space-name"}}]
(with-redefs [sut/get-username (fn [_ _] "blueshirt")
sut/get-endpoint (fn [_ _] "https://api.endpoint.net")
sut/get-password (fn [] "scotty")
sut/load-user-info (fn [_ _ _ state]
(t/is (= (:org-space state)
{}))
user-info)
cf/get-oauth-tokens (fn [_ _ _] {:access_token "TOKEN"})
cf/get-user-data (fn [_] {"user_name" "redshirt"})
write-state (fn [_])]
(with-out-str (sut/login prev-state ["login"] [])))))

(t/deftest logout
(with-redefs [write-state (fn [_])]
(t/is (= (str "Logging out..." new-line)
Expand Down

0 comments on commit 13cc2c9

Please sign in to comment.