Skip to content

Commit 3d77b74

Browse files
committed
CMR-11116: adds tests for launchpad token caching behavior
1 parent 90da569 commit 3d77b74

File tree

3 files changed

+69
-8
lines changed

3 files changed

+69
-8
lines changed

mock-echo-app/src/cmr/mock_echo/api/routes.clj

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,8 @@
11
(ns cmr.mock-echo.api.routes
22
"Defines the HTTP URL routes for the application."
33
(:require
4-
[cheshire.core :as json]
5-
[clojure.set :as set]
64
[cmr.common.api.context :as context]
75
[cmr.common.api.errors :as errors]
8-
[cmr.common.log :refer (debug info warn error)]
9-
[cmr.common.services.errors :as svc-errors]
106
[cmr.mock-echo.api.acls :as acls-api]
117
[cmr.mock-echo.api.providers :as providers-api]
128
[cmr.mock-echo.api.tokens :as token-api]
@@ -25,7 +21,8 @@
2521
(token-db/reset context)
2622
(provider-db/reset context)
2723
(acl-db/reset context)
28-
(urs-db/reset context))
24+
(urs-db/reset context)
25+
(urs-api/reset-launchpad-tokens!))
2926

3027
(defn- build-routes [system]
3128
(routes

mock-echo-app/src/cmr/mock_echo/api/urs.clj

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,28 @@
22
"Defines routes for mocking URS"
33
(:require
44
[cheshire.core :as json]
5+
[clj-time.core :as t]
56
[clojure.data.codec.base64 :as b64]
67
[clojure.string :as string]
78
[cmr.common.mime-types :as mt]
89
[cmr.common.services.errors :as errors]
10+
[cmr.common.time-keeper :as time-keeper]
11+
[cmr.common.util :as common-util]
912
[cmr.common.xml :as cx]
1013
[cmr.mock-echo.data.urs-db :as urs-db]
1114
[cmr.transmit.config :as transmit-config]
1215
[compojure.core :refer :all]))
1316

17+
(def launchpad-token-validations
18+
"Tracks when launchpad tokens were first validated to simulate absolute token expiration.
19+
Map of {token -> {:first-validated-at timestamp :expires-in seconds}}"
20+
(atom {}))
21+
22+
(defn reset-launchpad-tokens!
23+
"Resets the launchpad token validation tracking. Called by test fixtures."
24+
[]
25+
(reset! launchpad-token-validations {}))
26+
1427
(defn get-user
1528
"Processes a request to get a user."
1629
[context name]
@@ -44,11 +57,33 @@
4457
{:status 404 :body "Not found.\n"}))
4558

4659
(defn get-launchpad-user
47-
"Processes a request to get a user using their launchpad token."
60+
"Processes a request to get a user using their launchpad token.
61+
Supports specific test tokens that return different status codes for testing error handling.
62+
Simulates real EDL behavior where tokens have absolute expiration times."
4863
[token]
4964
(case token
5065
"ABC-1ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ"
51-
{:status 200 :body {:uid "user1" :lp_token_expires_in 1600}}
66+
(let [expires-in 1600
67+
validation-info (get @launchpad-token-validations token)]
68+
(if validation-info
69+
(let [elapsed-seconds (t/in-seconds (t/interval (:first-validated-at validation-info) (time-keeper/now)))]
70+
(if (>= elapsed-seconds expires-in)
71+
{:status 401 :body {:error (format "Launchpad token (partially redacted) [%s] has expired."
72+
(common-util/scrub-token token))}}
73+
{:status 200 :body {:uid "user1" :lp_token_expires_in (- expires-in elapsed-seconds)}}))
74+
(do
75+
(swap! launchpad-token-validations assoc token {:first-validated-at (time-keeper/now) :expires-in expires-in})
76+
{:status 200 :body {:uid "user1" :lp_token_expires_in expires-in}})))
77+
78+
"ABC-429-ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ"
79+
{:status 429 :body {:error "Rate limit exceeded"}}
80+
81+
"ABC-504-ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ"
82+
{:status 504 :body {:error "Gateway timeout"}}
83+
84+
"ABC-INV-ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ"
85+
{:status 401 :body {:error "Invalid token"}}
86+
5287
{:status 400 :body {:error "Launchpad SSO authentication failed"}}))
5388

5489
(defn get-user-info

system-int-test/test/cmr/system_int_test/misc/launchpad_user_cache.clj

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@
6868
(index/wait-until-indexed)
6969
(is (= 401 (:status resp)))
7070
(is (= ["Launchpad token (partially redacted) [ABC-1ZZZZZXXXZZZZZ] has expired."] (:errors resp)))
71-
(is (not= expiration-time (cache-util/list-cache-keys (url/ingest-read-caches-url) "launchpad-user" transmit-config/mock-echo-system-token)))))
71+
(is (empty? (cache-util/list-cache-keys (url/ingest-read-caches-url) "launchpad-user" transmit-config/mock-echo-system-token)))))
7272

7373
(testing "urs cache clears after 24 hours"
7474
(dev-sys-util/advance-time! (* 60 60 24))
@@ -79,3 +79,32 @@
7979
"cmr"
8080
transmit-config/mock-echo-system-token
8181
404))))))))))
82+
83+
(deftest transient-errors-not-cached-test
84+
(testing "Transient errors (429, 504) are not cached"
85+
(let [resp (ingest/ingest-concept (data-umm-c/collection-concept {}) {:token "ABC-429-ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ"})]
86+
(is (= 429 (:status resp)))
87+
(is (some #(re-find #"Rate limit exceeded" %) (:errors resp))))
88+
89+
(is (empty? (cache-util/list-cache-keys (url/ingest-read-caches-url) "launchpad-user" transmit-config/mock-echo-system-token))))
90+
91+
(testing "Gateway timeout errors (504) are not cached"
92+
(let [resp (ingest/ingest-concept (data-umm-c/collection-concept {}) {:token "ABC-504-ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ"})]
93+
(is (= 504 (:status resp)))
94+
(is (some #(re-find #"Gateway timeout" %) (:errors resp))))
95+
96+
(is (empty? (cache-util/list-cache-keys (url/ingest-read-caches-url) "launchpad-user" transmit-config/mock-echo-system-token)))))
97+
98+
(deftest non-transient-errors-are-cached-test
99+
(testing "Non-transient errors are cached for 5 minutes"
100+
(let [token "ABC-INV-ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ"
101+
token-key (hash token)]
102+
(let [resp (ingest/ingest-concept (data-umm-c/collection-concept {}) {:token token})]
103+
(is (= 401 (:status resp))))
104+
105+
(is (seq (cache-util/list-cache-keys (url/ingest-read-caches-url) "launchpad-user" transmit-config/mock-echo-system-token)))
106+
107+
(dev-sys-util/advance-time! 301)
108+
109+
(let [resp (ingest/ingest-concept (data-umm-c/collection-concept {}) {:token token})]
110+
(is (= 401 (:status resp)))))))

0 commit comments

Comments
 (0)