-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtelemost_test.go
417 lines (313 loc) · 11.9 KB
/
telemost_test.go
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
package telemost
// ////////////////////////////////////////////////////////////////////////////////// //
// //
// Copyright (c) 2025 ESSENTIAL KAOS //
// Apache License, Version 2.0 <https://www.apache.org/licenses/LICENSE-2.0> //
// //
// ////////////////////////////////////////////////////////////////////////////////// //
import (
"net/http"
"strings"
"testing"
"time"
. "github.com/essentialkaos/check"
)
// ////////////////////////////////////////////////////////////////////////////////// //
const TEST_PORT = "56123"
// ////////////////////////////////////////////////////////////////////////////////// //
func Test(t *testing.T) { TestingT(t) }
type TelemostSuite struct{}
// ////////////////////////////////////////////////////////////////////////////////// //
var _ = Suite(&TelemostSuite{})
// ////////////////////////////////////////////////////////////////////////////////// //
func (s *TelemostSuite) SetUpSuite(c *C) {
API = "http://127.0.0.1:" + TEST_PORT
mux := http.NewServeMux()
server := &http.Server{Addr: ":" + TEST_PORT, Handler: mux}
mux.HandleFunc("POST /", handlerCreateConference)
mux.HandleFunc("GET /12345678901234", handlerGetConference)
mux.HandleFunc("PATCH /12345678901234", handlerUpdateConference)
mux.HandleFunc("DELETE /12345678901234", handlerDeleteConference)
mux.HandleFunc("GET /12345678901234/cohosts", handlerGetCohosts)
mux.HandleFunc("PATCH /12345678901234/cohosts", handlerAddCohosts)
mux.HandleFunc("PUT /12345678901234/cohosts", handlerUpdateCohosts)
mux.HandleFunc("DELETE /12345678901234/cohosts", handlerDeleteCohosts)
go server.ListenAndServe()
time.Sleep(time.Second)
}
// ////////////////////////////////////////////////////////////////////////////////// //
func (s *TelemostSuite) TestConferenceValidation(c *C) {
conf := &Conference{WaitingRoomLevel: "TEST"}
c.Assert(validateConference(conf).Error(), Equals, `Unknown waiting room level "TEST"`)
conf = &Conference{LiveStream: &LiveStream{AccessLevel: "TEST"}}
c.Assert(validateConference(conf).Error(), Equals, `Unknown live stream access level "TEST"`)
conf = &Conference{LiveStream: &LiveStream{AccessLevel: "PUBLIC", Title: strings.Repeat("TEST1234", 180)}}
c.Assert(validateConference(conf).Error(), Equals, `Live stream title exceeds maximum length (1440 > 1024)`)
conf = &Conference{LiveStream: &LiveStream{AccessLevel: "PUBLIC", Description: strings.Repeat("TEST1234", 300)}}
c.Assert(validateConference(conf).Error(), Equals, `Live stream description exceeds maximum length (2400 > 2048)`)
var hosts Hosts
for range 50 {
hosts = append(hosts, &Host{`[email protected]`})
}
conf = &Conference{CoHosts: hosts}
c.Assert(validateConference(conf).Error(), Equals, `Too many cohosts (50 > 30)`)
}
func (s *TelemostSuite) TestGet(c *C) {
api, _ := NewClient("Test1234")
info, err := api.Get("12345678901234")
api.SetUserAgent("Test", "1.2.3")
c.Assert(err, IsNil)
c.Assert(info, NotNil)
c.Assert(info.ID, Equals, "12345678901234")
c.Assert(info.WaitingRoomLevel, Equals, "ORGANIZATION")
c.Assert(info.LiveStream.WatchURL, Equals, "https://telemost.yandex.ru/live/123456789abcdef0123456789abcdef0")
c.Assert(info.LiveStream.AccessLevel, Equals, "PUBLIC")
c.Assert(info.LiveStream.Title, Equals, "Example conference created via API")
c.Assert(info.LiveStream.Description, Equals, "Some description of example conference created via API")
c.Assert(info.JoinURL, Equals, "https://telemost.yandex.ru/j/12345678901234")
c.Assert(info.SIPURIMeeting, Equals, "[email protected]")
c.Assert(info.SIPURITelemost, Equals, "[email protected]")
c.Assert(info.SIPID, Equals, "12345678901234567890")
}
func (s *TelemostSuite) TestCreate(c *C) {
api, _ := NewClient("Test1234")
info, err := api.Create(&Conference{})
c.Assert(err, IsNil)
c.Assert(info, NotNil)
c.Assert(info.ID, Equals, "12345678901234")
c.Assert(info.JoinURL, Equals, "https://telemost.yandex.ru/j/12345678901234")
c.Assert(info.LiveStream.WatchURL, Equals, "https://telemost.yandex.ru/live/123456789abcdef0123456789abcdef0")
_, err = api.Create(&Conference{WaitingRoomLevel: "TEST"})
c.Assert(err, NotNil)
c.Assert(err.Error(), Equals, `Unknown waiting room level "TEST"`)
api, _ = NewClient("http-error")
_, err = api.Create(&Conference{})
c.Assert(err, NotNil)
c.Assert(err.Error(), Equals, `API returned error: Conference not found. (ConferenceNotFound)`)
}
func (s *TelemostSuite) TestUpdate(c *C) {
api, _ := NewClient("Test1234")
info, err := api.Update("12345678901234", &Conference{LiveStream: &LiveStream{
AccessLevel: "PUBLIC",
Title: "Example conference created via API",
Description: "Some description of example conference created via API",
}})
c.Assert(err, IsNil)
c.Assert(info, NotNil)
c.Assert(info.LiveStream.AccessLevel, Equals, "PUBLIC")
c.Assert(info.LiveStream.Title, Equals, "Example conference created via API")
c.Assert(info.LiveStream.Description, Equals, "Some description of example conference created via API")
_, err = api.Update("12345678901234", &Conference{WaitingRoomLevel: "TEST"})
c.Assert(err, NotNil)
c.Assert(err.Error(), Equals, `Unknown waiting room level "TEST"`)
api, _ = NewClient("http-error")
_, err = api.Update("12345678901234", &Conference{})
c.Assert(err, NotNil)
c.Assert(err.Error(), Equals, `API returned error: Conference not found. (ConferenceNotFound)`)
}
func (s *TelemostSuite) TestDelete(c *C) {
api, _ := NewClient("Test1234")
err := api.Delete("12345678901234")
c.Assert(err, IsNil)
}
func (s *TelemostSuite) TestGetCohosts(c *C) {
api, _ := NewClient("Test1234")
cohosts, err := api.GetCohosts("12345678901234")
c.Assert(err, IsNil)
c.Assert(cohosts, NotNil)
c.Assert(cohosts, HasLen, 2)
c.Assert(cohosts.Flatten(), DeepEquals, []string{"[email protected]", "[email protected]"})
api, _ = NewClient("http-error")
_, err = api.GetCohosts("12345678901234")
c.Assert(err, NotNil)
c.Assert(err.Error(), Equals, `API returned error: Conference not found. (ConferenceNotFound)`)
}
func (s *TelemostSuite) TestAddCohosts(c *C) {
api, _ := NewClient("Test1234")
err := api.AddCohosts("12345678901234", []string{"[email protected]"})
c.Assert(err, IsNil)
}
func (s *TelemostSuite) TestUpdateCohosts(c *C) {
api, _ := NewClient("Test1234")
err := api.UpdateCohosts("12345678901234", []string{"[email protected]"})
c.Assert(err, IsNil)
}
func (s *TelemostSuite) TestDeleteCohosts(c *C) {
api, _ := NewClient("Test1234")
err := api.DeleteCohosts("12345678901234", []string{"[email protected]"})
c.Assert(err, IsNil)
}
func (s *TelemostSuite) TestErrors(c *C) {
var api *Client
api.SetUserAgent("", "")
_, err := api.Create(&Conference{})
c.Assert(err, Equals, ErrNilClient)
_, err = api.Get("1234")
c.Assert(err, Equals, ErrNilClient)
_, err = api.Update("1234", &Conference{})
c.Assert(err, Equals, ErrNilClient)
err = api.Delete("1234")
c.Assert(err, Equals, ErrNilClient)
_, err = api.GetCohosts("1234")
c.Assert(err, Equals, ErrNilClient)
err = api.AddCohosts("1234", []string{"[email protected]"})
c.Assert(err, Equals, ErrNilClient)
err = api.UpdateCohosts("1234", []string{"[email protected]"})
c.Assert(err, Equals, ErrNilClient)
err = api.DeleteCohosts("1234", []string{"[email protected]"})
c.Assert(err, Equals, ErrNilClient)
api, err = NewClient("")
c.Assert(err, Equals, ErrEmptyToken)
api, _ = NewClient("Test1234")
_, err = api.Create(nil)
c.Assert(err, Equals, ErrNilConference)
_, err = api.Get("")
c.Assert(err, Equals, ErrEmptyID)
_, err = api.Update("", &Conference{})
c.Assert(err, Equals, ErrEmptyID)
_, err = api.Update("1234", nil)
c.Assert(err, Equals, ErrNilConference)
err = api.Delete("")
c.Assert(err, Equals, ErrEmptyID)
_, err = api.GetCohosts("")
c.Assert(err, Equals, ErrEmptyID)
err = api.AddCohosts("", []string{"A"})
c.Assert(err, Equals, ErrEmptyID)
err = api.AddCohosts("1234", nil)
c.Assert(err, Equals, ErrEmptyCohosts)
err = api.UpdateCohosts("", []string{"A"})
c.Assert(err, Equals, ErrEmptyID)
err = api.UpdateCohosts("1234", nil)
c.Assert(err, Equals, ErrEmptyCohosts)
err = api.DeleteCohosts("", []string{"A"})
c.Assert(err, Equals, ErrEmptyID)
err = api.DeleteCohosts("1234", nil)
c.Assert(err, Equals, ErrEmptyCohosts)
api, _ = NewClient("http-error")
_, err = api.Get("12345678901234")
c.Assert(err, NotNil)
c.Assert(err.Error(), Equals, "API returned error: Conference not found. (ConferenceNotFound)")
api, _ = NewClient("msg-error")
_, err = api.Get("12345678901234")
c.Assert(err, NotNil)
c.Assert(err.Error(), Equals, "API returned non-ok status code 404")
api, _ = NewClient("data-error")
_, err = api.Get("12345678901234")
c.Assert(err, NotNil)
c.Assert(err.Error(), Equals, "Can't decode API response: invalid character 'X' looking for beginning of value")
API = "http://127.0.0.1:9999"
api, _ = NewClient("Test1234")
_, err = api.Get("12345678901234")
c.Assert(err, NotNil)
API = "http://127.0.0.1:" + TEST_PORT
}
// ////////////////////////////////////////////////////////////////////////////////// //
func handlerCreateConference(rw http.ResponseWriter, r *http.Request) {
if writeErrorResponse(rw, r) {
return
}
rw.WriteHeader(200)
rw.Write([]byte(`{
"id": "12345678901234",
"join_url": "https://telemost.yandex.ru/j/12345678901234",
"live_stream": {
"watch_url": "https://telemost.yandex.ru/live/123456789abcdef0123456789abcdef0"
}
}`))
}
func handlerGetConference(rw http.ResponseWriter, r *http.Request) {
if writeErrorResponse(rw, r) {
return
}
rw.WriteHeader(200)
rw.Write([]byte(`{
"id": "12345678901234",
"join_url": "https://telemost.yandex.ru/j/12345678901234",
"access_level": "ORGANIZATION",
"waiting_room_level": "ORGANIZATION",
"live_stream": {
"watch_url": "https://telemost.yandex.ru/live/123456789abcdef0123456789abcdef0",
"access_level": "PUBLIC",
"title": "Example conference created via API",
"description": "Some description of example conference created via API"
},
"sip_uri_meeting": "[email protected]",
"sip_uri_telemost": "[email protected]",
"sip_id": "12345678901234567890"
}`))
}
func handlerUpdateConference(rw http.ResponseWriter, r *http.Request) {
if writeErrorResponse(rw, r) {
return
}
rw.WriteHeader(200)
rw.Write([]byte(`{
"live_stream": {
"access_level": "PUBLIC",
"title": "Example conference created via API",
"description": "Some description of example conference created via API"
}
}`))
}
func handlerDeleteConference(rw http.ResponseWriter, r *http.Request) {
if writeErrorResponse(rw, r) {
return
}
rw.WriteHeader(204)
}
func handlerGetCohosts(rw http.ResponseWriter, r *http.Request) {
if writeErrorResponse(rw, r) {
return
}
rw.WriteHeader(200)
rw.Write([]byte(`{
"cohosts": [
{
"email": "[email protected]"
},
{
"email": "[email protected]"
}
]
}`))
}
func handlerAddCohosts(rw http.ResponseWriter, r *http.Request) {
if writeErrorResponse(rw, r) {
return
}
rw.WriteHeader(204)
}
func handlerUpdateCohosts(rw http.ResponseWriter, r *http.Request) {
if writeErrorResponse(rw, r) {
return
}
rw.WriteHeader(204)
}
func handlerDeleteCohosts(rw http.ResponseWriter, r *http.Request) {
if writeErrorResponse(rw, r) {
return
}
rw.WriteHeader(204)
}
func writeErrorResponse(rw http.ResponseWriter, r *http.Request) bool {
if r.Header.Get("Authorization") == "OAuth http-error" {
rw.WriteHeader(404)
rw.Write([]byte(`{
"message": "Видео встреча не найдена.",
"description": "Conference not found.",
"error": "ConferenceNotFound"
}`))
return true
}
if r.Header.Get("Authorization") == "OAuth msg-error" {
rw.WriteHeader(404)
rw.Write([]byte(`XXX`))
return true
}
if r.Header.Get("Authorization") == "OAuth data-error" {
rw.WriteHeader(200)
rw.Write([]byte(`XXX`))
return true
}
return false
}