diff --git a/apitoolkit-server.cabal b/apitoolkit-server.cabal index c422ef867..be7dc55f9 100644 --- a/apitoolkit-server.cabal +++ b/apitoolkit-server.cabal @@ -58,6 +58,7 @@ library Pages.Anomalies.Routes Pages.Anomalies.Server Pages.Api + Pages.ApiSpec Pages.AutoComplete Pages.BodyWrapper Pages.Charts.Charts diff --git a/src/Pages/Api.hs b/src/Pages/Api.hs index 3d4be5001..a773a9888 100644 --- a/src/Pages/Api.hs +++ b/src/Pages/Api.hs @@ -1,4 +1,4 @@ -module Pages.Api (apiGetH, apiPostH, apiDeleteH, GenerateAPIKeyForm (..), ApiGet, ApiMut) where +module Pages.Api (apiGetH, apiPostH, apiDeleteH, GenerateAPIKeyForm (..), ApiGet (..), ApiMut (..)) where import Data.ByteString.Base64 qualified as B64 import Data.Default (def) diff --git a/src/Pages/ApiSpec.hs b/src/Pages/ApiSpec.hs new file mode 100644 index 000000000..a8e5a01a2 --- /dev/null +++ b/src/Pages/ApiSpec.hs @@ -0,0 +1,57 @@ +module Pages.ApiSpec (spec) where + +import Data.UUID qualified as UUID +import Data.Vector qualified as V +import Models.Projects.ProjectApiKeys (ProjectApiKey (..)) +import Models.Projects.Projects qualified as Projects +import Pages.BodyWrapper (PageCtx (..)) +import Pkg.TestUtils +import Relude +import Relude.Unsafe qualified as Unsafe +import Test.Hspec + +import Pages.Api qualified as Api + + +testPid :: Projects.ProjectId +testPid = Projects.ProjectId UUID.nil + + +spec :: Spec +spec = aroundAll withTestResources do + describe "Check API Keys" do + it "should empty apikey list" \TestResources{..} -> do + (Api.ApiGet (PageCtx bwconf (pid, apiKeys))) <- toServantResponse trATCtx trSessAndHeader trLogger $ Api.apiGetH testPid + length apiKeys `shouldBe` 0 + + it "should add apikey" \TestResources{..} -> do + let apikeyForm = Api.GenerateAPIKeyForm{title = "Test", from = Nothing} + (Api.ApiPost pid apiKeys apiKeyM) <- toServantResponse trATCtx trSessAndHeader trLogger $ Api.apiPostH testPid apikeyForm + length apiKeys `shouldBe` 1 + testPid `shouldBe` pid + isJust apiKeyM `shouldBe` True + let (apiKey, keyText) = Unsafe.fromJust apiKeyM + apiKey.title `shouldBe` "Test" + apiKey.active `shouldBe` True + apiKey.keyPrefix `shouldBe` keyText + + it "should get apikey list" \TestResources{..} -> do + (Api.ApiGet (PageCtx bwconf (pid, apiKeys))) <- toServantResponse trATCtx trSessAndHeader trLogger $ Api.apiGetH testPid + length apiKeys `shouldBe` 1 + + it "should delete apikey" \TestResources{..} -> do + (Api.ApiGet (PageCtx bwconf (_pid, apiKeys))) <- toServantResponse trATCtx trSessAndHeader trLogger $ Api.apiGetH testPid + length apiKeys `shouldBe` 1 + let apiKey = V.head apiKeys + + (Api.ApiPost pid apiKys apiKeyM) <- toServantResponse trATCtx trSessAndHeader trLogger $ Api.apiDeleteH testPid apiKey.id + isNothing apiKeyM `shouldBe` True + length apiKys `shouldBe` 1 + let apk = V.head apiKys + apk.active `shouldBe` False + pid `shouldBe` testPid + it "should get apikey list (revoked)" \TestResources{..} -> do + (Api.ApiGet (PageCtx _ (_pid, apks))) <- toServantResponse trATCtx trSessAndHeader trLogger $ Api.apiGetH testPid + length apks `shouldBe` 1 + let apk = V.head apks + apk.active `shouldBe` False