Skip to content

Commit

Permalink
WPB-9062 Provider API asset upload (#4082)
Browse files Browse the repository at this point in the history
  • Loading branch information
battermann authored Jun 5, 2024
1 parent 357aab6 commit 6b59c4f
Show file tree
Hide file tree
Showing 8 changed files with 127 additions and 24 deletions.
1 change: 1 addition & 0 deletions changelog.d/3-bug-fixes/expose-provider-assets
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Expose /providers/assets via nginz
22 changes: 19 additions & 3 deletions charts/nginz/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,10 @@ nginx_conf:
- path: /properties
envs:
- all
- path: /provider$
envs:
- all
allow_credentials: true
- path: /provider/register
envs:
- all
Expand All @@ -253,16 +257,28 @@ nginx_conf:
envs:
- all
disable_zauth: true
- path: /providers
- path: /provider/email
envs:
- all
- path: /services
allow_credentials: true
- path: /provider/password
envs:
- all
allow_credentials: true
- path: /provider/pid
envs:
- all
- path: /provider
allow_credentials: true
- path: /provider/services
envs:
- all
allow_credentials: true
- path: /providers
envs:
- all
- path: /services
envs:
- all
- path: /bot/self
envs:
- all
Expand Down
1 change: 1 addition & 0 deletions integration/integration.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ library
Test.MLS.Unreachable
Test.Notifications
Test.Presence
Test.Provider
Test.PushToken
Test.Roles
Test.Search
Expand Down
47 changes: 27 additions & 20 deletions integration/test/API/Cargohold.hs
Original file line number Diff line number Diff line change
Expand Up @@ -32,38 +32,45 @@ uploadAssetV3 user isPublic retention mimeType bdy = do
req
& zUser uid
& addBody body multipartMixedMime
where
multipartMixedMime :: String
multipartMixedMime = "multipart/mixed; boundary=" <> multipartBoundary

uploadAsset :: (HasCallStack, MakesValue user) => user -> App Response
uploadAsset = flip uploadFreshAsset "Hello World!"

uploadProviderAsset :: (HasCallStack, MakesValue domain) => domain -> String -> String -> App Response
uploadProviderAsset domain pid payload = do
req <- rawBaseRequest domain Cargohold Versioned $ joinHttpPath ["provider", "assets"]
bdy <- txtAsset payload
submit "POST" $
req
& zProvider pid
& zType "provider"
& addBody bdy multipartMixedMime

uploadFreshAsset :: (HasCallStack, MakesValue user) => user -> String -> App Response
uploadFreshAsset user payload = do
uid <- user & objId
req <- baseRequest user Cargohold Versioned "/assets"
bdy <- txtAsset
bdy <- txtAsset payload
submit "POST" $
req
& zUser uid
& addBody bdy multipartMixedMime
where
txtAsset :: HasCallStack => App HTTP.RequestBody
txtAsset =
buildUploadAssetRequestBody
True
(Nothing :: Maybe String)
(LBSC.pack payload)
textPlainMime

textPlainMime :: MIME.MIMEType
textPlainMime = MIME.Text $ T.pack "plain"

-- This case is a bit special and doesn't fit to MIMEType: We need to define
-- the boundary.
multipartMixedMime :: String
multipartMixedMime = "multipart/mixed; boundary=" <> multipartBoundary

txtAsset :: HasCallStack => String -> App HTTP.RequestBody
txtAsset payload =
buildUploadAssetRequestBody
True
(Nothing :: Maybe String)
(LBSC.pack payload)
textPlainMime

textPlainMime :: MIME.MIMEType
textPlainMime = MIME.Text $ T.pack "plain"

-- This case is a bit special and doesn't fit to MIMEType: We need to define
-- the boundary.
multipartMixedMime :: String
multipartMixedMime = "multipart/mixed; boundary=" <> multipartBoundary

mimeTypeToString :: MIME.MIMEType -> String
mimeTypeToString = T.unpack . MIME.showMIMEType
Expand Down
49 changes: 49 additions & 0 deletions integration/test/API/Nginz.hs
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
module API.Nginz where

import qualified Codec.MIME.Type as MIME
import qualified Data.Aeson as Aeson
import qualified Data.ByteString.Lazy as LBS
import qualified Data.ByteString.Lazy.Char8 as LBSC
import qualified Data.Text as T
import qualified Network.HTTP.Client as HTTP
import Test.Cargohold.API.Util (buildMultipartBody, multipartBoundary)
import Testlib.Prelude

getSystemSettingsUnAuthorized :: (HasCallStack, MakesValue domain) => domain -> App Response
Expand Down Expand Up @@ -41,3 +48,45 @@ getConversation user qcnv t = do
token <- make t & asString
req <- rawBaseRequest user Nginz Versioned (joinHttpPath ["conversations", domain, cnv])
submit "GET" (req & addHeader "Authorization" ("Bearer " <> token))

uploadProviderAsset :: (HasCallStack, MakesValue domain) => domain -> String -> String -> App Response
uploadProviderAsset domain cookie payload = do
req <- rawBaseRequest domain Nginz Versioned $ joinHttpPath ["provider", "assets"]
bdy <- txtAsset payload
submit "POST" $
req
& setCookie cookie
& addBody bdy multipartMixedMime

txtAsset :: HasCallStack => String -> App HTTP.RequestBody
txtAsset payload =
buildUploadAssetRequestBody
True
(Nothing :: Maybe String)
(LBSC.pack payload)
textPlainMime

textPlainMime :: MIME.MIMEType
textPlainMime = MIME.Text $ T.pack "plain"

-- This case is a bit special and doesn't fit to MIMEType: We need to define
-- the boundary.
multipartMixedMime :: String
multipartMixedMime = "multipart/mixed; boundary=" <> multipartBoundary

buildUploadAssetRequestBody ::
(HasCallStack, MakesValue assetRetention) =>
Bool ->
assetRetention ->
LBS.ByteString ->
MIME.MIMEType ->
App HTTP.RequestBody
buildUploadAssetRequestBody isPublic retention body mimeType = do
mbRetention <- make retention
let header' :: Aeson.Value
header' =
Aeson.object
[ "public" .= isPublic,
"retention" .= mbRetention
]
HTTP.RequestBodyLBS <$> buildMultipartBody header' body mimeType
26 changes: 26 additions & 0 deletions integration/test/Test/Provider.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
module Test.Provider where

import API.Brig
-- import API.Cargohold (uploadProviderAsset)

import qualified API.Cargohold as Cargohold
import API.Common
import qualified API.Nginz as Nginz
import Data.String.Conversions (cs)
import SetupHelpers
import Testlib.Prelude

testProviderUploadAsset :: HasCallStack => App ()
testProviderUploadAsset = do
email <- randomEmail
alice <- randomUser OwnDomain def
provider <- setupProvider alice def {newProviderEmail = email}
pid <- provider %. "id" & asString
-- test cargohold API
bindResponse (Cargohold.uploadProviderAsset OwnDomain pid "profile pic") $ \resp -> do
resp.status `shouldMatchInt` 201
pw <- provider %. "password" & asString
cookie <- loginProvider OwnDomain email pw
-- test Nginz API
bindResponse (Nginz.uploadProviderAsset OwnDomain (cs cookie) "another profile pic") $ \resp -> do
resp.status `shouldMatchInt` 201
3 changes: 3 additions & 0 deletions integration/test/Testlib/HTTP.hs
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,9 @@ baseRequest user service versioned path = do
zUser :: String -> HTTP.Request -> HTTP.Request
zUser = addHeader "Z-User"

zProvider :: String -> HTTP.Request -> HTTP.Request
zProvider = addHeader "Z-Provider"

zConnection :: String -> HTTP.Request -> HTTP.Request
zConnection = addHeader "Z-Connection"

Expand Down
2 changes: 1 addition & 1 deletion services/nginz/integration-test/conf/nginz/nginx.conf
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ http {
proxy_pass http://cargohold;
}

location /provider/assets {
location ~* ^(/v[0-9]+)?/provider/assets$ {
include common_response_with_zauth.conf;
proxy_pass http://cargohold;
}
Expand Down

0 comments on commit 6b59c4f

Please sign in to comment.