· 7 years ago · Mar 01, 2018, 02:20 PM
1@Stepwise
2@Category(IntTest)
3class ChannelWebhookSubscriptionSpec extends Specification {
4
5 static Environment environment = Environment.selectFromNameInEnvProperty();
6 static RESTClient apiSrvClient;
7 static RESTClient vkSrvClient;
8
9 static Sql archiveSql
10 static Sql vkSql
11
12
13 static ServiceBusSubscription commandsListener
14
15 static String vkSrvBaseUrl = environment.properties.getProperty("VK_SRV_BASE_URL")
16
17 static String testAccountId = UUID.fromString(environment.properties.getProperty("TEST_ACCOUNT_ID")) as String;
18
19 static String testGroupId = environment.properties.getProperty("TEST_VK_CHANNEL_ID") as Integer;
20
21 static Integer chatId = environment.properties.getProperty("TEST_VK_CHAT_ID") as Integer;
22
23 static final String TEST_CHAT_ID = "testChat321"
24
25 static Integer channelId = environment.properties.getProperty("TEST_VK_CHANNEL_ID") as Integer;
26
27 static Integer remoteAddress = environment.properties.getProperty("TEST_VK_CHAT_ID") as Integer;
28
29 static String deepLinkId;
30
31 def setupSpec() {
32 apiSrvClient = environment.apiSrv.httpClient;
33 vkSrvClient = environment.vkSrv.httpClient;
34
35 archiveSql = Environment.selectFromNameInEnvProperty().archiveSrv.sqlClient;
36 vkSql = Environment.selectFromNameInEnvProperty().vkSrv.sqlClient;
37
38 commandsListener = Environment.selectFromNameInEnvProperty().serviceBus.messagingCommandsTopic.createTempSubscription();
39 }
40
41 def cleanupSpec() {
42 commandsListener?.delete();
43 }
44
45 def "Deep link creation"() {
46 given:
47 def deepLink = [
48 "accountId": testAccountId
49 ]
50
51 when:
52 def response = apiSrvClient.post(
53 path: '/api/endUserNotifications/deepLinks',
54 body: deepLink,
55 requestContentType: ContentType.JSON
56 )
57 deepLinkId = response.body['$value']
58 then: "deep link was created"
59 response.status == 200
60 response.body.$error == null
61 response.body['$value'] instanceof String
62 response.body['$value'].startsWith("subscribe_")
63 }
64
65 def "Deep link activation"() {
66 given:
67 def webhookKey = '1153AFBD-6E69-4C8A-B78B-E3828C6AFD99'.toLowerCase();
68 //def secretKey = 'ece9c236';
69 def webhookSubscription = [
70 "type" : "message_allow",
71 "object" : [
72 "user_id": chatId,
73 "key" : deepLinkId
74 ],
75 "groupId": testGroupId
76 ]
77 when:
78 def response = vkSrvClient.post(
79 path : 'webhook/' + webhookKey,
80 body : webhookSubscription,
81 requestContentType: ContentType.JSON
82 )
83 def receivedMessageEnvelope = commandsListener.receiveMessage(3) {
84 def body = new JsonSlurper().parse(it.body);
85 return body.name == "SubscriptionCreated"
86 }
87 then: "subscription with deep link was created"
88 receivedMessageEnvelope != null
89 with(receivedMessageEnvelope.customProperties) {
90 TB_CHANNEL == 'vk'
91 TB_CHANNEL_ID as Integer == -1
92 TB_ACCOUNT_ID as String == 22
93 }
94 def receivedMessage = new JsonSlurper().parse(receivedMessageEnvelope.body);
95 receivedMessage.accountId == 22
96 receivedMessage.channel == "vk"
97 receivedMessage.channelId == -1
98 receivedMessage.text == webhookSubscription
99 then: "webhook controller worked correctly"
100 //select 1 webhookKey
101 //fix
102 //vkSql.firstRow("SELECT count(1) c FROM vk_srv_db.vkChannels WHERE accountId =?", testAccountId).c == 1
103
104 response.status == 200
105 response.body.$error == null
106 //response.body.$value.accountId == TEST_CHAT_ID
107 !(response.body.$value.remoteContact.empty)
108 }