11import { NetworkAsCodeClient } from "../src" ;
22import "dotenv/config" ;
33import { Device } from "../src/models/device" ;
4- import { configureClient } from "./configClient" ;
4+ import { configureClient , configureNotificationServerUrl } from "./configClient" ;
5+ import { ProxyAgent } from "proxy-agent" ;
6+ import fetch from "node-fetch" ;
7+
58
69let client : NetworkAsCodeClient ;
710let device : Device ;
11+ let notificationUrl : string ;
12+ let agent : ProxyAgent
13+
814
915beforeAll ( ( ) => {
1016 client = configureClient ( ) ;
@@ -16,20 +22,38 @@ beforeAll(() => {
1622 publicPort : 80 ,
1723 } ,
1824 } ) ;
25+ notificationUrl = configureNotificationServerUrl ( ) ;
26+ agent = new ProxyAgent ( ) ;
1927} ) ;
2028
2129describe ( "Device Status" , ( ) => {
2230 it ( "can create a connectivity subscription and delete it" , async ( ) => {
2331 const subscription = await client . deviceStatus . subscribe (
2432 device ,
2533 "org.camaraproject.device-status.v0.connectivity-data" ,
26- "https://example.com/ notify"
34+ ` ${ notificationUrl } / notify`
2735 ) ;
28-
2936 expect ( subscription . eventSubscriptionId ) . toBeDefined ( ) ;
3037
38+ await new Promise ( resolve => setTimeout ( resolve , 5 * 1000 ) ) ;
39+ let notification = await fetch ( `${ notificationUrl } /device-status/get/${ subscription . eventSubscriptionId } ` ,
40+ {
41+ method : "GET" ,
42+ agent : agent
43+ }
44+ ) ;
45+
46+ const data = await notification . json ( ) ;
47+
48+ expect ( data ) . not . toBeNull ( ) ;
49+ notification = await fetch ( `${ notificationUrl } /device-status/delete/${ subscription . eventSubscriptionId } ` ,
50+ {
51+ method : 'DELETE' ,
52+ agent : agent
53+ } ) ;
54+
3155 subscription . delete ( ) ;
32- } ) ;
56+ } , 20 * 1000 ) ;
3357
3458 it ( "can create a connectivity subscription with expiry" , async ( ) => {
3559 const tomorrowDate = new Date ( Date . now ( ) + 24 * 60 * 60 * 1000 ) ;
@@ -38,7 +62,7 @@ describe("Device Status", () => {
3862 const subscription = await client . deviceStatus . subscribe (
3963 device ,
4064 "org.camaraproject.device-status.v0.connectivity-data" ,
41- "https://example.com/ notify" ,
65+ ` ${ notificationUrl } / notify` ,
4266 {
4367 subscriptionExpireTime : tomorrowDate ,
4468 }
@@ -48,14 +72,31 @@ describe("Device Status", () => {
4872 new Date ( tomorrowDate . toISOString ( ) . replace ( ".000" , "" ) )
4973 ) ;
5074
75+ await new Promise ( resolve => setTimeout ( resolve , 5 * 1000 ) ) ;
76+ let notification = await fetch ( `${ notificationUrl } /device-status/get/${ subscription . eventSubscriptionId } ` ,
77+ {
78+ method : "GET" ,
79+ agent : agent
80+ }
81+ ) ;
82+
83+ const data = await notification . json ( ) ;
84+
85+ expect ( data ) . not . toBeNull ( ) ;
86+ notification = await fetch ( `${ notificationUrl } /device-status/delete/${ subscription . eventSubscriptionId } ` ,
87+ {
88+ method : 'DELETE' ,
89+ agent : agent
90+ } ) ;
91+
5192 subscription . delete ( ) ;
52- } ) ;
93+ } , 20 * 1000 ) ;
5394
5495 it ( "can create a connectivity subscription with other optional arguments" , async ( ) => {
5596 const subscription = await client . deviceStatus . subscribe (
5697 device ,
5798 "org.camaraproject.device-status.v0.connectivity-data" ,
58- "https://example.com/ notify" ,
99+ ` ${ notificationUrl } / notify` ,
59100 {
60101 maxNumberOfReports : 2 ,
61102 notificationAuthToken : "my-token" ,
@@ -66,8 +107,25 @@ describe("Device Status", () => {
66107 expect ( subscription . maxNumOfReports ) . toEqual ( 2 ) ;
67108 expect ( subscription . notificationAuthToken ) . toEqual ( "my-token" ) ;
68109
110+ await new Promise ( resolve => setTimeout ( resolve , 5 * 1000 ) ) ;
111+ let notification = await fetch ( `${ notificationUrl } /device-status/get/${ subscription . eventSubscriptionId } ` ,
112+ {
113+ method : "GET" ,
114+ agent : agent
115+ }
116+ ) ;
117+
118+ const data = await notification . json ( ) ;
119+
120+ expect ( data ) . not . toBeNull ( ) ;
121+ notification = await fetch ( `${ notificationUrl } /device-status/delete/${ subscription . eventSubscriptionId } ` ,
122+ {
123+ method : 'DELETE' ,
124+ agent : agent
125+ } ) ;
126+
69127 subscription . delete ( ) ;
70- } ) ;
128+ } , 20 * 1000 ) ;
71129
72130 it ( "can get a subscription by id" , async ( ) => {
73131 const subscription = await client . deviceStatus . subscribe (
0 commit comments