Skip to content

Commit 0ab843a

Browse files
committed
Add brig integration tests for password-less users.
1 parent 01e2e55 commit 0ab843a

File tree

4 files changed

+42
-30
lines changed

4 files changed

+42
-30
lines changed

services/brig/test/integration/API/Team.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -562,9 +562,9 @@ testCreateUserInternalSSO brig galley = do
562562
let ssoid = UserSSOId "nil" "nil"
563563

564564
-- creating users requires both sso_id and team_id
565-
postUser' False "dummy" True False (Just ssoid) Nothing brig
565+
postUser' True False "dummy" True False (Just ssoid) Nothing brig
566566
!!! const 400 === statusCode
567-
postUser' False "dummy" True False Nothing (Just teamid) brig
567+
postUser' True False "dummy" True False Nothing (Just teamid) brig
568568
!!! const 400 === statusCode
569569

570570
-- creating user with sso_id, team_id is ok

services/brig/test/integration/API/Team/Util.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ deleteTeam g tid u = do
151151
. paths ["teams", toByteString' tid]
152152
. zUser u
153153
. zConn "conn"
154-
. lbytes (encode $ Team.newTeamDeleteData Util.defPassword)
154+
. lbytes (encode $ Team.newTeamDeleteData $ Just Util.defPassword)
155155
) !!! const 202 === statusCode
156156

157157
getTeams :: UserId -> Galley -> Http Team.TeamList

services/brig/test/integration/API/User/Client.hs

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -38,20 +38,22 @@ tests :: ConnectionLimit -> Opt.Timeout -> Maybe Opt.Opts -> Manager -> Brig ->
3838
tests _cl _at _conf p b c g = testGroup "client"
3939
[ test p "get /users/:user/prekeys - 200" $ testGetUserPrekeys b
4040
, test p "get /users/:user/prekeys/:client - 200" $ testGetClientPrekey b
41-
, test p "post /clients - 201" $ testAddGetClient b c
41+
, test p "post /clients - 201 (pwd)" $ testAddGetClient True b c
42+
, test p "post /clients - 201 (no pwd)" $ testAddGetClient False b c
4243
, test p "post /clients - 403" $ testClientReauthentication b
4344
, test p "get /clients - 200" $ testListClients b
4445
, test p "get /clients/:client/prekeys - 200" $ testListPrekeyIds b
4546
, test p "post /clients - 400" $ testTooManyClients b
46-
, test p "delete /clients/:client - 200" $ testRemoveClient b c
47+
, test p "delete /clients/:client - 200 (pwd)" $ testRemoveClient True b c
48+
-- , test p "delete /clients/:client - 200 (no pwd)" $ testRemoveClient False b c -- TODO: is there an easy way with phone user?
4749
, test p "put /clients/:client - 200" $ testUpdateClient b
4850
, test p "post /clients - 200 multiple temporary" $ testAddMultipleTemporary b g
4951
, test p "client/prekeys/race" $ testPreKeyRace b
5052
]
5153

52-
testAddGetClient :: Brig -> Cannon -> Http ()
53-
testAddGetClient brig cannon = do
54-
uid <- userId <$> randomUser brig
54+
testAddGetClient :: Bool -> Brig -> Cannon -> Http ()
55+
testAddGetClient hasPwd brig cannon = do
56+
uid <- userId <$> randomUser' hasPwd brig
5557
let rq = addClientReq brig uid (defNewClient TemporaryClient [somePrekeys !! 0] (someLastPrekeys !! 0))
5658
. header "X-Forwarded-For" "127.0.0.1" -- Fake IP to test IpAddr parsing.
5759
c <- WS.bracketR cannon uid $ \ws -> do
@@ -182,9 +184,12 @@ testTooManyClients brig = do
182184
const 403 === statusCode
183185
const (Just "too-many-clients") === fmap Error.label . decodeBody
184186

185-
testRemoveClient :: Brig -> Cannon -> Http ()
186-
testRemoveClient brig cannon = do
187-
u <- randomUser brig
187+
testRemoveClient :: Bool -> Brig -> Cannon -> Http ()
188+
testRemoveClient hasPwd brig cannon = do
189+
unless hasPwd $ error
190+
"not implemented: for password-less users, we need to figure out another way to login."
191+
192+
u <- randomUser' hasPwd brig
188193
let uid = userId u
189194
let Just email = userEmail u
190195

@@ -194,12 +199,13 @@ testRemoveClient brig cannon = do
194199
numCookies <- countCookies brig uid defCookieLabel
195200
liftIO $ Just 1 @=? numCookies
196201
c <- decodeBody =<< addClient brig uid (client PermanentClient (someLastPrekeys !! 10))
197-
-- Missing password
198-
deleteClient brig uid (clientId c) Nothing !!! const 403 === statusCode
202+
when hasPwd $ do
203+
-- Missing password
204+
deleteClient brig uid (clientId c) Nothing !!! const 403 === statusCode
199205

