module Test.Cargohold.AssetDownload where
import API.Cargohold
import qualified Data.ByteString.Char8 as BSC
import GHC.Stack
import SetupHelpers
import Testlib.Prelude
testDownloadAsset :: (HasCallStack) => App ()
testDownloadAsset :: HasCallStack => App ()
testDownloadAsset = do
user <- Domain -> CreateUser -> App Value
forall domain.
(HasCallStack, MakesValue domain) =>
domain -> CreateUser -> App Value
randomUser Domain
OwnDomain CreateUser
forall a. Default a => a
def
key <- bindResponse (uploadSomeAsset user) $ \Response
resp -> do
Response
resp.status Int -> Int -> App ()
forall a. (MakesValue a, HasCallStack) => a -> Int -> App ()
`shouldMatchInt` Int
201
Response
resp.json Maybe Value -> [Char] -> App Value
forall a. (HasCallStack, MakesValue a) => a -> [Char] -> App Value
%. [Char]
"key"
bindResponse (downloadAsset user user key "nginz-https.example.com" id) $ \Response
resp -> do
Response
resp.status Int -> Int -> App ()
forall a. (MakesValue a, HasCallStack) => a -> Int -> App ()
`shouldMatchInt` Int
200
ByteString -> [Char]
BSC.unpack Response
resp.body [Char] -> [Char] -> App ()
forall a b.
(MakesValue a, MakesValue b, HasCallStack) =>
a -> b -> App ()
`shouldMatch` [Char]
"Hello World!"
testDownloadAssetMultiIngressS3DownloadUrl :: (HasCallStack) => App ()
testDownloadAssetMultiIngressS3DownloadUrl :: HasCallStack => App ()
testDownloadAssetMultiIngressS3DownloadUrl = do
user <- Domain -> CreateUser -> App Value
forall domain.
(HasCallStack, MakesValue domain) =>
domain -> CreateUser -> App Value
randomUser Domain
OwnDomain CreateUser
forall a. Default a => a
def
key <- doUploadAsset user
bindResponse (downloadAsset user user key "nginz-https.example.com" noRedirect) $ \Response
resp -> do
Response
resp.status Int -> Int -> App ()
forall a. (MakesValue a, HasCallStack) => a -> Int -> App ()
`shouldMatchInt` Int
302
bindResponse (downloadAsset user user key "red.example.com" noRedirect) $ \Response
resp -> do
Response
resp.status Int -> Int -> App ()
forall a. (MakesValue a, HasCallStack) => a -> Int -> App ()
`shouldMatchInt` Int
302
bindResponse (downloadAsset user user key "green.example.com" noRedirect) $ \Response
resp -> do
Response
resp.status Int -> Int -> App ()
forall a. (MakesValue a, HasCallStack) => a -> Int -> App ()
`shouldMatchInt` Int
302
bindResponse (downloadAsset user user key "unknown.example.com" noRedirect) $ \Response
resp -> do
Response
resp.status Int -> Int -> App ()
forall a. (MakesValue a, HasCallStack) => a -> Int -> App ()
`shouldMatchInt` Int
302
withModifiedBackend modifyConfig $ \[Char]
domain -> do
user' <- [Char] -> CreateUser -> App Value
forall domain.
(HasCallStack, MakesValue domain) =>
domain -> CreateUser -> App Value
randomUser [Char]
domain CreateUser
forall a. Default a => a
def
key' <- doUploadAsset user'
bindResponse (downloadAsset user' user' key' "nginz-https.example.com" noRedirect) $ \Response
resp -> do
Response
resp.status Int -> Int -> App ()
forall a. (MakesValue a, HasCallStack) => a -> Int -> App ()
`shouldMatchInt` Int
404
Response
resp.json Maybe Value -> [Char] -> App Value
forall a. (HasCallStack, MakesValue a) => a -> [Char] -> App Value
%. [Char]
"label" App Value -> [Char] -> App ()
forall a b.
(MakesValue a, MakesValue b, HasCallStack) =>
a -> b -> App ()
`shouldMatch` [Char]
"not-found"
bindResponse (downloadAsset user' user' key' "red.example.com" noRedirect) $ \Response
resp -> do
Response
resp.status Int -> Int -> App ()
forall a. (MakesValue a, HasCallStack) => a -> Int -> App ()
`shouldMatchInt` Int
302
Response -> [Char]
locationHeaderHost Response
resp [Char] -> [Char] -> App ()
forall a b.
(MakesValue a, MakesValue b, HasCallStack) =>
a -> b -> App ()
`shouldMatch` [Char]
"s3-download.red.example.com"
bindResponse (downloadAsset user' user' key' "green.example.com" noRedirect) $ \Response
resp -> do
Response
resp.status Int -> Int -> App ()
forall a. (MakesValue a, HasCallStack) => a -> Int -> App ()
`shouldMatchInt` Int
302
Response -> [Char]
locationHeaderHost Response
resp [Char] -> [Char] -> App ()
forall a b.
(MakesValue a, MakesValue b, HasCallStack) =>
a -> b -> App ()
`shouldMatch` [Char]
"s3-download.green.example.com"
bindResponse (downloadAsset user' user' key' "unknown.example.com" noRedirect) $ \Response
resp -> do
Response
resp.status Int -> Int -> App ()
forall a. (MakesValue a, HasCallStack) => a -> Int -> App ()
`shouldMatchInt` Int
404
Response
resp.json Maybe Value -> [Char] -> App Value
forall a. (HasCallStack, MakesValue a) => a -> [Char] -> App Value
%. [Char]
"label" App Value -> [Char] -> App ()
forall a b.
(MakesValue a, MakesValue b, HasCallStack) =>
a -> b -> App ()
`shouldMatch` [Char]
"not-found"
where
modifyConfig :: ServiceOverrides
modifyConfig :: ServiceOverrides
modifyConfig =
ServiceOverrides
forall a. Default a => a
def
{ cargoholdCfg =
setField "aws.multiIngress"
$ object
[ "red.example.com" .= "http://s3-download.red.example.com",
"green.example.com" .= "http://s3-download.green.example.com"
]
}
doUploadAsset :: (HasCallStack) => Value -> App Value
doUploadAsset :: HasCallStack => Value -> App Value
doUploadAsset Value
user = App Response -> (Response -> App Value) -> App Value
forall a.
HasCallStack =>
App Response -> (Response -> App a) -> App a
bindResponse (Value -> App Response
forall user.
(HasCallStack, MakesValue user) =>
user -> App Response
uploadSomeAsset Value
user) ((Response -> App Value) -> App Value)
-> (Response -> App Value) -> App Value
forall a b. (a -> b) -> a -> b
$ \Response
resp -> do
Response
resp.status Int -> Int -> App ()
forall a. (MakesValue a, HasCallStack) => a -> Int -> App ()
`shouldMatchInt` Int
201
Response
resp.json Maybe Value -> [Char] -> App Value
forall a. (HasCallStack, MakesValue a) => a -> [Char] -> App Value
%. [Char]
"key"