github.com/bitfinexcom/bitfinex-api-go@v0.0.0-20210608095005-9e0b26f200fb/v2/rest/pulse_test.go (about) 1 package rest 2 3 import ( 4 "encoding/json" 5 "net/http" 6 "net/http/httptest" 7 "testing" 8 "time" 9 10 "github.com/bitfinexcom/bitfinex-api-go/pkg/models/common" 11 "github.com/bitfinexcom/bitfinex-api-go/pkg/models/pulse" 12 "github.com/bitfinexcom/bitfinex-api-go/pkg/models/pulseprofile" 13 "github.com/stretchr/testify/assert" 14 "github.com/stretchr/testify/require" 15 ) 16 17 func TestPublicPulseProfile(t *testing.T) { 18 t.Run("missing arguments", func(t *testing.T) { 19 c := NewClient() 20 pp, err := c.Pulse.PublicPulseProfile("") 21 require.NotNil(t, err) 22 require.Nil(t, pp) 23 }) 24 25 t.Run("response data slice too short", func(t *testing.T) { 26 handler := func(w http.ResponseWriter, r *http.Request) { 27 respMock := []interface{}{"abc123"} 28 payload, _ := json.Marshal(respMock) 29 _, err := w.Write(payload) 30 require.Nil(t, err) 31 } 32 33 server := httptest.NewServer(http.HandlerFunc(handler)) 34 defer server.Close() 35 36 c := NewClientWithURL(server.URL) 37 pp, err := c.Pulse.PublicPulseProfile("Bitfinex") 38 require.NotNil(t, err) 39 require.Nil(t, pp) 40 }) 41 42 t.Run("valid response data", func(t *testing.T) { 43 handler := func(w http.ResponseWriter, r *http.Request) { 44 assert.Equal(t, "/pulse/profile/Bitfinex", r.RequestURI) 45 respMock := []interface{}{ 46 "bf324e24-5a09-4317-b418-6c37281ab855", 47 1591614631576, 48 nil, 49 "Bitfinex", 50 nil, 51 "image-33533a4d-a796-4afe-9b8b-690bc7075e05-1587476823358-size.png", 52 "Bitfinex is the world’s leading digital asset trading platform.", 53 nil, 54 nil, 55 "bitfinex", 56 nil, 57 40, 58 5, 59 nil, 60 nil, 61 nil, 62 0, 63 } 64 payload, _ := json.Marshal(respMock) 65 _, err := w.Write(payload) 66 require.Nil(t, err) 67 } 68 69 server := httptest.NewServer(http.HandlerFunc(handler)) 70 defer server.Close() 71 72 c := NewClientWithURL(server.URL) 73 pp, err := c.Pulse.PublicPulseProfile("Bitfinex") 74 require.Nil(t, err) 75 76 expected := &pulseprofile.PulseProfile{ 77 ID: "bf324e24-5a09-4317-b418-6c37281ab855", 78 MTS: 1591614631576, 79 Nickname: "Bitfinex", 80 Picture: "image-33533a4d-a796-4afe-9b8b-690bc7075e05-1587476823358-size.png", 81 Text: "Bitfinex is the world’s leading digital asset trading platform.", 82 TwitterHandle: "bitfinex", 83 Followers: 40, 84 Following: 5, 85 TippingStatus: 0, 86 } 87 assert.Equal(t, expected, pp) 88 }) 89 } 90 91 func TestPublicPulseHistory(t *testing.T) { 92 t.Run("response data slice too short", func(t *testing.T) { 93 handler := func(w http.ResponseWriter, r *http.Request) { 94 respMock := []interface{}{ 95 []interface{}{"id"}, 96 } 97 payload, _ := json.Marshal(respMock) 98 _, err := w.Write(payload) 99 require.Nil(t, err) 100 } 101 102 server := httptest.NewServer(http.HandlerFunc(handler)) 103 defer server.Close() 104 105 c := NewClientWithURL(server.URL) 106 now := time.Now() 107 millis := now.UnixNano() / 1000000 108 end := common.Mts(millis) 109 pp, err := c.Pulse.PublicPulseHistory(1, end) 110 require.NotNil(t, err) 111 require.Nil(t, pp) 112 }) 113 114 t.Run("valid response data no profile", func(t *testing.T) { 115 handler := func(w http.ResponseWriter, r *http.Request) { 116 assert.Equal(t, "/pulse/hist?end=1591614631576&limit=1", r.RequestURI) 117 limit := r.URL.Query().Get("limit") 118 end := r.URL.Query().Get("end") 119 assert.Equal(t, "1", limit) 120 assert.Equal(t, "1591614631576", end) 121 122 respMock := []interface{}{ 123 []interface{}{ 124 "d139512a-6558-431a-a6aa-69646fc97d55", 125 1593608548140, 126 nil, 127 "bf324e24-5a09-4317-b418-6c37281ab855", 128 nil, 129 "Take an active role in the discussion with the Comment feature!", 130 "Bitfinex Pulse, the social trading you were waiting for, keeps improving to give you a complete social trading experience.", 131 nil, 132 nil, 133 1, 134 1, 135 nil, 136 []interface{}{"BitfinexPulse", "PulseFeatures", "PulseUpdates", "L_EN", "N_Bitfinex"}, 137 []interface{}{"image-a55eda4c-4f5a-4609-abf0-7bdfca1a8911-1593608547535-size.jpg"}, 138 []interface{}{}, 139 1, 140 nil, 141 nil, 142 nil, 143 1, 144 nil, 145 nil, 146 }, 147 } 148 payload, _ := json.Marshal(respMock) 149 _, err := w.Write(payload) 150 require.Nil(t, err) 151 } 152 153 server := httptest.NewServer(http.HandlerFunc(handler)) 154 defer server.Close() 155 156 c := NewClientWithURL(server.URL) 157 end := common.Mts(1591614631576) 158 pph, err := c.Pulse.PublicPulseHistory(1, end) 159 require.Nil(t, err) 160 161 expected := &pulse.Pulse{ 162 ID: "d139512a-6558-431a-a6aa-69646fc97d55", 163 MTS: 1593608548140, 164 UserID: "bf324e24-5a09-4317-b418-6c37281ab855", 165 Title: "Take an active role in the discussion with the Comment feature!", 166 Content: "Bitfinex Pulse, the social trading you were waiting for, keeps improving to give you a complete social trading experience.", 167 IsPin: 1, 168 IsPublic: 1, 169 CommentsDisabled: 0, 170 Tags: []string{"BitfinexPulse", "PulseFeatures", "PulseUpdates", "L_EN", "N_Bitfinex"}, 171 Attachments: []string{"image-a55eda4c-4f5a-4609-abf0-7bdfca1a8911-1593608547535-size.jpg"}, 172 Likes: 1, 173 Comments: 1, 174 } 175 176 assert.Equal(t, expected, pph[0]) 177 }) 178 } 179 180 func TestAddPulse(t *testing.T) { 181 t.Run("invalid payload", func(t *testing.T) { 182 p := &pulse.Pulse{Title: "foo"} 183 c := NewClient() 184 pm, err := c.Pulse.AddPulse(p) 185 require.NotNil(t, err) 186 require.Nil(t, pm) 187 }) 188 189 t.Run("response data slice too short", func(t *testing.T) { 190 handler := func(w http.ResponseWriter, r *http.Request) { 191 assert.Equal(t, "/auth/w/pulse/add", r.RequestURI) 192 respMock := []interface{}{"id"} 193 payload, _ := json.Marshal(respMock) 194 _, err := w.Write(payload) 195 require.Nil(t, err) 196 } 197 198 server := httptest.NewServer(http.HandlerFunc(handler)) 199 defer server.Close() 200 201 c := NewClientWithURL(server.URL) 202 pm, err := c.Pulse.AddPulse(&pulse.Pulse{Title: "foo bar baz qux 123"}) 203 require.NotNil(t, err) 204 require.Nil(t, pm) 205 }) 206 207 t.Run("valid payload", func(t *testing.T) { 208 handler := func(w http.ResponseWriter, r *http.Request) { 209 respMock := []interface{}{ 210 "d139512a-6558-431a-a6aa-69646fc97d55", 211 1593608548140, 212 nil, 213 "bf324e24-5a09-4317-b418-6c37281ab855", 214 nil, 215 "Take an active role in the discussion with the Comment feature!", 216 "Bitfinex Pulse, the social trading you were waiting for, keeps improving to give you a complete social trading experience.", 217 nil, 218 nil, 219 1, 220 1, 221 nil, 222 []interface{}{"BitfinexPulse", "PulseFeatures", "PulseUpdates", "L_EN", "N_Bitfinex"}, 223 []interface{}{"image-a55eda4c-4f5a-4609-abf0-7bdfca1a8911-1593608547535-size.jpg"}, 224 []interface{}{}, 225 1, 226 nil, 227 nil, 228 nil, 229 1, 230 nil, 231 nil, 232 } 233 payload, _ := json.Marshal(respMock) 234 _, err := w.Write(payload) 235 require.Nil(t, err) 236 } 237 238 server := httptest.NewServer(http.HandlerFunc(handler)) 239 defer server.Close() 240 241 c := NewClientWithURL(server.URL) 242 pm, err := c.Pulse.AddPulse(&pulse.Pulse{Title: "foo bar baz qux 123"}) 243 require.Nil(t, err) 244 245 expected := &pulse.Pulse{ 246 ID: "d139512a-6558-431a-a6aa-69646fc97d55", 247 MTS: 1593608548140, 248 UserID: "bf324e24-5a09-4317-b418-6c37281ab855", 249 Title: "Take an active role in the discussion with the Comment feature!", 250 Content: "Bitfinex Pulse, the social trading you were waiting for, keeps improving to give you a complete social trading experience.", 251 IsPin: 1, 252 IsPublic: 1, 253 CommentsDisabled: 0, 254 Tags: []string{"BitfinexPulse", "PulseFeatures", "PulseUpdates", "L_EN", "N_Bitfinex"}, 255 Attachments: []string{"image-a55eda4c-4f5a-4609-abf0-7bdfca1a8911-1593608547535-size.jpg"}, 256 Likes: 1, 257 Comments: 1, 258 } 259 260 assert.Equal(t, expected, pm) 261 }) 262 } 263 264 func TestPulseHistory(t *testing.T) { 265 t.Run("response data slice too short", func(t *testing.T) { 266 handler := func(w http.ResponseWriter, r *http.Request) { 267 assert.Equal(t, "/auth/r/pulse/hist", r.RequestURI) 268 269 respMock := []interface{}{ 270 []interface{}{"id"}, 271 } 272 payload, _ := json.Marshal(respMock) 273 _, err := w.Write(payload) 274 require.Nil(t, err) 275 } 276 277 server := httptest.NewServer(http.HandlerFunc(handler)) 278 defer server.Close() 279 280 c := NewClientWithURL(server.URL) 281 pp, err := c.Pulse.PulseHistory() 282 require.NotNil(t, err) 283 require.Nil(t, pp) 284 }) 285 286 t.Run("private pulse history", func(t *testing.T) { 287 handler := func(w http.ResponseWriter, r *http.Request) { 288 respMock := []interface{}{ 289 []interface{}{ 290 "d139512a-6558-431a-a6aa-69646fc97d55", 291 1593608548140, 292 nil, 293 "bf324e24-5a09-4317-b418-6c37281ab855", 294 nil, 295 "Take an active role in the discussion with the Comment feature!", 296 "Bitfinex Pulse, the social trading you were waiting for, keeps improving to give you a complete social trading experience.", 297 nil, 298 nil, 299 1, 300 1, 301 nil, 302 []interface{}{"BitfinexPulse", "PulseFeatures", "PulseUpdates", "L_EN", "N_Bitfinex"}, 303 []interface{}{"image-a55eda4c-4f5a-4609-abf0-7bdfca1a8911-1593608547535-size.jpg"}, 304 []interface{}{}, 305 1, 306 nil, 307 nil, 308 nil, 309 1, 310 nil, 311 nil, 312 }, 313 } 314 payload, _ := json.Marshal(respMock) 315 _, err := w.Write(payload) 316 require.Nil(t, err) 317 } 318 319 server := httptest.NewServer(http.HandlerFunc(handler)) 320 defer server.Close() 321 322 c := NewClientWithURL(server.URL) 323 pm, err := c.Pulse.PulseHistory() 324 require.Nil(t, err) 325 326 expected := &pulse.Pulse{ 327 ID: "d139512a-6558-431a-a6aa-69646fc97d55", 328 MTS: 1593608548140, 329 UserID: "bf324e24-5a09-4317-b418-6c37281ab855", 330 Title: "Take an active role in the discussion with the Comment feature!", 331 Content: "Bitfinex Pulse, the social trading you were waiting for, keeps improving to give you a complete social trading experience.", 332 IsPin: 1, 333 IsPublic: 1, 334 CommentsDisabled: 0, 335 Tags: []string{"BitfinexPulse", "PulseFeatures", "PulseUpdates", "L_EN", "N_Bitfinex"}, 336 Attachments: []string{"image-a55eda4c-4f5a-4609-abf0-7bdfca1a8911-1593608547535-size.jpg"}, 337 Likes: 1, 338 Comments: 1, 339 } 340 341 assert.Equal(t, expected, pm[0]) 342 }) 343 344 t.Run("public pulse history", func(t *testing.T) { 345 handler := func(w http.ResponseWriter, r *http.Request) { 346 respMock := []interface{}{ 347 []interface{}{ 348 "d139512a-6558-431a-a6aa-69646fc97d55", 349 1593608548140, 350 nil, 351 "bf324e24-5a09-4317-b418-6c37281ab855", 352 nil, 353 "Take an active role in the discussion with the Comment feature!", 354 "Bitfinex Pulse, the social trading you were waiting for, keeps improving to give you a complete social trading experience.", 355 nil, 356 nil, 357 1, 358 1, 359 nil, 360 []interface{}{"BitfinexPulse", "PulseFeatures", "PulseUpdates", "L_EN", "N_Bitfinex"}, 361 []interface{}{"image-a55eda4c-4f5a-4609-abf0-7bdfca1a8911-1593608547535-size.jpg"}, 362 []interface{}{}, 363 1, 364 nil, 365 nil, 366 []interface{}{ 367 []interface{}{ 368 "bf324e24-5a09-4317-b418-6c37281ab855", 369 1591614631576, 370 nil, 371 "Bitfinex", 372 nil, 373 "image-33533a4d-a796-4afe-9b8b-690bc7075e05-1587476823358-size.png", 374 "Bitfinex is the world’s leading digital asset trading platform.", 375 nil, 376 nil, 377 "bitfinex", 378 nil, 379 40, 380 5, 381 nil, 382 nil, 383 nil, 384 0, 385 }, 386 }, 387 1, 388 nil, 389 nil, 390 }, 391 } 392 payload, _ := json.Marshal(respMock) 393 _, err := w.Write(payload) 394 require.Nil(t, err) 395 } 396 397 server := httptest.NewServer(http.HandlerFunc(handler)) 398 defer server.Close() 399 400 c := NewClientWithURL(server.URL) 401 pm, err := c.Pulse.PulseHistory() 402 require.Nil(t, err) 403 404 expected := &pulse.Pulse{ 405 ID: "d139512a-6558-431a-a6aa-69646fc97d55", 406 MTS: 1593608548140, 407 UserID: "bf324e24-5a09-4317-b418-6c37281ab855", 408 Title: "Take an active role in the discussion with the Comment feature!", 409 Content: "Bitfinex Pulse, the social trading you were waiting for, keeps improving to give you a complete social trading experience.", 410 IsPin: 1, 411 IsPublic: 1, 412 CommentsDisabled: 0, 413 Tags: []string{"BitfinexPulse", "PulseFeatures", "PulseUpdates", "L_EN", "N_Bitfinex"}, 414 Attachments: []string{"image-a55eda4c-4f5a-4609-abf0-7bdfca1a8911-1593608547535-size.jpg"}, 415 Likes: 1, 416 Comments: 1, 417 PulseProfile: &pulseprofile.PulseProfile{ 418 ID: "bf324e24-5a09-4317-b418-6c37281ab855", 419 MTS: 1591614631576, 420 Nickname: "Bitfinex", 421 Picture: "image-33533a4d-a796-4afe-9b8b-690bc7075e05-1587476823358-size.png", 422 Text: "Bitfinex is the world’s leading digital asset trading platform.", 423 TwitterHandle: "bitfinex", 424 Followers: 40, 425 Following: 5, 426 TippingStatus: 0, 427 }, 428 } 429 430 assert.Equal(t, expected, pm[0]) 431 }) 432 } 433 434 func TestDeletePulse(t *testing.T) { 435 t.Run("response", func(t *testing.T) { 436 handler := func(w http.ResponseWriter, r *http.Request) { 437 assert.Equal(t, "/auth/w/pulse/del", r.RequestURI) 438 439 respMock := []interface{}{1} 440 payload, _ := json.Marshal(respMock) 441 _, err := w.Write(payload) 442 require.Nil(t, err) 443 } 444 445 server := httptest.NewServer(http.HandlerFunc(handler)) 446 defer server.Close() 447 448 c := NewClientWithURL(server.URL) 449 deleted, err := c.Pulse.DeletePulse("abc123") 450 require.Nil(t, err) 451 assert.Equal(t, 1, deleted) 452 }) 453 } 454 455 func TestAddPulseComment(t *testing.T) { 456 t.Run("invalid payload", func(t *testing.T) { 457 p := &pulse.Pulse{Title: "foo"} 458 c := NewClient() 459 pm, err := c.Pulse.AddComment(p) 460 require.NotNil(t, err) 461 require.Nil(t, pm) 462 }) 463 464 t.Run("response data slice too short", func(t *testing.T) { 465 handler := func(w http.ResponseWriter, r *http.Request) { 466 assert.Equal(t, "/auth/w/pulse/add", r.RequestURI) 467 respMock := []interface{}{"id"} 468 payload, _ := json.Marshal(respMock) 469 _, err := w.Write(payload) 470 require.Nil(t, err) 471 } 472 473 server := httptest.NewServer(http.HandlerFunc(handler)) 474 defer server.Close() 475 476 c := NewClientWithURL(server.URL) 477 pm, err := c.Pulse.AddComment(&pulse.Pulse{Title: "foo bar baz qux 123"}) 478 require.NotNil(t, err) 479 require.Nil(t, pm) 480 }) 481 482 t.Run("valid payload", func(t *testing.T) { 483 handler := func(w http.ResponseWriter, r *http.Request) { 484 respMock := []interface{}{ 485 "d139512a-6558-431a-a6aa-69646fc97d55", 486 1593608548140, 487 nil, 488 "bf324e24-5a09-4317-b418-6c37281ab855", 489 nil, 490 "Take an active role in the discussion with the Comment feature!", 491 "Bitfinex Pulse, the social trading you were waiting for, keeps improving to give you a complete social trading experience.", 492 nil, 493 nil, 494 1, 495 1, 496 nil, 497 []interface{}{"BitfinexPulse", "PulseFeatures", "PulseUpdates", "L_EN", "N_Bitfinex"}, 498 []interface{}{"image-a55eda4c-4f5a-4609-abf0-7bdfca1a8911-1593608547535-size.jpg"}, 499 []interface{}{}, 500 1, 501 nil, 502 nil, 503 nil, 504 1, 505 nil, 506 nil, 507 } 508 payload, _ := json.Marshal(respMock) 509 _, err := w.Write(payload) 510 require.Nil(t, err) 511 } 512 513 server := httptest.NewServer(http.HandlerFunc(handler)) 514 defer server.Close() 515 516 c := NewClientWithURL(server.URL) 517 payload := &pulse.Pulse{Title: "foo bar baz qux 123", Parent: "foo"} 518 pm, err := c.Pulse.AddComment(payload) 519 require.Nil(t, err) 520 521 expected := &pulse.Pulse{ 522 ID: "d139512a-6558-431a-a6aa-69646fc97d55", 523 MTS: 1593608548140, 524 UserID: "bf324e24-5a09-4317-b418-6c37281ab855", 525 Title: "Take an active role in the discussion with the Comment feature!", 526 Content: "Bitfinex Pulse, the social trading you were waiting for, keeps improving to give you a complete social trading experience.", 527 IsPin: 1, 528 IsPublic: 1, 529 CommentsDisabled: 0, 530 Tags: []string{"BitfinexPulse", "PulseFeatures", "PulseUpdates", "L_EN", "N_Bitfinex"}, 531 Attachments: []string{"image-a55eda4c-4f5a-4609-abf0-7bdfca1a8911-1593608547535-size.jpg"}, 532 Likes: 1, 533 Comments: 1, 534 } 535 536 assert.Equal(t, expected, pm) 537 }) 538 }