Skip to content

Commit

Permalink
brig-integration: Fix flaky tests for API.Federation (#3539)
Browse files Browse the repository at this point in the history
* brig-integration: Don't assume only 1 result in search by display name

Display names are random strings from 2 to 128 characters. If a 2 string name gets generated it is likely that it matches some name generated in another test.

* brig-integration: Mark test not flaky

It didn't fail after runnning it 1000 times.
  • Loading branch information
akshaymankar committed Aug 24, 2023
1 parent bec112a commit f356578
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 16 deletions.
39 changes: 23 additions & 16 deletions services/brig/test/integration/API/Federation.hs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
{-# OPTIONS_GHC -Wno-deferred-out-of-scope-variables #-}
-- This file is part of the Wire Server implementation.
--
-- Copyright (C) 2022 Wire Swiss GmbH <opensource@wire.com>
Expand Down Expand Up @@ -68,7 +67,7 @@ tests m opts brig cannon fedBrigClient =
test m "POST /federation/search-users : Found (multiple users)" (testFulltextSearchMultipleUsers opts brig),
test m "POST /federation/search-users : NotFound" (testSearchNotFound opts),
test m "POST /federation/search-users : Empty Input - NotFound" (testSearchNotFoundEmpty opts),
flakyTest m "POST /federation/search-users : configured restrictions" (testSearchRestrictions opts brig),
test m "POST /federation/search-users : configured restrictions" (testSearchRestrictions opts brig),
test m "POST /federation/get-user-by-handle : configured restrictions" (testGetUserByHandleRestrictions opts brig),
test m "POST /federation/get-user-by-handle : Found" (testGetUserByHandleSuccess opts brig),
test m "POST /federation/get-user-by-handle : NotFound" (testGetUserByHandleNotFound opts),
Expand All @@ -80,7 +79,7 @@ tests m opts brig cannon fedBrigClient =
test m "POST /federation/claim-multi-prekey-bundle : 200" (testClaimMultiPrekeyBundleSuccess brig fedBrigClient),
test m "POST /federation/get-user-clients : 200" (testGetUserClients brig fedBrigClient),
test m "POST /federation/get-user-clients : Not Found" (testGetUserClientsNotFound fedBrigClient),
flakyTest m "POST /federation/on-user-deleted-connections : 200" (testRemoteUserGetsDeleted opts brig cannon fedBrigClient),
test m "POST /federation/on-user-deleted-connections : 200" (testRemoteUserGetsDeleted opts brig cannon fedBrigClient),
test m "POST /federation/api-version : 200" (testAPIVersion brig fedBrigClient)
]

Expand Down Expand Up @@ -116,11 +115,11 @@ testFulltextSearchSuccess opts brig = do
searchResponse <- withSettingsOverrides (allowFullSearch domain opts) $ do
runWaiTestFedClient domain $
createWaiTestFedClient @"search-users" @'Brig $
SearchRequest ((fromName . userDisplayName) user)
SearchRequest (fromName $ userDisplayName user)

liftIO $ do
let contacts = contactQualifiedId <$> S.contacts searchResponse
assertEqual "should return the user id" [quid] contacts
assertElem "should return the user id" quid contacts

testFulltextSearchMultipleUsers :: Opt.Opts -> Brig -> Http ()
testFulltextSearchMultipleUsers opts brig = do
Expand Down Expand Up @@ -190,22 +189,30 @@ testSearchRestrictions opts brig = do
FD.FederationDomainConfig domainFullSearch FullSearch
]

let expectSearch :: HasCallStack => Domain -> Text -> [Qualified UserId] -> FederatedUserSearchPolicy -> WaiTest.Session ()
expectSearch domain squery expectedUsers expectedSearchPolicy = do
let expectSearch :: HasCallStack => Domain -> Either Handle Name -> Maybe (Qualified UserId) -> FederatedUserSearchPolicy -> WaiTest.Session ()
expectSearch domain handleOrName mExpectedUser expectedSearchPolicy = do
let squery = either fromHandle fromName handleOrName
searchResponse <-
runWaiTestFedClient domain $
createWaiTestFedClient @"search-users" @'Brig (SearchRequest squery)
liftIO $ assertEqual "Unexpected search result" expectedUsers (contactQualifiedId <$> S.contacts searchResponse)
liftIO $ assertEqual "Unexpected search result" expectedSearchPolicy (S.searchPolicy searchResponse)
liftIO $ do
case (mExpectedUser, handleOrName) of
(Just expectedUser, Right _) ->
assertElem "Unexpected search result" expectedUser (contactQualifiedId <$> S.contacts searchResponse)
(Nothing, Right _) ->
assertEqual "Unexpected search result" [] (contactQualifiedId <$> S.contacts searchResponse)
_ ->
assertEqual "Unexpected search result" (maybeToList mExpectedUser) (contactQualifiedId <$> S.contacts searchResponse)
assertEqual "Unexpected search result" expectedSearchPolicy (S.searchPolicy searchResponse)

withSettingsOverrides opts' $ do
expectSearch domainNoSearch (fromHandle handle) [] NoSearch
expectSearch domainExactHandle (fromHandle handle) [quid] ExactHandleSearch
expectSearch domainExactHandle (fromName (userDisplayName user)) [] ExactHandleSearch
expectSearch domainFullSearch (fromHandle handle) [quid] FullSearch
expectSearch domainFullSearch (fromName (userDisplayName user)) [quid] FullSearch
expectSearch domainUnconfigured (fromHandle handle) [] NoSearch
expectSearch domainUnconfigured (fromName (userDisplayName user)) [] NoSearch
expectSearch domainNoSearch (Left handle) Nothing NoSearch
expectSearch domainExactHandle (Left handle) (Just quid) ExactHandleSearch
expectSearch domainExactHandle (Right (userDisplayName user)) Nothing ExactHandleSearch
expectSearch domainFullSearch (Left handle) (Just quid) FullSearch
expectSearch domainFullSearch (Right (userDisplayName user)) (Just quid) FullSearch
expectSearch domainUnconfigured (Left handle) Nothing NoSearch
expectSearch domainUnconfigured (Right (userDisplayName user)) Nothing NoSearch

testGetUserByHandleRestrictions :: Opt.Opts -> Brig -> Http ()
testGetUserByHandleRestrictions opts brig = do
Expand Down
5 changes: 5 additions & 0 deletions services/brig/test/integration/Util.hs
Original file line number Diff line number Diff line change
Expand Up @@ -1333,3 +1333,8 @@ spawn cp minput = do
assertJust :: (HasCallStack, MonadIO m) => Maybe a -> m a
assertJust (Just a) = pure a
assertJust Nothing = liftIO $ error "Expected Just, got Nothing"

assertElem :: (HasCallStack, Eq a, Show a) => String -> a -> [a] -> Assertion
assertElem msg x xs =
unless (x `elem` xs) $
assertFailure (msg <> "\nExpected to find: \n" <> show x <> "\nin:\n" <> show xs)

0 comments on commit f356578

Please sign in to comment.