tunnel_test.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. package cfapi
  2. import (
  3. "bytes"
  4. "fmt"
  5. "net"
  6. "reflect"
  7. "strings"
  8. "testing"
  9. "time"
  10. "github.com/google/uuid"
  11. "github.com/stretchr/testify/assert"
  12. )
  13. var loc, _ = time.LoadLocation("UTC")
  14. func Test_unmarshalTunnel(t *testing.T) {
  15. type args struct {
  16. body string
  17. }
  18. tests := []struct {
  19. name string
  20. args args
  21. want *Tunnel
  22. wantErr bool
  23. }{
  24. {
  25. name: "empty list",
  26. args: args{body: `{"success": true, "result": {"id":"b34cc7ce-925b-46ee-bc23-4cb5c18d8292","created_at":"2021-07-29T13:46:14.090955Z","deleted_at":"2021-07-29T14:07:27.559047Z","name":"qt-bIWWN7D662ogh61pCPfu5s2XgqFY1OyV","account_id":6946212,"account_tag":"5ab4e9dfbd435d24068829fda0077963","conns_active_at":null,"conns_inactive_at":"2021-07-29T13:47:22.548482Z","tun_type":"cfd_tunnel","metadata":{"qtid":"a6fJROgkXutNruBGaJjD"}}}`},
  27. want: &Tunnel{
  28. ID: uuid.MustParse("b34cc7ce-925b-46ee-bc23-4cb5c18d8292"),
  29. Name: "qt-bIWWN7D662ogh61pCPfu5s2XgqFY1OyV",
  30. CreatedAt: time.Date(2021, 07, 29, 13, 46, 14, 90955000, loc),
  31. DeletedAt: time.Date(2021, 07, 29, 14, 7, 27, 559047000, loc),
  32. Connections: nil,
  33. },
  34. },
  35. }
  36. for _, tt := range tests {
  37. t.Run(tt.name, func(t *testing.T) {
  38. got, err := unmarshalTunnel(strings.NewReader(tt.args.body))
  39. if (err != nil) != tt.wantErr {
  40. t.Errorf("unmarshalTunnel() error = %v, wantErr %v", err, tt.wantErr)
  41. return
  42. }
  43. if !reflect.DeepEqual(got, tt.want) {
  44. t.Errorf("unmarshalTunnel() = %v, want %v", got, tt.want)
  45. }
  46. })
  47. }
  48. }
  49. func TestUnmarshalTunnelOk(t *testing.T) {
  50. jsonBody := `{"success": true, "result": {"id": "00000000-0000-0000-0000-000000000000","name":"test","created_at":"0001-01-01T00:00:00Z","connections":[]}}`
  51. expected := Tunnel{
  52. ID: uuid.Nil,
  53. Name: "test",
  54. CreatedAt: time.Time{},
  55. Connections: []Connection{},
  56. }
  57. actual, err := unmarshalTunnel(bytes.NewReader([]byte(jsonBody)))
  58. assert.NoError(t, err)
  59. assert.Equal(t, &expected, actual)
  60. }
  61. func TestUnmarshalTunnelErr(t *testing.T) {
  62. tests := []string{
  63. `abc`,
  64. `{"success": true, "result": abc}`,
  65. `{"success": false, "result": {"id": "00000000-0000-0000-0000-000000000000","name":"test","created_at":"0001-01-01T00:00:00Z","connections":[]}}}`,
  66. `{"errors": [{"code": 1003, "message":"An A, AAAA or CNAME record already exists with that host"}], "result": {"id": "00000000-0000-0000-0000-000000000000","name":"test","created_at":"0001-01-01T00:00:00Z","connections":[]}}}`,
  67. }
  68. for i, test := range tests {
  69. _, err := unmarshalTunnel(bytes.NewReader([]byte(test)))
  70. assert.Error(t, err, fmt.Sprintf("Test #%v failed", i))
  71. }
  72. }
  73. func TestUnmarshalConnections(t *testing.T) {
  74. jsonBody := `{"success":true,"messages":[],"errors":[],"result":[{"id":"d4041254-91e3-4deb-bd94-b46e11680b1e","features":["ha-origin"],"version":"2021.2.5","arch":"darwin_amd64","conns":[{"colo_name":"LIS","id":"ac2286e5-c708-4588-a6a0-ba6b51940019","is_pending_reconnect":false,"origin_ip":"148.38.28.2","opened_at":"0001-01-01T00:00:00Z"}],"run_at":"0001-01-01T00:00:00Z"}]}`
  75. expected := ActiveClient{
  76. ID: uuid.MustParse("d4041254-91e3-4deb-bd94-b46e11680b1e"),
  77. Features: []string{"ha-origin"},
  78. Version: "2021.2.5",
  79. Arch: "darwin_amd64",
  80. RunAt: time.Time{},
  81. Connections: []Connection{{
  82. ID: uuid.MustParse("ac2286e5-c708-4588-a6a0-ba6b51940019"),
  83. ColoName: "LIS",
  84. IsPendingReconnect: false,
  85. OriginIP: net.ParseIP("148.38.28.2"),
  86. OpenedAt: time.Time{},
  87. }},
  88. }
  89. actual, err := parseConnectionsDetails(bytes.NewReader([]byte(jsonBody)))
  90. assert.NoError(t, err)
  91. assert.Equal(t, []*ActiveClient{&expected}, actual)
  92. }