Skip to content

Commit df3e560

Browse files
p1gp1gepoberezkin
authored andcommitted
WIP: add test to check webpush requests from ntf server
1 parent ea9adf1 commit df3e560

File tree

3 files changed

+148
-67
lines changed

3 files changed

+148
-67
lines changed

tests/AgentTests/NotificationTests.hs

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -217,11 +217,11 @@ runNtfTestCfg (t, msType) baseId smpCfg ntfCfg' aCfg bCfg runTest = do
217217
threadDelay 100000
218218

219219
testNotificationToken :: APNSMockServer -> IO ()
220-
testNotificationToken apns = do
220+
testNotificationToken (APNSMockServer apns) = do
221221
withAgent 1 agentCfg initAgentServers testDB $ \a -> runRight_ $ do
222222
let tkn = APNSDeviceToken PPApnsTest "abcd"
223223
NTRegistered <- registerNtfToken a tkn NMPeriodic
224-
APNSMockRequest {notification = APNSNotification {aps = APNSBackground _, notificationData = Just ntfData}} <-
224+
PushMockRequest {notification = APNSNotification {aps = APNSBackground _, notificationData = Just ntfData}} <-
225225
getMockNotification apns tkn
226226
verification <- ntfData .-> "verification"
227227
nonce <- C.cbNonce <$> ntfData .-> "nonce"
@@ -241,16 +241,16 @@ v .-> key = do
241241
-- logCfg = LogConfig {lc_file = Nothing, lc_stderr = True}
242242

