(ns dda.backup.cred-rot.domain-test (:require [clojure.test :refer [deftest is]] [clojure.spec.alpha :as s] [clojure.spec.test.alpha :as st] [cljc.java-time.local-date :as ld] [dda.backup.cred-rot.domain :as cut])) (deftest test-spec-id (is (s/valid? ::cut/id "521e0760")) (is (s/valid? ::cut/id "test")) (is (s/valid? ::cut/id "123456")) (is (not (s/valid? ::cut/id "ROOT"))) (is (not (s/valid? ::cut/id "Test!")))) (deftest test-spec-username (is (s/valid? ::cut/userName "521e0760")) (is (s/valid? ::cut/userName "Testuser")) (is (s/valid? ::cut/userName "root")) (is (s/valid? ::cut/userName "ROOT")) (is (not (s/valid? ::cut/userName "test-user")))) (deftest test-spec-hostName (let [valid #(s/valid? ::cut/hostName %)] (is (valid "test-some-combination-2")) (is (valid "backup-restore-65bd9b6ff5-z69sn")))) (deftest test-spec-timestamp (let [valid #(s/valid? cut/timestamp-string? %)] (is (valid "2024-10-18 13:08:16")) (is (valid "2032-09-01 12:56:59")) (is (not (valid "2024-13-5 13:08:16"))) (is (not (valid "2024-6-42 13:08:16"))) (is (not (valid "test"))))) (deftest test-parse-response (is (= (ld/parse "2024-10-19 13:16:54" cut/timestamp-formatter) (:created (first (cut/parse-response "[ { \"current\": false, \"id\": \"521e0760\", \"userName\": \"root\", \"hostName\": \"backup-restore-65bd9b6ff5-z69sn\", \"created\": \"2024-11-18 13:08:16\" }, { \"current\": true, \"id\": \"b67161fb\", \"userName\": \"root\", \"hostName\": \"backup-restore-65bd9b6ff5-z69sn\", \"created\": \"2024-10-19 13:16:54\" } ]")))))) (deftest test-set-new-password-action (is (= :error-parse-password (cut/set-new-password-action (ld/parse "2024-10-19 13:16:54" cut/timestamp-formatter) [] {:new-password-config {:new-password-file "new-pw-file" :valid-from "2024-11-29 12:00:16"}}))) (is (= :wait-for-new-pwd (cut/set-new-password-action (ld/parse "2024-10-19 13:16:54" cut/timestamp-formatter) [{:current true :id "a1" :userName "root" :hostName "host" :created (ld/parse "2023-01-01 00:00:00" cut/timestamp-formatter)}] {:new-password-config {:new-password-file "new-pw-file" :valid-from "2024-11-29 12:00:16"}}))) (is (= :change-pwd (cut/set-new-password-action (ld/parse "2024-11-29 13:16:54" cut/timestamp-formatter) [{:current true :id "a1" :userName "root" :hostName "host" :created (ld/parse "2023-01-01 00:00:00" cut/timestamp-formatter)}] {:new-password-config {:new-password-file "new-pw-file" :valid-from "2024-11-29 12:00:16"}}))) (is (= :pwd-change-finished (cut/set-new-password-action (ld/parse "2024-11-29 13:16:55" cut/timestamp-formatter) [{:current true :id "a2" :userName "root" :hostName "host" :created (ld/parse "2024-11-29 13:16:54" cut/timestamp-formatter)}] {:new-password-config {:new-password-file "new-pw-file" :valid-from "2024-11-29 12:00:16"}}))) (is (= :no-pwd-change-needed (cut/set-new-password-action (ld/parse "2024-11-29 13:16:55" cut/timestamp-formatter) [{:current true :id "a2" :userName "root" :hostName "host" :created (ld/parse "2024-11-29 13:16:54" cut/timestamp-formatter)}] {}))) )