Skip to content

Commit

Permalink
Start adding tests
Browse files Browse the repository at this point in the history
  • Loading branch information
amitaibu committed Oct 5, 2024
1 parent d5865c4 commit b9ced44
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 0 deletions.
89 changes: 89 additions & 0 deletions Test/FileStorage/ControllerFunctionsSpec.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE ImplicitParams #-}

module Test.FileStorage.ControllerFunctionsSpec (spec) where

import Test.Hspec
import IHP.Prelude
import IHP.FileStorage.ControllerFunctions
import IHP.Controller.Context
import IHP.FrameworkConfig
import IHP.ModelSupport
import IHP.FileStorage.Types
import System.IO.Temp (withSystemTempDirectory)
import qualified Data.ByteString.Lazy as LBS
import qualified Data.Text as Text
import Data.Default (def)
import Data.Time.Clock (getCurrentTime, addUTCTime)
import IHP.Controller.RequestContext (RequestContext (..))
import Network.Wai (defaultRequest)

spec :: Spec
spec = describe "IHP.FileStorage.ControllerFunctions" $ do
describe "storeFileWithOptions" $ do
it "returns the objectPath without the baseUrl" $ do
withSystemTempDirectory "ihp-test" $ \tempDir -> do
let frameworkConfig = def
{ baseUrl = "http://localhost:8000"
, staticDir = tempDir
, storageDir = tempDir
, appConfig = StaticDirStorage
}
let ?context = ControllerContext
{ requestContext = RequestContext
{ request = defaultRequest
, respond = \_ -> pure undefined
, vault = mempty
, frameworkConfig = frameworkConfig
, requestBody = pure ""
}
, response = error "response not used in test"
, applicationContext = error "applicationContext not used in test"
}
let fileInfo = FileInfo
{ fileName = "test.txt"
, contentType = "text/plain"
, fileContent = "Hello, world!"
}
let options = def { objectName = Just "test.txt" }

result <- storeFileWithOptions fileInfo options

result `shouldBe` "static/test.txt"

describe "createTemporaryDownloadUrlFromPathWithExpiredAt" $ do
it "returns baseUrl concatenated with objectPath when objectPath does not start with http:// or https://" $ do
let frameworkConfig = def { baseUrl = "http://localhost:8000", appConfig = StaticDirStorage }
let ?context = ControllerContext
{ requestContext = RequestContext
{ request = defaultRequest
, respond = \_ -> pure undefined
, vault = mempty
, frameworkConfig = frameworkConfig
, requestBody = pure ""
}
, response = error "response not used in test"
, applicationContext = error "applicationContext not used in test"
}
let objectPath = "static/test.txt"
temporaryDownloadUrl <- createTemporaryDownloadUrlFromPathWithExpiredAt 3600 objectPath

temporaryDownloadUrl.url `shouldBe` "http://localhost:8000/static/test.txt"

it "returns '/' concatenated with objectPath when objectPath starts with 'http://' or 'https://'" $ do
let frameworkConfig = def { baseUrl = "http://localhost:8000", appConfig = StaticDirStorage }
let ?context = ControllerContext
{ requestContext = RequestContext
{ request = defaultRequest
, respond = \_ -> pure undefined
, vault = mempty
, frameworkConfig = frameworkConfig
, requestBody = pure ""
}
, response = error "response not used in test"
, applicationContext = error "applicationContext not used in test"
}
let objectPath = "https://example.com/static/test.txt"
temporaryDownloadUrl <- createTemporaryDownloadUrlFromPathWithExpiredAt 3600 objectPath

temporaryDownloadUrl.url `shouldBe` "https://example.com/static/test.txt"
2 changes: 2 additions & 0 deletions Test/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ import qualified Test.RouterSupportSpec
import qualified Test.ViewSupportSpec
import qualified Test.ServerSideComponent.HtmlParserSpec
import qualified Test.ServerSideComponent.HtmlDiffSpec
import qualified Test.FileStorage.ControllerFunctionsSpec
import qualified Test.FileStorage.MimeTypesSpec
import qualified Test.DataSync.DynamicQueryCompiler
import qualified Test.IDE.CodeGeneration.MigrationGenerator
Expand Down Expand Up @@ -78,6 +79,7 @@ main = hspec do
Test.ViewSupportSpec.tests
Test.ServerSideComponent.HtmlParserSpec.tests
Test.ServerSideComponent.HtmlDiffSpec.tests
Test.FileStorage.ControllerFunctionsSpec.tests
Test.FileStorage.MimeTypesSpec.tests
Test.DataSync.DynamicQueryCompiler.tests
Test.IDE.SchemaDesigner.SchemaOperationsSpec.tests
Expand Down

0 comments on commit b9ced44

Please sign in to comment.