Skip to content

Commit

Permalink
Ensure ranges are respected during updates as well
Browse files Browse the repository at this point in the history
  • Loading branch information
tiago-loureiro committed Jul 23, 2017
1 parent b28332c commit b24c0a1
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 15 deletions.
18 changes: 11 additions & 7 deletions libs/galley-types/src/Galley/Types/Teams.hs
Original file line number Diff line number Diff line change
Expand Up @@ -156,9 +156,9 @@ data EventData =
deriving (Eq, Show)

data TeamUpdateData = TeamUpdateData
{ _nameUpdate :: Maybe Text
, _iconUpdate :: Maybe Text
, _iconKeyUpdate :: Maybe Text
{ _nameUpdate :: Maybe (Range 1 256 Text)
, _iconUpdate :: Maybe (Range 1 256 Text)
, _iconKeyUpdate :: Maybe (Range 1 256 Text)
} deriving (Eq, Show)

data TeamList = TeamList
Expand Down Expand Up @@ -544,10 +544,14 @@ instance ToJSON TeamUpdateData where

instance FromJSON TeamUpdateData where
parseJSON = withObject "team update data" $ \o -> do
x <- TeamUpdateData <$> o .:? "name" <*> o .:? "icon" <*> o .:? "icon_key"
when (isNothing (_nameUpdate x) && isNothing (_iconUpdate x) && isNothing (_iconKeyUpdate x)) $
fail "no update data specified"
pure x
name <- o .:? "name"
icon <- o .:? "icon"
icon_key <- o .:? "icon_key"
when (isNothing name && isNothing icon && isNothing icon_key) $
fail "TeamUpdateData: no update data specified"
either fail pure $ TeamUpdateData <$> maybe (pure Nothing) (fmap Just . checkedEitherMsg "name") name
<*> maybe (pure Nothing) (fmap Just . checkedEitherMsg "icon") icon
<*> maybe (pure Nothing) (fmap Just . checkedEitherMsg "icon_key") icon_key

instance FromJSON TeamMemberDeleteData where
parseJSON = withObject "team-member-delete-data" $ \o ->
Expand Down
6 changes: 3 additions & 3 deletions services/galley/src/Galley/Data.hs
Original file line number Diff line number Diff line change
Expand Up @@ -222,11 +222,11 @@ updateTeam tid u = retry x5 $ batch $ do
setType BatchLogged
setConsistency Quorum
for_ (u^.nameUpdate) $ \n ->
addPrepQuery Cql.updateTeamName (n, tid)
addPrepQuery Cql.updateTeamName (fromRange n, tid)
for_ (u^.iconUpdate) $ \i ->
addPrepQuery Cql.updateTeamIcon (i, tid)
addPrepQuery Cql.updateTeamIcon (fromRange i, tid)
for_ (u^.iconKeyUpdate) $ \k ->
addPrepQuery Cql.updateTeamIconKey (k, tid)
addPrepQuery Cql.updateTeamIconKey (fromRange k, tid)

-- Conversations ------------------------------------------------------------

Expand Down
18 changes: 13 additions & 5 deletions services/galley/test/integration/API/Teams.hs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ import API.Util (Galley, Brig, test, zUser, zConn)
import Bilge hiding (timeout)
import Bilge.Assert
import Control.Concurrent.Async (mapConcurrently)
import Control.Lens hiding ((#))
import Control.Lens hiding ((#), (.=))
import Control.Monad (void)
import Control.Monad.IO.Class
import Data.Aeson (Value (Null))
import Data.Aeson hiding (json)
import Data.ByteString.Conversion
import Data.Foldable (for_)
import Data.Id
Expand All @@ -27,6 +27,7 @@ import Test.Tasty.HUnit
import qualified API.Util as Util
import qualified Data.List1 as List1
import qualified Data.Set as Set
import qualified Data.Text as T
import qualified Galley.Types as Conv
import qualified Network.Wai.Utilities.Error as Error
import qualified Test.Tasty.Cannon as WS
Expand Down Expand Up @@ -528,10 +529,17 @@ testUpdateTeam g b c = do
member <- flip newTeamMember p <$> Util.randomUser b
Util.connectUsers b owner (list1 (member^.userId) [])
tid <- Util.createTeam g "foo" owner [member]
let bad = object ["name" .= T.replicate 100 "too large"]
put ( g
. paths ["teams", toByteString' tid]
. zUser owner
. zConn "conn"
. json bad
) !!! const 400 === statusCode
let u = newTeamUpdateData
& nameUpdate .~ (Just "bar")
& iconUpdate .~ (Just "xxx")
& iconKeyUpdate .~ (Just "yyy")
& nameUpdate .~ (Just $ unsafeRange "bar")
& iconUpdate .~ (Just $ unsafeRange "xxx")
& iconKeyUpdate .~ (Just $ unsafeRange "yyy")
WS.bracketR2 c owner (member^.userId) $ \(wsOwner, wsMember) -> do
put ( g
. paths ["teams", toByteString' tid]
Expand Down

0 comments on commit b24c0a1

Please sign in to comment.