|
| 1 | +-- This file is part of the Wire Server implementation. |
| 2 | +-- |
| 3 | +-- Copyright (C) 2026 Wire Swiss GmbH <opensource@wire.com> |
| 4 | +-- |
| 5 | +-- This program is free software: you can redistribute it and/or modify it under |
| 6 | +-- the terms of the GNU Affero General Public License as published by the Free |
| 7 | +-- Software Foundation, either version 3 of the License, or (at your option) any |
| 8 | +-- later version. |
| 9 | +-- |
| 10 | +-- This program is distributed in the hope that it will be useful, but WITHOUT |
| 11 | +-- ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
| 12 | +-- FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more |
| 13 | +-- details. |
| 14 | +-- |
| 15 | +-- You should have received a copy of the GNU Affero General Public License along |
| 16 | +-- with this program. If not, see <https://www.gnu.org/licenses/>. |
| 17 | + |
| 18 | +module Test.NotificationsVersioned where |
| 19 | + |
| 20 | +import API.Brig (addClient, putHandle) |
| 21 | +import API.BrigCommon (AddClient (..)) |
| 22 | +import API.Common (randomHandle) |
| 23 | +import API.Galley |
| 24 | +import Control.Monad.Codensity (runCodensity) |
| 25 | +import Data.Time.Clock |
| 26 | +import Notifications (isConvCreateMeetingNotif, isMeetingCreateNotif) |
| 27 | +import SetupHelpers |
| 28 | +import Test.Events (ackEvent, assertFindsEvent, consumeAllEventsNoAck, createEventsWebSocketAtVersion, enableConsumableNotifications) |
| 29 | +import Test.Meetings (defaultMeetingJson) |
| 30 | +import Testlib.Cannon |
| 31 | +import Testlib.Prelude |
| 32 | +import UnliftIO.Concurrent (threadDelay) |
| 33 | + |
| 34 | +gatedTypes :: [String] |
| 35 | +gatedTypes = |
| 36 | + [ "conversation.create-meeting", |
| 37 | + "conversation.delete-meeting", |
| 38 | + "meeting.create", |
| 39 | + "meeting.update", |
| 40 | + "meeting.delete", |
| 41 | + "meeting.member-add" |
| 42 | + ] |
| 43 | + |
| 44 | +isGated :: String -> Bool |
| 45 | +isGated t = t `elem` gatedTypes |
| 46 | + |
| 47 | +-- | Drain all notification pages at the given API version and return the |
| 48 | +-- payload event types seen. Also asserts that pagination terminates (no |
| 49 | +-- endless empty 'has_more=true' pages). |
| 50 | +drainNotificationsAt :: (HasCallStack, MakesValue user) => user -> Int -> App [String] |
| 51 | +drainNotificationsAt user v = go Nothing [] |
| 52 | + where |
| 53 | + go since acc = do |
| 54 | + req <- baseRequest user Gundeck (ExplicitVersion v) "/notifications" |
| 55 | + let req' = |
| 56 | + req |
| 57 | + & addQueryParams |
| 58 | + ( [("since", s) | s <- toList since] |
| 59 | + <> [("size", "100")] |
| 60 | + ) |
| 61 | + r <- submit "GET" req' |
| 62 | + r.status `shouldMatchInt` 200 |
| 63 | + body <- getJSON 200 r |
| 64 | + notifications <- body %. "notifications" & asList |
| 65 | + types <- |
| 66 | + mconcat |
| 67 | + <$> for |
| 68 | + notifications |
| 69 | + ( \n -> do |
| 70 | + payload <- n %. "payload" & asList |
| 71 | + for payload (\e -> e %. "type" >>= asString) |
| 72 | + ) |
| 73 | + lastId <- case reverse notifications of |
| 74 | + [] -> pure Nothing |
| 75 | + (n : _) -> Just <$> (n %. "id" >>= asString) |
| 76 | + hasMore <- body %. "has_more" & asBool |
| 77 | + if hasMore |
| 78 | + then case lastId of |
| 79 | + Just l -> go (Just l) (acc <> types) |
| 80 | + Nothing -> assertFailure "has_more=true but no notification id for cursor" |
| 81 | + else pure (acc <> types) |
| 82 | + |
| 83 | +mkMeeting :: App Value |
| 84 | +mkMeeting = do |
| 85 | + now <- liftIO getCurrentTime |
| 86 | + let startTime = addUTCTime 3600 now |
| 87 | + endTime = addUTCTime 7200 now |
| 88 | + pure $ defaultMeetingJson "Versioned meeting" startTime endTime [] |
| 89 | + |
| 90 | +-- | The meeting creator (who receives the meeting events) must not see them |
| 91 | +-- via a V14 fetch, while a current-version fetch of the same window shows |
| 92 | +-- them; V14 pagination terminates. |
| 93 | +testVersionedNotificationsHideMeetingEvents :: (HasCallStack) => App () |
| 94 | +testVersionedNotificationsHideMeetingEvents = do |
| 95 | + (alice, _tid, _members) <- createTeam OwnDomain 1 |
| 96 | + meeting <- mkMeeting |
| 97 | + |
| 98 | + withWebSocket alice $ \wsAlice -> do |
| 99 | + resp <- postMeetings alice meeting |
| 100 | + assertSuccess resp |
| 101 | + -- the current-version websocket sees the meeting events |
| 102 | + void $ awaitMatch isConvCreateMeetingNotif wsAlice |
| 103 | + void $ awaitMatch isMeetingCreateNotif wsAlice |
| 104 | + |
| 105 | + v14Types <- drainNotificationsAt alice 14 |
| 106 | + filter isGated v14Types `shouldMatch` ([] :: [String]) |
| 107 | + |
| 108 | + curTypes <- drainNotificationsAt alice 17 |
| 109 | + curTypes `shouldContain` ["conversation.create-meeting"] |
| 110 | + curTypes `shouldContain` ["meeting.create"] |
| 111 | + |
| 112 | +-- | A V14 client still sees non-meeting events (e.g. conversation.create) |
| 113 | +-- while meeting events are filtered from the same window. |
| 114 | +testVersionedNotificationsKeepNonMeetingEvents :: (HasCallStack) => App () |
| 115 | +testVersionedNotificationsKeepNonMeetingEvents = do |
| 116 | + (alice, tid, [bob]) <- createTeam OwnDomain 2 |
| 117 | + resp <- |
| 118 | + postConversation |
| 119 | + alice |
| 120 | + defProteus |
| 121 | + { qualifiedUsers = [bob], |
| 122 | + name = Just "plain conv", |
| 123 | + team = Just tid |
| 124 | + } |
| 125 | + assertSuccess resp |
| 126 | + meeting <- mkMeeting |
| 127 | + mresp <- postMeetings alice meeting |
| 128 | + assertSuccess mresp |
| 129 | + |
| 130 | + -- bob sees the plain conversation event, but no meeting events (the |
| 131 | + -- meeting's gated events go to alice, and none leak to bob at V14). |
| 132 | + bobTypes <- drainNotificationsAt bob 14 |
| 133 | + bobTypes `shouldContain` ["conversation.create"] |
| 134 | + filter isGated bobTypes `shouldMatch` ([] :: [String]) |
| 135 | + |
| 136 | + aliceTypes <- drainNotificationsAt alice 14 |
| 137 | + aliceTypes `shouldContain` ["conversation.create"] |
| 138 | + filter isGated aliceTypes `shouldMatch` ([] :: [String]) |
| 139 | + |
| 140 | +-- | A websocket connected at a low version receives no meeting event frames, |
| 141 | +-- while a current-version connection of the same user does. |
| 142 | +testVersionedWebSocketFiltersMeetingEvents :: (HasCallStack) => App () |
| 143 | +testVersionedWebSocketFiltersMeetingEvents = do |
| 144 | + (alice, _tid, _members) <- createTeam OwnDomain 1 |
| 145 | + aliceId <- alice %. "id" >>= asString |
| 146 | + aliceDomain <- objDomain alice |
| 147 | + meeting <- mkMeeting |
| 148 | + |
| 149 | + let lowV = WSConnect aliceId aliceDomain Nothing (Just "lowconn") (Just 14) |
| 150 | + highV = WSConnect aliceId aliceDomain Nothing (Just "highconn") Nothing |
| 151 | + |
| 152 | + withWebSocket lowV $ \wsLow -> |
| 153 | + withWebSocket highV $ \wsHigh -> do |
| 154 | + resp <- postMeetings alice meeting |
| 155 | + assertSuccess resp |
| 156 | + -- current version gets the meeting events ... |
| 157 | + void $ awaitMatch isConvCreateMeetingNotif wsHigh |
| 158 | + void $ awaitMatch isMeetingCreateNotif wsHigh |
| 159 | + -- ... the low version does not (allow some time for delivery) |
| 160 | + liftIO $ threadDelay 1_000_000 |
| 161 | + assertNoEvent 1 wsLow |
| 162 | + |
| 163 | +-- | The rabbitmq-backed /events websocket of a low-version client skips (and |
| 164 | +-- server-side acks) meeting event frames, while a current-version connection |
| 165 | +-- of the same user receives them. Tolerant drain: stray ungated events are |
| 166 | +-- allowed on the low socket, gated ones are not. |
| 167 | +testVersionedEventsSocketFiltersMeetingEvents :: (HasCallStack) => App () |
| 168 | +testVersionedEventsSocketFiltersMeetingEvents = |
| 169 | + withModifiedBackend (enableConsumableNotifications def) $ \domain -> do |
| 170 | + (alice, _tid, _members) <- createTeam domain 1 |
| 171 | + -- mirror the other temp-/events tests in Test.Events: create a |
| 172 | + -- consumable-notifications client for alice |
| 173 | + void $ addClient alice def {acapabilities = Just ["consumable-notifications"]} >>= getJSON 201 |
| 174 | + -- Two temp queues with no client id (each binds userRoutingKey and gets its |
| 175 | + -- own version-filtered consumer); sharing a client id would round-robin a |
| 176 | + -- single queue and race. |
| 177 | + runCodensity (createEventsWebSocketAtVersion alice Nothing 14) $ \wsLow -> |
| 178 | + runCodensity (createEventsWebSocketAtVersion alice Nothing 17) $ \wsHigh -> do |
| 179 | + meeting <- mkMeeting |
| 180 | + postMeetings alice meeting >>= assertSuccess |
| 181 | + assertFindsEvent wsHigh $ \e -> do |
| 182 | + e %. "type" `shouldMatch` "event" |
| 183 | + t <- e %. "data.event.payload.0.type" >>= asString |
| 184 | + unless (isGated t) |
| 185 | + $ assertFailure ("expected a gated meeting event on the V17 socket, got: " <> t) |
| 186 | + ackEvent wsHigh e |
| 187 | + -- allow some time for delivery, then tolerate stray ungated events on |
| 188 | + -- the low socket but assert that none of them is gated |
| 189 | + liftIO $ threadDelay 1_000_000 |
| 190 | + drained <- consumeAllEventsNoAck wsLow |
| 191 | + types <- traverse (\e -> e %. "data.event.payload.0.type" >>= asString) drained |
| 192 | + filter isGated types `shouldMatch` ([] :: [String]) |
| 193 | + |
| 194 | +-- | A V14 notification cursor is not stranded on an all-gated page: the |
| 195 | +-- gundeck refill loop must skip past a fully-gated page (server minimum page |
| 196 | +-- size is 100) and still deliver a later ungated event. |
| 197 | +testVersionedNotificationsRefillPastGatedBacklog :: (HasCallStack) => App () |
| 198 | +testVersionedNotificationsRefillPastGatedBacklog = do |
| 199 | + (alice, _tid, _members) <- createTeam OwnDomain 1 |
| 200 | + -- 101 meetings (~30-60 s by design) guarantee > 100 gated notification rows |
| 201 | + -- for the creator even if galley batches conversation.create-meeting and |
| 202 | + -- meeting.create into a single row. |
| 203 | + replicateM_ 101 $ do |
| 204 | + meeting <- mkMeeting |
| 205 | + postMeetings alice meeting >>= assertSuccess |
| 206 | + -- The ungated event must land strictly after the gated backlog (gundeck |
| 207 | + -- persists notifications synchronously in the request path). If that ever |
| 208 | + -- becomes async, user.update could land inside the first 100 rows and this |
| 209 | + -- test would silently degrade to never exercising the refill loop. |
| 210 | + handle <- randomHandle |
| 211 | + putHandle alice handle >>= assertSuccess |
| 212 | + |
| 213 | + v14 <- drainNotificationsAt alice 14 |
| 214 | + v14 `shouldContain` ["user.update"] |
| 215 | + filter isGated v14 `shouldMatch` ([] :: [String]) |
| 216 | + |
| 217 | + v17 <- drainNotificationsAt alice 17 |
| 218 | + v17 `shouldContain` ["meeting.create"] |
0 commit comments