forked from ringcentral/RingCentral.Net
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SubscriptionTest.cs
89 lines (85 loc) · 3.72 KB
/
SubscriptionTest.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using RingCentral.Net.PubNub;
using Xunit;
namespace RingCentral.Tests
{
public class SubscriptionTest
{
private Task<GetSMSMessageInfoResponse> SendSms(RestClient rc)
{
return rc.Restapi().Account().Extension().Sms().Post(new CreateSMSMessage
{
from = new MessageStoreCallerInfoRequest
{
phoneNumber = Environment.GetEnvironmentVariable("RINGCENTRAL_USERNAME")
},
to = new[]
{
new MessageStoreCallerInfoRequest
{
phoneNumber = Environment.GetEnvironmentVariable("RINGCENTRAL_RECEIVER")
}
},
text = "Hello world"
});
}
[Fact]
public async void TestSetupSubscription()
{
using (var rc = new RestClient(
Environment.GetEnvironmentVariable("RINGCENTRAL_CLIENT_ID"),
Environment.GetEnvironmentVariable("RINGCENTRAL_CLIENT_SECRET"),
Environment.GetEnvironmentVariable("RINGCENTRAL_SERVER_URL")
))
{
await rc.Authorize(
Environment.GetEnvironmentVariable("RINGCENTRAL_USERNAME"),
Environment.GetEnvironmentVariable("RINGCENTRAL_EXTENSION"),
Environment.GetEnvironmentVariable("RINGCENTRAL_PASSWORD")
);
var pubNubExtension = new PubNubExtension();
rc.InstallExtension(pubNubExtension);
var eventFilters = new[]
{
"/restapi/v1.0/account/~/extension/~/presence?detailedTelephonyState=true",
"/restapi/v1.0/account/~/extension/~/message-store"
};
var messages = new List<string>();
var messageStoreMessageCount = 0;
var subscription = await pubNubExtension.Subscribe(eventFilters, message =>
{
messages.Add(message);
dynamic jObject = JObject.Parse(message);
var eventString = (string) jObject.@event;
if (new Regex("/account/\\d+/extension/\\d+/message-store").Match(eventString).Success)
{
messageStoreMessageCount += 1;
var messageEvent = JsonConvert.DeserializeObject<MessageEvent>(message);
Assert.NotNull(messageEvent.uuid);
Assert.NotNull(messageEvent.body);
}
});
var subscriptionInfo = subscription.SubscriptionInfo;
Assert.NotNull(subscriptionInfo);
Assert.NotNull(subscription.SubscriptionInfo);
Assert.Equal(eventFilters.Length, subscriptionInfo.eventFilters.Length);
await SendSms(rc);
await System.Threading.Tasks.Task.Delay(20000);
Assert.True(messages.Count >= 1);
Assert.True(messageStoreMessageCount >= 1);
Assert.Contains(messages, message => message.Contains("message-store"));
subscriptionInfo = await subscription.Refresh();
Assert.NotNull(subscriptionInfo);
Assert.NotNull(subscription.SubscriptionInfo);
Assert.Equal(eventFilters.Length, subscriptionInfo.eventFilters.Length);
await subscription.Revoke();
Assert.Null(subscription.SubscriptionInfo);
}
}
}
}