200206
-- Success
201207
WS.bracketR cannon uid $ \ws -> do
202-
deleteClient brig uid (clientId c) (Just defPassword)
208+
deleteClient brig uid (clientId c) (if hasPwd then Just defPassword else Nothing)
203209
!!! const 200 === statusCode
204210
void . liftIO $ WS.assertMatch (5 # Second) ws $ \n -> do
205211
let j = Object $ List1.head (ntfPayload n)

services/brig/test/integration/Util.hs

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -90,19 +90,25 @@ test' :: AWS.Env -> Manager -> TestName -> Http a -> TestTree
9090
test' e m n h = testCase n $ void $ runHttpT m (liftIO (purgeJournalQueue e) >> h)
9191

9292
randomUser :: HasCallStack => Brig -> Http User
93-
randomUser brig = do
93+
randomUser = randomUser' True
94+
95+
randomUser' :: HasCallStack => Bool -> Brig -> Http User
96+
randomUser' hasPwd brig = do
9497
n <- fromName <$> randomName
95-
createUser n brig
98+
createUser' hasPwd n brig
9699

97100
createUser :: HasCallStack => Text -> Brig -> Http User
98-
createUser name brig = do
99-
r <- postUser name True False Nothing Nothing brig <!!
101+
createUser = createUser' True
102+
103+
createUser' :: HasCallStack => Bool -> Text -> Brig -> Http User
104+
createUser' hasPwd name brig = do
105+
r <- postUser' hasPwd True name True False Nothing Nothing brig <!!
100106
const 201 === statusCode
101107
decodeBody r
102108

103109
createUserWithEmail :: HasCallStack => Text -> Email -> Brig -> Http User
104110
createUserWithEmail name email brig = do
105-
r <- postUserWithEmail True name (Just email) False Nothing Nothing brig <!!
111+
r <- postUserWithEmail True True name (Just email) False Nothing Nothing brig <!!
106112
const 201 === statusCode
107113
decodeBody r
108114

@@ -164,32 +170,32 @@ getConnection brig from to = get $ brig
164170

165171
-- | More flexible variant of 'createUser' (see above).
166172
postUser :: Text -> Bool -> Bool -> Maybe UserSSOId -> Maybe TeamId -> Brig -> Http ResponseLBS
167-
postUser = postUser' True
173+
postUser = postUser' True True
168174

169-
-- | Use @postUser' False@ instead of 'postUser' if you want to send broken bodies to test error
170-
-- messages.
171-
postUser' :: Bool -> Text -> Bool -> Bool -> Maybe UserSSOId -> Maybe TeamId -> Brig -> Http ResponseLBS
172-
postUser' validateBody name haveEmail havePhone ssoid teamid brig = do
175+
-- | Use @postUser' True False@ instead of 'postUser' if you want to send broken bodies to test error
176+
-- messages. Or @postUser' False True@ if you want to validate the body, but not set a password.
177+
postUser' :: Bool -> Bool -> Text -> Bool -> Bool -> Maybe UserSSOId -> Maybe TeamId -> Brig -> Http ResponseLBS
178+
postUser' hasPassword validateBody name haveEmail havePhone ssoid teamid brig = do
173179
email <- if haveEmail
174180
then Just <$> randomEmail
175181
else pure Nothing
176-
postUserWithEmail validateBody name email havePhone ssoid teamid brig
182+
postUserWithEmail hasPassword validateBody name email havePhone ssoid teamid brig
177183

178184
-- | More flexible variant of 'createUserUntrustedEmail' (see above).
179-
postUserWithEmail :: Bool -> Text -> Maybe Email -> Bool -> Maybe UserSSOId -> Maybe TeamId -> Brig -> Http ResponseLBS
180-
postUserWithEmail validateBody name email havePhone ssoid teamid brig = do
185+
postUserWithEmail :: Bool -> Bool -> Text -> Maybe Email -> Bool -> Maybe UserSSOId -> Maybe TeamId -> Brig -> Http ResponseLBS
186+
postUserWithEmail hasPassword validateBody name email havePhone ssoid teamid brig = do
181187
phone <- if havePhone
182188
then Just <$> randomPhone
183189
else pure Nothing
184-
let o = object
190+
let o = object $
185191
[ "name" .= name
186192
, "email" .= (fromEmail <$> email)
187193
, "phone" .= phone
188-
, "password" .= defPassword
189194
, "cookie" .= defCookieLabel
190195
, "sso_id" .= ssoid
191196
, "team_id" .= teamid
192-
]
197+
] <>
198+
[ "password" .= defPassword | hasPassword ]
193199
p = case Aeson.parse parseJSON o of
194200
Aeson.Success (p_ :: NewUser) -> p_
195201
bad -> error $ show (bad, o)

0 commit comments

Comments
 (0)