243243
testNtfTokenRepeatRegistration :: APNSMockServer -> IO ()
244-
testNtfTokenRepeatRegistration apns = do
244+
testNtfTokenRepeatRegistration (APNSMockServer apns) = do
245245
withAgent 1 agentCfg initAgentServers testDB $ \a -> runRight_ $ do
246246
let tkn = APNSDeviceToken PPApnsTest "abcd"
247247
NTRegistered <- registerNtfToken a tkn NMPeriodic
248-
APNSMockRequest {notification = APNSNotification {aps = APNSBackground _, notificationData = Just ntfData}} <-
248+
PushMockRequest {notification = APNSNotification {aps = APNSBackground _, notificationData = Just ntfData}} <-
249249
getMockNotification apns tkn
250250
verification <- ntfData .-> "verification"
251251
nonce <- C.cbNonce <$> ntfData .-> "nonce"
252252
NTRegistered <- registerNtfToken a tkn NMPeriodic
253-
APNSMockRequest {notification = APNSNotification {aps = APNSBackground _, notificationData = Just ntfData'}} <-
253+
PushMockRequest {notification = APNSNotification {aps = APNSBackground _, notificationData = Just ntfData'}} <-
254254
getMockNotification apns tkn
255255
_ <- ntfData' .-> "verification"
256256
_ <- C.cbNonce <$> ntfData' .-> "nonce"
@@ -260,18 +260,18 @@ testNtfTokenRepeatRegistration apns = do
260260
pure ()
261261

262262
testNtfTokenSecondRegistration :: APNSMockServer -> IO ()
263-
testNtfTokenSecondRegistration apns =
263+
testNtfTokenSecondRegistration (APNSMockServer apns) =
264264
withAgentClients2 $ \a a' -> runRight_ $ do
265265
let tkn = APNSDeviceToken PPApnsTest "abcd"
266266
NTRegistered <- registerNtfToken a tkn NMPeriodic
267-
APNSMockRequest {notification = APNSNotification {aps = APNSBackground _, notificationData = Just ntfData}} <-
267+
PushMockRequest {notification = APNSNotification {aps = APNSBackground _, notificationData = Just ntfData}} <-
268268
getMockNotification apns tkn
269269
verification <- ntfData .-> "verification"
270270
nonce <- C.cbNonce <$> ntfData .-> "nonce"
271271
verifyNtfToken a tkn nonce verification
272272

273273
NTRegistered <- registerNtfToken a' tkn NMPeriodic
274-
APNSMockRequest {notification = APNSNotification {aps = APNSBackground _, notificationData = Just ntfData'}} <-
274+
PushMockRequest {notification = APNSNotification {aps = APNSBackground _, notificationData = Just ntfData'}} <-
275275
getMockNotification apns tkn
276276
verification' <- ntfData' .-> "verification"
277277
nonce' <- C.cbNonce <$> ntfData' .-> "nonce"
@@ -290,12 +290,12 @@ testNtfTokenSecondRegistration apns =
290290
pure ()
291291

292292
testNtfTokenServerRestart :: ASrvTransport -> APNSMockServer -> IO ()
293-
testNtfTokenServerRestart t apns = do
293+
testNtfTokenServerRestart t (APNSMockServer apns) = do
294294
let tkn = APNSDeviceToken PPApnsTest "abcd"
295295
ntfData <- withAgent 1 agentCfg initAgentServers testDB $ \a ->
296296
withNtfServer t $ runRight $ do
297297
NTRegistered <- registerNtfToken a tkn NMPeriodic
298-
APNSMockRequest {notification = APNSNotification {aps = APNSBackground _, notificationData = Just ntfData}} <-
298+
PushMockRequest {notification = APNSNotification {aps = APNSBackground _, notificationData = Just ntfData}} <-
299299
getMockNotification apns tkn
300300
pure ntfData
301301
-- the new agent is created as otherwise when running the tests in CI the old agent was keeping the connection to the server
@@ -311,12 +311,12 @@ testNtfTokenServerRestart t apns = do
311311
pure ()
312312

313313
testNtfTokenServerRestartReverify :: ASrvTransport -> APNSMockServer -> IO ()
314-
testNtfTokenServerRestartReverify t apns = do
314+
testNtfTokenServerRestartReverify t (APNSMockServer apns) = do
315315
let tkn = APNSDeviceToken PPApnsTest "abcd"
316316
withAgent 1 agentCfg initAgentServers testDB $ \a -> do
317317
ntfData <- withNtfServer t $ runRight $ do
318318
NTRegistered <- registerNtfToken a tkn NMPeriodic
319-
APNSMockRequest {notification = APNSNotification {aps = APNSBackground _, notificationData = Just ntfData}} <-
319+
PushMockRequest {notification = APNSNotification {aps = APNSBackground _, notificationData = Just ntfData}} <-
320320
getMockNotification apns tkn
321321
pure ntfData
322322
runRight_ $ do
@@ -334,12 +334,12 @@ testNtfTokenServerRestartReverify t apns = do
334334
pure ()
335335

336336
testNtfTokenServerRestartReverifyTimeout :: ASrvTransport -> APNSMockServer -> IO ()
337-
testNtfTokenServerRestartReverifyTimeout t apns = do
337+
testNtfTokenServerRestartReverifyTimeout t (APNSMockServer apns) = do
338338
let tkn = APNSDeviceToken PPApnsTest "abcd"
339339
withAgent 1 agentCfg initAgentServers testDB $ \a@AgentClient {agentEnv = Env {store}} -> do
340340
(nonce, verification) <- withNtfServer t $ runRight $ do
341341
NTRegistered <- registerNtfToken a tkn NMPeriodic
342-
APNSMockRequest {notification = APNSNotification {aps = APNSBackground _, notificationData = Just ntfData}} <-
342+
PushMockRequest {notification = APNSNotification {aps = APNSBackground _, notificationData = Just ntfData}} <-
343343
getMockNotification apns tkn
344344
verification <- ntfData .-> "verification"
345345
nonce <- C.cbNonce <$> ntfData .-> "nonce"
@@ -369,12 +369,12 @@ testNtfTokenServerRestartReverifyTimeout t apns = do
369369
pure ()
370370

371371
testNtfTokenServerRestartReregister :: ASrvTransport -> APNSMockServer -> IO ()
372-
testNtfTokenServerRestartReregister t apns = do
372+
testNtfTokenServerRestartReregister t (APNSMockServer apns) = do
373373
let tkn = APNSDeviceToken PPApnsTest "abcd"
374374
withAgent 1 agentCfg initAgentServers testDB $ \a ->
375375
withNtfServer t $ runRight $ do
376376
NTRegistered <- registerNtfToken a tkn NMPeriodic
377-
APNSMockRequest {notification = APNSNotification {aps = APNSBackground _, notificationData = Just _}} <-
377+
PushMockRequest {notification = APNSNotification {aps = APNSBackground _, notificationData = Just _}} <-
378378
getMockNotification apns tkn
379379
pure ()
380380
-- the new agent is created as otherwise when running the tests in CI the old agent was keeping the connection to the server
@@ -384,7 +384,7 @@ testNtfTokenServerRestartReregister t apns = do
384384
-- so that repeat registration happens when client is restarted.
385385
withNtfServer t $ runRight_ $ do
386386
NTRegistered <- registerNtfToken a' tkn NMPeriodic
387-
APNSMockRequest {notification = APNSNotification {aps = APNSBackground _, notificationData = Just ntfData}} <-
387+
PushMockRequest {notification = APNSNotification {aps = APNSBackground _, notificationData = Just ntfData}} <-
388388
getMockNotification apns tkn
389389
verification <- ntfData .-> "verification"
390390
nonce <- C.cbNonce <$> ntfData .-> "nonce"
@@ -393,12 +393,12 @@ testNtfTokenServerRestartReregister t apns = do
393393
pure ()
394394

395395
testNtfTokenServerRestartReregisterTimeout :: ASrvTransport -> APNSMockServer -> IO ()
396-
testNtfTokenServerRestartReregisterTimeout t apns = do
396+
testNtfTokenServerRestartReregisterTimeout t (APNSMockServer apns) = do
397397
let tkn = APNSDeviceToken PPApnsTest "abcd"
398398
withAgent 1 agentCfg initAgentServers testDB $ \a@AgentClient {agentEnv = Env {store}} -> do
399399
withNtfServer t $ runRight $ do
400400
NTRegistered <- registerNtfToken a tkn NMPeriodic
401-
APNSMockRequest {notification = APNSNotification {aps = APNSBackground _, notificationData = Just _}} <-
401+
PushMockRequest {notification = APNSNotification {aps = APNSBackground _, notificationData = Just _}} <-
402402
getMockNotification apns tkn
403403
pure ()
404404
-- this emulates the situation when server registered token but the client did not receive the response
@@ -419,7 +419,7 @@ testNtfTokenServerRestartReregisterTimeout t apns = do
419419
-- so that repeat registration happens when client is restarted.
420420
withNtfServer t $ runRight_ $ do
421421
NTRegistered <- registerNtfToken a' tkn NMPeriodic
422-
APNSMockRequest {notification = APNSNotification {aps = APNSBackground _, notificationData = Just ntfData}} <-
422+
PushMockRequest {notification = APNSNotification {aps = APNSBackground _, notificationData = Just ntfData}} <-
423423
getMockNotification apns tkn
424424
verification <- ntfData .-> "verification"
425425
nonce <- C.cbNonce <$> ntfData .-> "nonce"
@@ -434,14 +434,14 @@ getTestNtfTokenPort a =
434434
Nothing -> error "no active NtfToken"
435435

436436
testNtfTokenMultipleServers :: ASrvTransport -> APNSMockServer -> IO ()
437-
testNtfTokenMultipleServers t apns = do
437+
testNtfTokenMultipleServers t (APNSMockServer apns) = do
438438
let tkn = APNSDeviceToken PPApnsTest "abcd"
439439
withAgent 1 agentCfg initAgentServers2 testDB $ \a ->
440440
withNtfServerThreadOn t ntfTestPort ntfTestDBCfg $ \ntf ->
441441
withNtfServerThreadOn t ntfTestPort2 ntfTestDBCfg2 $ \ntf2 -> runRight_ $ do
442442
-- register a new token, the agent picks a server and stores its choice
443443
NTRegistered <- registerNtfToken a tkn NMPeriodic
444-
APNSMockRequest {notification = APNSNotification {aps = APNSBackground _, notificationData = Just ntfData}} <-
444+
PushMockRequest {notification = APNSNotification {aps = APNSBackground _, notificationData = Just ntfData}} <-
445445
getMockNotification apns tkn
446446
verification <- ntfData .-> "verification"
447447
nonce <- C.cbNonce <$> ntfData .-> "nonce"
@@ -544,7 +544,7 @@ testRunNTFServerTests t srv =
544544
testProtocolServer a NRMInteractive 1 $ ProtoServerWithAuth srv Nothing
545545

546546
testNotificationSubscriptionExistingConnection :: APNSMockServer -> AgentMsgId -> AgentClient -> AgentClient -> IO ()
547-
testNotificationSubscriptionExistingConnection apns baseId alice@AgentClient {agentEnv = Env {config = aliceCfg, store}} bob = do
547+
testNotificationSubscriptionExistingConnection (APNSMockServer apns) baseId alice@AgentClient {agentEnv = Env {config = aliceCfg, store}} bob = do
548548
(bobId, aliceId, nonce, message) <- runRight $ do
549549
-- establish connection
550550
(bobId, qInfo) <- createConnection alice 1 True SCMInvitation Nothing SMSubscribe
@@ -557,7 +557,7 @@ testNotificationSubscriptionExistingConnection apns baseId alice@AgentClient {ag
557557
-- register notification token
558558
let tkn = APNSDeviceToken PPApnsTest "abcd"
559559
NTRegistered <- registerNtfToken alice tkn NMInstant
560-
APNSMockRequest {notification = APNSNotification {aps = APNSBackground _, notificationData = Just ntfData}} <-
560+
PushMockRequest {notification = APNSNotification {aps = APNSBackground _, notificationData = Just ntfData}} <-
561561
getMockNotification apns tkn
562562
verification <- ntfData .-> "verification"
563563
vNonce <- C.cbNonce <$> ntfData .-> "nonce"
@@ -567,7 +567,7 @@ testNotificationSubscriptionExistingConnection apns baseId alice@AgentClient {ag
567567
1 <- msgId <$> sendMessage bob aliceId (SMP.MsgFlags True) "hello"
568568
get bob ##> ("", aliceId, SENT $ baseId + 1)
569569
-- notification
570-
(nonce, message) <- messageNotification apns tkn
570+
(nonce, message) <- messageNotification (APNSMockServer apns) tkn
571571
pure (bobId, aliceId, nonce, message)
572572

573573
Right [NotificationInfo {ntfConnId = cId, ntfMsgMeta = Just NMsgMeta {msgTs}}] <- runExceptT $ getNotificationConns alice nonce message
@@ -600,7 +600,7 @@ testNotificationSubscriptionExistingConnection apns baseId alice@AgentClient {ag
600600
2 <- msgId <$> sendMessage bob aliceId (SMP.MsgFlags True) "hello again"
601601
get bob ##> ("", aliceId, SENT $ baseId + 2)
602602
-- no notifications should follow
603-
noNotification alice apns
603+
noNotification alice $ APNSMockServer apns
604604
where
605605
msgId = subtract baseId
606606

@@ -645,10 +645,10 @@ testNotificationSubscriptionNewConnection apns baseId alice bob =
645645
msgId = subtract baseId
646646

647647
registerTestToken :: AgentClient -> ByteString -> NotificationsMode -> APNSMockServer -> ExceptT AgentErrorType IO DeviceToken
648-
registerTestToken a token mode apns = do
648+
registerTestToken a token mode (APNSMockServer apns) = do
649649
let tkn = APNSDeviceToken PPApnsTest token
650650
NTRegistered <- registerNtfToken a tkn mode
651-
Just APNSMockRequest {notification = APNSNotification {aps = APNSBackground _, notificationData = Just ntfData'}} <-
651+
Just PushMockRequest {notification = APNSNotification {aps = APNSBackground _, notificationData = Just ntfData'}} <-
652652
timeout 1000000 $ getMockNotification apns tkn
653653
verification' <- ntfData' .-> "verification"
654654
nonce' <- C.cbNonce <$> ntfData' .-> "nonce"
@@ -1032,10 +1032,10 @@ testMessage_ apns a aId b bId msg = do
10321032
ackMessage a bId msgId Nothing
10331033

10341034
messageNotification :: HasCallStack => APNSMockServer -> DeviceToken -> ExceptT AgentErrorType IO (C.CbNonce, ByteString)
1035-
messageNotification apns tkn = do
1035+
messageNotification (APNSMockServer apns) tkn = do
10361036
500000 `timeout` getMockNotification apns tkn >>= \case
10371037
Nothing -> error "no notification"
1038-
Just APNSMockRequest {notification = APNSNotification {aps = APNSMutableContent {}, notificationData = Just ntfData}} -> do
1038+
Just PushMockRequest {notification = APNSNotification {aps = APNSMutableContent {}, notificationData = Just ntfData}} -> do
10391039
nonce <- C.cbNonce <$> ntfData .-> "nonce"
10401040
message <- ntfData .-> "message"
10411041
pure (nonce, message)
@@ -1049,14 +1049,14 @@ messageNotificationData c apns = do
10491049
pure $ L.last pnMsgs
10501050

10511051
noNotification :: AgentClient -> APNSMockServer -> ExceptT AgentErrorType IO ()
1052-
noNotification c apns = do
1052+
noNotification c (APNSMockServer apns) = do
10531053
NtfToken {deviceToken} <- getNtfTokenData c
10541054
500000 `timeout` getMockNotification apns deviceToken >>= \case
10551055
Nothing -> pure ()
10561056
_ -> error "unexpected notification"
10571057

10581058
noNotifications :: APNSMockServer -> ExceptT AgentErrorType IO ()
1059-
noNotifications apns = do
1059+
noNotifications (APNSMockServer apns) = do
10601060
500000 `timeout` getAnyMockNotification apns >>= \case
10611061
Nothing -> pure ()
10621062
_ -> error "unexpected notification"

0 commit comments

Comments
 (0)