-- This file is part of the Wire Server implementation.
--
-- Copyright (C) 2023 Wire Swiss GmbH <opensource@wire.com>
--
-- This program is free software: you can redistribute it and/or modify it under
-- the terms of the GNU Affero General Public License as published by the Free
-- Software Foundation, either version 3 of the License, or (at your option) any
-- later version.
--
-- This program is distributed in the hope that it will be useful, but WITHOUT
-- ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
-- FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
-- details.
--
-- You should have received a copy of the GNU Affero General Public License along
-- with this program. If not, see <https://www.gnu.org/licenses/>.

module Test.Services where

import API.Brig
import API.Common
import SetupHelpers
import Testlib.Prelude

testUpdateServiceUpdateAcceptHeader :: (HasCallStack) => App ()
testUpdateServiceUpdateAcceptHeader :: HasCallStack => App ()
testUpdateServiceUpdateAcceptHeader = do
  let dom :: Domain
dom = Domain
OwnDomain
  String
email <- App String
randomEmail
  Value
alice <- Domain -> CreateUser -> App Value
forall domain.
(HasCallStack, MakesValue domain) =>
domain -> CreateUser -> App Value
randomUser Domain
dom CreateUser
forall a. Default a => a
def
  Value
provider <- Value -> NewProvider -> App Value
forall user.
(HasCallStack, MakesValue user) =>
user -> NewProvider -> App Value
setupProvider Value
alice NewProvider
forall a. Default a => a
def {newProviderEmail = email}
  String
pId <- Value
provider Value -> String -> App Value
forall a. (HasCallStack, MakesValue a) => a -> String -> App Value
%. String
"id" App Value -> (App Value -> App String) -> App String
forall a b. a -> (a -> b) -> b
& App Value -> App String
forall a. (HasCallStack, MakesValue a) => a -> App String
asString
  Value
service <- Domain -> String -> NewService -> App Value
forall dom.
(HasCallStack, MakesValue dom) =>
dom -> String -> NewService -> App Value
newService Domain
dom String
pId NewService
forall a. Default a => a
def
  Value
sId <- Value
service Value -> String -> App Value
forall a. (HasCallStack, MakesValue a) => a -> String -> App Value
%. String
"id"
  App ByteString -> App ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void
    (App ByteString -> App ()) -> App ByteString -> App ()
forall a b. (a -> b) -> a -> b
$ Domain
-> String -> Value -> Maybe String -> Maybe String -> App Response
forall dom serviceId.
(HasCallStack, MakesValue dom, MakesValue serviceId) =>
dom
-> String
-> serviceId
-> Maybe String
-> Maybe String
-> App Response
updateService Domain
dom String
pId Value
sId (String -> Maybe String
forall a. a -> Maybe a
Just String
"application/json") (String -> Maybe String
forall a. a -> Maybe a
Just String
"brand new service")
    App Response -> (Response -> App ByteString) -> App ByteString
forall a b. App a -> (a -> App b) -> App b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= HasCallStack => Int -> Response -> App ByteString
Int -> Response -> App ByteString
getBody Int
200
  App ByteString -> App ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void
    (App ByteString -> App ()) -> App ByteString -> App ()
forall a b. (a -> b) -> a -> b
$ Domain
-> String -> Value -> Maybe String -> Maybe String -> App Response
forall dom serviceId.
(HasCallStack, MakesValue dom, MakesValue serviceId) =>
dom
-> String
-> serviceId
-> Maybe String
-> Maybe String
-> App Response
updateService Domain
dom String
pId Value
sId (String -> Maybe String
forall a. a -> Maybe a
Just String
"text/plain") (String -> Maybe String
forall a. a -> Maybe a
Just String
"even newer service")
    App Response -> (Response -> App ByteString) -> App ByteString
forall a b. App a -> (a -> App b) -> App b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= HasCallStack => Int -> Response -> App ByteString
Int -> Response -> App ByteString
getBody Int
200
  App ByteString -> App ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void
    (App ByteString -> App ()) -> App ByteString -> App ()
forall a b. (a -> b) -> a -> b
$ Domain
-> String -> Value -> Maybe String -> Maybe String -> App Response
forall dom serviceId.
(HasCallStack, MakesValue dom, MakesValue serviceId) =>
dom
-> String
-> serviceId
-> Maybe String
-> Maybe String
-> App Response
updateService Domain
dom String
pId Value
sId Maybe String
forall a. Maybe a
Nothing (String -> Maybe String
forall a. a -> Maybe a
Just String
"really old service")
    App Response -> (Response -> App ByteString) -> App ByteString
forall a b. App a -> (a -> App b) -> App b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= HasCallStack => Int -> Response -> App ByteString
Int -> Response -> App ByteString
getBody Int
200