· 7 years ago · Mar 01, 2018, 01:04 PM
1package integration.vk
2
3import categories.IntTest
4import common.Environment
5import common.ServiceBusSubscription
6import common.UUIDUtil
7import groovy.json.JsonSlurper
8import groovy.sql.Sql
9import groovyx.net.http.ContentType
10import groovyx.net.http.RESTClient
11import org.apache.commons.lang3.RandomStringUtils
12import org.junit.experimental.categories.Category
13import spock.lang.Specification
14import spock.lang.Stepwise
15
16import java.time.Instant
17
18@Stepwise
19@Category(IntTest)
20class ChannelWebhookSubscriptionSpec extends Specification {
21
22 static Environment environment = Environment.selectFromNameInEnvProperty();
23 static RESTClient apiSrvClient;
24 static RESTClient vkSrvClient;
25
26 static Sql archiveSql
27 static Sql vkSql
28
29
30 static ServiceBusSubscription commandsListener
31
32 //static String vkSrvBaseUrl = environment.properties.getProperty("VK_SRV_BASE_URL")
33
34 static String testAccountId = UUID.fromString(environment.properties.getProperty("TEST_ACCOUNT_ID")) as String;
35
36 static String testGroupId = environment.properties.getProperty("TEST_VK_CHANNEL_ID") as Integer;
37
38 static Integer chatId = environment.properties.getProperty("TEST_VK_CHAT_ID") as Integer;
39
40 static final String TEST_CHAT_ID = "testChat321"
41
42 static Integer channelId = environment.properties.getProperty("TEST_VK_CHANNEL_ID") as Integer;
43
44 static Integer remoteAddress = environment.properties.getProperty("TEST_VK_CHAT_ID") as Integer;
45
46 def setupSpec() {
47 apiSrvClient = environment.apiSrv.httpClient;
48 vkSrvClient = environment.vkSrv.httpClient;
49
50 archiveSql = Environment.selectFromNameInEnvProperty().archiveSrv.sqlClient;
51 vkSql = Environment.selectFromNameInEnvProperty().vkSrv.sqlClient;
52
53 commandsListener = Environment.selectFromNameInEnvProperty().serviceBus.messagingCommandsTopic.createTempSubscription();
54 }
55
56 def cleanupSpec() {
57 commandsListener?.delete();
58 }
59
60/* def "Deep link creation"() {
61 given:
62 def deepLink = [
63 "accountId": testAccountId
64 ]
65
66 when: "dispatch message was saved"
67 def response = apiSrvClient.post(
68 path: '/api/endUserNotifications/deepLinks',
69 body: deepLink,
70 requestContentType: ContentType.JSON
71 )
72
73 then: "deep link was created"
74 response.status == 200
75 response.body.$error == null
76 response.body['$value'] instanceof String
77 response.body['$value'].startsWith("subscribe_")
78 }*/
79
80 def "Deep link activation"() {
81 given:
82 def webhookKey = '1153AFBD-6E69-4C8A-B78B-E3828C6AFD99'.toLowerCase();
83 def secretKey = 'ece9c236';
84 def webhookSubscription = [
85 "type" : "message_allow",
86 "object" : [
87 "user_id": testAccountId,
88 "key" : webhookKey
89 ],
90 "groupId": testGroupId,
91 "secret" : secretKey
92 ]
93 when:
94 def response = vkSrvClient.post(
95 path : 'webhook/' + webhookKey,
96 body : webhookSubscription,
97 requestContentType: ContentType.JSON
98 )
99 def receivedMessageEnvelope = commandsListener.receiveMessage(3) {
100 def body = new JsonSlurper().parse(it.body);
101 return body.name == "SubscriptionCreated"
102 }
103 then: "subscription with deep link was created"
104 receivedMessageEnvelope != null
105 with(receivedMessageEnvelope.customProperties) {
106 TB_CHANNEL == 'vk'
107 TB_CHANNEL_ID as Integer == -1
108 TB_ACCOUNT_ID as String == 22
109 }
110 def receivedMessage = new JsonSlurper().parse(receivedMessageEnvelope?.body);
111 receivedMessage.accountId == 22
112 receivedMessage.channel == "vk"
113 receivedMessage.channelId == -1
114 receivedMessage.text == webhookSubscription
115 then: "webhook controller worked correctly"
116 //select 1 webhookKey
117 //fix
118 //vkSql.firstRow("SELECT count(1) c FROM vk_srv_db.vkChannels WHERE accountId =?", testAccountId).c == 1
119
120 response.status == 200
121 response.body.$error == null
122 response.body.$value.accountId == testAccountId
123 !(response.body.$value.remoteContact.empty)
124 }
125/*
126
127 def "User has subscribed"() {
128 when:
129 def requestBody = [
130 "accountId": testAccountId,
131 "address" : [
132 "channel" : "vk",
133 "channelId" : -1,
134 "accountId" : testAccountId,
135 "remoteAddress" : -1,
136 "chatUd" : TEST_CHAT_ID
137 ]
138 ]
139 def response = apiSrvClient.post(
140 path: 'api/endUserNotifications/subscriptions',
141 body: requestBody,
142 requestContentType: ContentType.JSON
143 )
144 receivedMessageEnvelope = commandsListener.receiveMessage(3) {
145 def body = new JsonSlurper().parse(it.body);
146 return body.name == "EndUserActivatedNotificationDeepLink"
147 }
148 then: "user activated deep link"
149 receivedMessageEnvelope != null
150 response.status == 200
151 response.body.$error == null
152 response.body.$value.accountId == testAccountId
153 response.body.$value.active == true
154
155*/
156/*
157 when:
158 response = apiSrvClient.get(
159 path: 'api/endUserNotifications/subscriptions',
160 requestContentType: ContentType.JSON
161 )
162 then: "user has subscribed"*//*
163
164 }
165*/
166
167
168 /*def "Subscription was activated"() {
169 given:
170 def requestBody = [
171 "accountId" : testAccountId,
172 "address" : [
173 "channel" : "vk",
174 "channelId" : channelId,
175 "accountId" : testAccountId,
176 "remoteAddress" : remoteAddress,
177 "chatId" : chatId
178 ]
179 ]
180 when:
181
182 then: "subscription was activated"
183 response.status == 200
184 response.body.$error == null
185 response.body.$value.active == true
186 response.body.$value.accountId == testAccountId
187 response.body.$value.address.channel == 'vk'
188 response.body.$value.address.channelId as Integer == channelId
189 response.body.$value.address.chatId as Integer == chatId
190 response.body.$value.address.chatId as Integer == chatId
191 response.body.$value.address.remoteAddress as Integer == remoteAddress
192 !(response.body.$value.remoteContact.empty)
193 }
194*/
195/* def "User received welcome message"() {
196 given:
197 def inputMessage1 = "ПриветÑтвенное Ñообщение!"
198 def firstName1 = RandomStringUtils.randomAlphanumeric(12);
199 def lastName1 = RandomStringUtils.randomAlphanumeric(12);
200 def messageId1 = UUIDUtil.sequentialUUID() as String;
201 def widgetId = archiveSql.firstRow("SELECT widgetId FROM op_srv_db.notificationWidgetChannel where channelId = ? AND channel = 'vk'", [channelId as Integer]);
202 def requestBody = [
203 "accountId" : testAccountId,
204 "channel" : "vk",
205 "channelId" : channelId,
206 "chatId" : chatId,
207 "id" : messageId1,
208 "text" : inputMessage1,
209 "remoteContact": [
210 "nationalName": [
211 "firstName" :firstName1,
212 "lastName" :lastName1,
213 "displayName" :firstName1 +" "+ lastName1,
214 ]
215 ],
216 "sentTimestamp" :Instant.now().toEpochMilli(),
217 "direction" :"outbound"
218 ];
219 when:
220 def response = apiSrvClient.get(
221 path: "api/endUserNotifications/widgets/${widgetId}",
222 requestContentType: ContentType.JSON
223 )
224
225
226 then: "welcome message exists"
227 widgetId != null
228 response.status == 200
229 response.body.$error == null
230 //response.body.$value.welcomeMessage.text != null
231 }*/
232
233}