|
| 1 | +package fakestorage |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "encoding/json" |
| 6 | + "fmt" |
| 7 | + "net/http" |
| 8 | + "testing" |
| 9 | + |
| 10 | + "github.com/fsouza/fake-gcs-server/internal/notification" |
| 11 | + "github.com/stretchr/testify/assert" |
| 12 | + "github.com/stretchr/testify/require" |
| 13 | +) |
| 14 | + |
| 15 | +func newNotificationServer(t *testing.T) *Server { |
| 16 | + t.Helper() |
| 17 | + srv, err := NewServerWithOptions(Options{NoListener: true}) |
| 18 | + require.NoError(t, err) |
| 19 | + srv.CreateBucketWithOpts(CreateBucketOpts{Name: "test-bucket"}) |
| 20 | + t.Cleanup(srv.Stop) |
| 21 | + return srv |
| 22 | +} |
| 23 | + |
| 24 | +func postNotification(t *testing.T, client *http.Client, bucket string, cfg notification.NotificationConfig) (*http.Response, notification.NotificationConfig) { |
| 25 | + t.Helper() |
| 26 | + body, _ := json.Marshal(cfg) |
| 27 | + resp, err := client.Post( |
| 28 | + fmt.Sprintf("https://storage.googleapis.com/storage/v1/b/%s/notificationConfigs", bucket), |
| 29 | + "application/json", |
| 30 | + bytes.NewReader(body), |
| 31 | + ) |
| 32 | + require.NoError(t, err) |
| 33 | + defer resp.Body.Close() |
| 34 | + var created notification.NotificationConfig |
| 35 | + if resp.StatusCode == http.StatusCreated { |
| 36 | + require.NoError(t, json.NewDecoder(resp.Body).Decode(&created)) |
| 37 | + } |
| 38 | + return resp, created |
| 39 | +} |
| 40 | + |
| 41 | +func TestInsertNotification(t *testing.T) { |
| 42 | + srv := newNotificationServer(t) |
| 43 | + cfg := notification.NotificationConfig{Topic: "projects/p/topics/t", PayloadFormat: "JSON_API_V1"} |
| 44 | + |
| 45 | + resp, created := postNotification(t, srv.HTTPClient(), "test-bucket", cfg) |
| 46 | + assert.Equal(t, http.StatusCreated, resp.StatusCode) |
| 47 | + assert.NotEmpty(t, created.ID) |
| 48 | + assert.Equal(t, "storage#notification", created.Kind) |
| 49 | + assert.Equal(t, cfg.Topic, created.Topic) |
| 50 | +} |
| 51 | + |
| 52 | +func TestInsertNotification_BucketNotFound(t *testing.T) { |
| 53 | + srv := newNotificationServer(t) |
| 54 | + cfg := notification.NotificationConfig{Topic: "projects/p/topics/t"} |
| 55 | + resp, _ := postNotification(t, srv.HTTPClient(), "no-such-bucket", cfg) |
| 56 | + assert.Equal(t, http.StatusNotFound, resp.StatusCode) |
| 57 | +} |
| 58 | + |
| 59 | +func TestInsertNotification_MissingTopic(t *testing.T) { |
| 60 | + srv := newNotificationServer(t) |
| 61 | + resp, _ := postNotification(t, srv.HTTPClient(), "test-bucket", notification.NotificationConfig{}) |
| 62 | + assert.Equal(t, http.StatusBadRequest, resp.StatusCode) |
| 63 | +} |
| 64 | + |
| 65 | +func TestGetNotification(t *testing.T) { |
| 66 | + srv := newNotificationServer(t) |
| 67 | + cfg := notification.NotificationConfig{Topic: "projects/p/topics/t"} |
| 68 | + _, inserted := postNotification(t, srv.HTTPClient(), "test-bucket", cfg) |
| 69 | + |
| 70 | + resp, err := srv.HTTPClient().Get(fmt.Sprintf( |
| 71 | + "https://storage.googleapis.com/storage/v1/b/test-bucket/notificationConfigs/%s", |
| 72 | + inserted.ID, |
| 73 | + )) |
| 74 | + require.NoError(t, err) |
| 75 | + defer resp.Body.Close() |
| 76 | + assert.Equal(t, http.StatusOK, resp.StatusCode) |
| 77 | + |
| 78 | + var got notification.NotificationConfig |
| 79 | + require.NoError(t, json.NewDecoder(resp.Body).Decode(&got)) |
| 80 | + assert.Equal(t, inserted.ID, got.ID) |
| 81 | + assert.Equal(t, cfg.Topic, got.Topic) |
| 82 | +} |
| 83 | + |
| 84 | +func TestGetNotification_NotFound(t *testing.T) { |
| 85 | + srv := newNotificationServer(t) |
| 86 | + resp, err := srv.HTTPClient().Get( |
| 87 | + "https://storage.googleapis.com/storage/v1/b/test-bucket/notificationConfigs/9999", |
| 88 | + ) |
| 89 | + require.NoError(t, err) |
| 90 | + defer resp.Body.Close() |
| 91 | + assert.Equal(t, http.StatusNotFound, resp.StatusCode) |
| 92 | +} |
| 93 | + |
| 94 | +func TestListNotifications(t *testing.T) { |
| 95 | + srv := newNotificationServer(t) |
| 96 | + client := srv.HTTPClient() |
| 97 | + listURL := "https://storage.googleapis.com/storage/v1/b/test-bucket/notificationConfigs" |
| 98 | + cfg := notification.NotificationConfig{Topic: "projects/p/topics/t"} |
| 99 | + |
| 100 | + // empty list |
| 101 | + resp, err := client.Get(listURL) |
| 102 | + require.NoError(t, err) |
| 103 | + defer resp.Body.Close() |
| 104 | + assert.Equal(t, http.StatusOK, resp.StatusCode) |
| 105 | + var listResp struct { |
| 106 | + Items []notification.NotificationConfig `json:"items"` |
| 107 | + } |
| 108 | + require.NoError(t, json.NewDecoder(resp.Body).Decode(&listResp)) |
| 109 | + assert.Empty(t, listResp.Items) |
| 110 | + |
| 111 | + // insert two |
| 112 | + for i := 0; i < 2; i++ { |
| 113 | + postNotification(t, client, "test-bucket", cfg) |
| 114 | + } |
| 115 | + |
| 116 | + resp2, err := client.Get(listURL) |
| 117 | + require.NoError(t, err) |
| 118 | + defer resp2.Body.Close() |
| 119 | + require.NoError(t, json.NewDecoder(resp2.Body).Decode(&listResp)) |
| 120 | + assert.Len(t, listResp.Items, 2) |
| 121 | +} |
| 122 | + |
| 123 | +func TestDeleteNotification(t *testing.T) { |
| 124 | + srv := newNotificationServer(t) |
| 125 | + client := srv.HTTPClient() |
| 126 | + cfg := notification.NotificationConfig{Topic: "projects/p/topics/t"} |
| 127 | + _, inserted := postNotification(t, client, "test-bucket", cfg) |
| 128 | + |
| 129 | + deleteURL := fmt.Sprintf( |
| 130 | + "https://storage.googleapis.com/storage/v1/b/test-bucket/notificationConfigs/%s", |
| 131 | + inserted.ID, |
| 132 | + ) |
| 133 | + |
| 134 | + req, _ := http.NewRequest(http.MethodDelete, deleteURL, nil) |
| 135 | + resp, err := client.Do(req) |
| 136 | + require.NoError(t, err) |
| 137 | + resp.Body.Close() |
| 138 | + assert.Equal(t, http.StatusNoContent, resp.StatusCode) |
| 139 | + |
| 140 | + // second delete → 404 |
| 141 | + req2, _ := http.NewRequest(http.MethodDelete, deleteURL, nil) |
| 142 | + resp2, err := client.Do(req2) |
| 143 | + require.NoError(t, err) |
| 144 | + resp2.Body.Close() |
| 145 | + assert.Equal(t, http.StatusNotFound, resp2.StatusCode) |
| 146 | +} |
0 commit comments