|
| 1 | +package client |
| 2 | + |
| 3 | +import ( |
| 4 | + "net/http" |
| 5 | + "os" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/stretchr/testify/assert" |
| 9 | + "github.com/stretchr/testify/require" |
| 10 | + "gopkg.in/dnaeon/go-vcr.v2/cassette" |
| 11 | + "gopkg.in/dnaeon/go-vcr.v2/recorder" |
| 12 | +) |
| 13 | + |
| 14 | +// TestListInstancesVCR tests listing all instances |
| 15 | +func TestListInstancesVCR(t *testing.T) { |
| 16 | + r, err := recorder.New("fixtures/list_instances") |
| 17 | + if err != nil { |
| 18 | + t.Fatal(err) |
| 19 | + } |
| 20 | + defer r.Stop() |
| 21 | + |
| 22 | + // Add sanitization filters |
| 23 | + r.AddFilter(func(i *cassette.Interaction) error { |
| 24 | + delete(i.Request.Headers, "Authorization") |
| 25 | + i.Response.Body = sanitizeResponseBody(i.Response.Body) |
| 26 | + delete(i.Response.Headers, "Set-Cookie") |
| 27 | + return nil |
| 28 | + }) |
| 29 | + |
| 30 | + apiKey := os.Getenv("CLOUDAMQP_APIKEY") |
| 31 | + if apiKey == "" { |
| 32 | + apiKey = "vcr-replay-mode" |
| 33 | + } |
| 34 | + |
| 35 | + httpClient := &http.Client{Transport: r} |
| 36 | + client := NewWithHTTPClient(apiKey, "https://customer.cloudamqp.com/api", httpClient) |
| 37 | + |
| 38 | + instances, err := client.ListInstances() |
| 39 | + |
| 40 | + require.NoError(t, err) |
| 41 | + require.NotNil(t, instances) |
| 42 | + // Should have at least some instances (or empty list is OK) |
| 43 | + t.Logf("✓ Listed %d instances", len(instances)) |
| 44 | +} |
| 45 | + |
| 46 | +// TestGetInstanceVCR tests getting a specific instance |
| 47 | +func TestGetInstanceVCR(t *testing.T) { |
| 48 | + r, err := recorder.New("fixtures/get_instance") |
| 49 | + if err != nil { |
| 50 | + t.Fatal(err) |
| 51 | + } |
| 52 | + defer r.Stop() |
| 53 | + |
| 54 | + r.AddFilter(func(i *cassette.Interaction) error { |
| 55 | + delete(i.Request.Headers, "Authorization") |
| 56 | + i.Response.Body = sanitizeResponseBody(i.Response.Body) |
| 57 | + delete(i.Response.Headers, "Set-Cookie") |
| 58 | + return nil |
| 59 | + }) |
| 60 | + |
| 61 | + apiKey := os.Getenv("CLOUDAMQP_APIKEY") |
| 62 | + if apiKey == "" { |
| 63 | + apiKey = "vcr-replay-mode" |
| 64 | + } |
| 65 | + |
| 66 | + httpClient := &http.Client{Transport: r} |
| 67 | + client := NewWithHTTPClient(apiKey, "https://customer.cloudamqp.com/api", httpClient) |
| 68 | + |
| 69 | + // Use an existing instance ID (should match cassette) |
| 70 | + instanceID := 359563 |
| 71 | + |
| 72 | + instance, err := client.GetInstance(instanceID) |
| 73 | + |
| 74 | + require.NoError(t, err) |
| 75 | + require.NotNil(t, instance) |
| 76 | + assert.Equal(t, instanceID, instance.ID) |
| 77 | + t.Logf("✓ Got instance %d: %s (plan: %s)", instance.ID, instance.Name, instance.Plan) |
| 78 | +} |
| 79 | + |
| 80 | +// TestListRegionsVCR tests listing all regions |
| 81 | +func TestListRegionsVCR(t *testing.T) { |
| 82 | + r, err := recorder.New("fixtures/list_regions") |
| 83 | + if err != nil { |
| 84 | + t.Fatal(err) |
| 85 | + } |
| 86 | + defer r.Stop() |
| 87 | + |
| 88 | + r.AddFilter(func(i *cassette.Interaction) error { |
| 89 | + delete(i.Request.Headers, "Authorization") |
| 90 | + i.Response.Body = sanitizeResponseBody(i.Response.Body) |
| 91 | + delete(i.Response.Headers, "Set-Cookie") |
| 92 | + return nil |
| 93 | + }) |
| 94 | + |
| 95 | + apiKey := os.Getenv("CLOUDAMQP_APIKEY") |
| 96 | + if apiKey == "" { |
| 97 | + apiKey = "vcr-replay-mode" |
| 98 | + } |
| 99 | + |
| 100 | + httpClient := &http.Client{Transport: r} |
| 101 | + client := NewWithHTTPClient(apiKey, "https://customer.cloudamqp.com/api", httpClient) |
| 102 | + |
| 103 | + regions, err := client.ListRegions("") |
| 104 | + |
| 105 | + require.NoError(t, err) |
| 106 | + require.NotEmpty(t, regions) |
| 107 | + t.Logf("✓ Listed %d regions", len(regions)) |
| 108 | + |
| 109 | + // Verify some expected fields |
| 110 | + if len(regions) > 0 { |
| 111 | + assert.NotEmpty(t, regions[0].Name) |
| 112 | + assert.NotEmpty(t, regions[0].Provider) |
| 113 | + t.Logf(" Example: %s (%s)", regions[0].Name, regions[0].Provider) |
| 114 | + } |
| 115 | +} |
| 116 | + |
| 117 | +// TestListPlansVCR tests listing all plans |
| 118 | +func TestListPlansVCR(t *testing.T) { |
| 119 | + r, err := recorder.New("fixtures/list_plans") |
| 120 | + if err != nil { |
| 121 | + t.Fatal(err) |
| 122 | + } |
| 123 | + defer r.Stop() |
| 124 | + |
| 125 | + r.AddFilter(func(i *cassette.Interaction) error { |
| 126 | + delete(i.Request.Headers, "Authorization") |
| 127 | + i.Response.Body = sanitizeResponseBody(i.Response.Body) |
| 128 | + delete(i.Response.Headers, "Set-Cookie") |
| 129 | + return nil |
| 130 | + }) |
| 131 | + |
| 132 | + apiKey := os.Getenv("CLOUDAMQP_APIKEY") |
| 133 | + if apiKey == "" { |
| 134 | + apiKey = "vcr-replay-mode" |
| 135 | + } |
| 136 | + |
| 137 | + httpClient := &http.Client{Transport: r} |
| 138 | + client := NewWithHTTPClient(apiKey, "https://customer.cloudamqp.com/api", httpClient) |
| 139 | + |
| 140 | + plans, err := client.ListPlans("") |
| 141 | + |
| 142 | + require.NoError(t, err) |
| 143 | + require.NotEmpty(t, plans) |
| 144 | + t.Logf("✓ Listed %d plans", len(plans)) |
| 145 | + |
| 146 | + // Verify some expected fields and find bunny-1 |
| 147 | + bunnyFound := false |
| 148 | + for _, plan := range plans { |
| 149 | + assert.NotEmpty(t, plan.Name) |
| 150 | + if plan.Name == "bunny-1" { |
| 151 | + bunnyFound = true |
| 152 | + t.Logf(" Found bunny-1 plan") |
| 153 | + } |
| 154 | + } |
| 155 | + assert.True(t, bunnyFound, "Should find bunny-1 plan") |
| 156 | +} |
0 commit comments