Skip to content

Commit

Permalink
Fix some tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
hwrdprkns committed Feb 5, 2024
1 parent 536e4f8 commit 84c3e22
Show file tree
Hide file tree
Showing 4 changed files with 139 additions and 8 deletions.
6 changes: 3 additions & 3 deletions images_variants.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ type CreateImagesVariantParams struct {
}

type UpdateImagesVariantParams struct {
ID string
ID string `json:"-"`
NeverRequireSignedURLs bool `json:"neverRequireSignedURLs,omitempty"`
Options ImagesVariantsOptions `json:"options,omitempty"`
}
Expand Down Expand Up @@ -94,7 +94,7 @@ func (api *API) GetImagesVariantDetails(ctx context.Context, rc *ResourceContain
return ImagesVariant{}, fmt.Errorf("%s: %w", errUnmarshalError, err)
}

return ImagesVariant{}, nil
return imagesVariantDetailResponse.Result.Variant, nil
}

// Specify variants that allow you to resize images for different use cases.
Expand Down Expand Up @@ -140,7 +140,7 @@ func (api *API) DeleteImagesVariant(ctx context.Context, rc *ResourceContainer,
// Updating a variant purges the cache for all images associated with the variant.
//
// API Reference: https://developers.cloudflare.com/api/operations/cloudflare-images-variants-variant-details
func (api *API) UpdateImagesVariant(ctx context.Context, rc *ResourceContainer, variantId string, params UpdateImagesVariantParams) (ImagesVariant, error) {
func (api *API) UpdateImagesVariant(ctx context.Context, rc *ResourceContainer, params UpdateImagesVariantParams) (ImagesVariant, error) {
if rc.Identifier == "" {
return ImagesVariant{}, ErrMissingAccountID
}
Expand Down
105 changes: 100 additions & 5 deletions images_variants_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ import (
"github.com/stretchr/testify/assert"
)

const (
testImagesVariantID = "hero"
)

func TestImageVariants_ListVariants(t *testing.T) {
setup()
defer teardown()
Expand Down Expand Up @@ -45,25 +49,116 @@ func TestImageVariants_Delete(t *testing.T) {
defer teardown()

handler := func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, http.MethodGet, r.Method, "Expected method '%s', got %s", http.MethodGet, r.Method)

assert.Equal(t, http.MethodDelete, r.Method, "Expected method '%s', got %s", http.MethodDelete, r.Method)
w.Header().Set("content-type", "application/json")
fmt.Fprint(w, loadFixture("tunnel", "configuration"))
fmt.Fprint(w, `{
"success": true,
"errors": [],
"messages": [],
"result": {}
}`)
}

url := fmt.Sprintf("/account/%s/images/v1/variants/%s", testAccountID, "hero")
url := fmt.Sprintf("/accounts/%s/images/v1/variants/%s", testAccountID, testImagesVariantID)
mux.HandleFunc(url, handler)

err := client.DeleteImagesVariant(context.Background(), AccountIdentifier(testAccountID), "hero")
err := client.DeleteImagesVariant(context.Background(), AccountIdentifier(testAccountID), testImagesVariantID)
assert.NoError(t, err)
}

func TestImagesVariants_GetDetails(t *testing.T) {
setup()
defer teardown()

handler := func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, http.MethodGet, r.Method, "Expected method '%s', got %s", http.MethodGet, r.Method)
w.Header().Set("content-type", "application/json")
fmt.Fprint(w, loadFixture("images_variants", "single_full"))
}

url := fmt.Sprintf("/accounts/%s/images/v1/variants/%s", testAccountID, testImagesVariantID)
mux.HandleFunc(url, handler)

want := ImagesVariant{
ID: "hero",
NeverRequireSignedURLs: true,
Options: ImagesVariantsOptions{
Fit: "scale-down",
Height: 768,
Width: 1366,
Metadata: "none",
},
}

got, err := client.GetImagesVariantDetails(context.Background(), AccountIdentifier(testAccountID), testImagesVariantID)
if assert.NoError(t, err) {
assert.Equal(t, want, got)
}
}

func TestImagesVariants_CreateVariant(t *testing.T) {
setup()
defer teardown()

handler := func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, http.MethodPost, r.Method, "Expected method '%s', got %s", http.MethodPost, r.Method)
w.Header().Set("content-type", "application/json")
fmt.Fprint(w, loadFixture("images_variants", "single_full"))
}

url := fmt.Sprintf("/accounts/%s/images/v1/variants", testAccountID)
mux.HandleFunc(url, handler)

want := ImagesVariant{
ID: "hero",
NeverRequireSignedURLs: true,
Options: ImagesVariantsOptions{
Fit: "scale-down",
Height: 768,
Width: 1366,
Metadata: "none",
},
}

got, err := client.CreateImagesVariant(context.Background(), AccountIdentifier(testAccountID), CreateImagesVariantParams{
ImagesVariant: want,
})
if assert.NoError(t, err) {
assert.Equal(t, want, got)
}
}

func TestImagesVariants_UpdateVariant(t *testing.T) {
setup()
defer teardown()

handler := func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, http.MethodPatch, r.Method, "Expected method '%s', got %s", http.MethodPatch, r.Method)
w.Header().Set("content-type", "application/json")
fmt.Fprint(w, loadFixture("images_variants", "single_full"))
}

url := fmt.Sprintf("/accounts/%s/images/v1/variants/%s", testAccountID, testImagesVariantID)
mux.HandleFunc(url, handler)

want := ImagesVariant{
ID: "hero",
NeverRequireSignedURLs: true,
Options: ImagesVariantsOptions{
Fit: "scale-down",
Height: 768,
Width: 1366,
Metadata: "none",
},
}

got, err := client.UpdateImagesVariant(context.Background(), AccountIdentifier(testAccountID), UpdateImagesVariantParams{

Check failure on line 155 in images_variants_test.go

View workflow job for this annotation

GitHub Actions / lint

S1016: should convert want (type ImagesVariant) to UpdateImagesVariantParams instead of using struct literal (gosimple)

Check failure on line 155 in images_variants_test.go

View workflow job for this annotation

GitHub Actions / lint

S1016: should convert want (type ImagesVariant) to UpdateImagesVariantParams instead of using struct literal (gosimple)

Check failure on line 155 in images_variants_test.go

View workflow job for this annotation

GitHub Actions / lint

S1016: should convert want (type ImagesVariant) to UpdateImagesVariantParams instead of using struct literal (gosimple)
ID: want.ID,
NeverRequireSignedURLs: want.NeverRequireSignedURLs,
Options: want.Options,
})

if assert.NoError(t, err) {
assert.Equal(t, want, got)
}
}
17 changes: 17 additions & 0 deletions testdata/fixtures/images_variants/single_full.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"errors": [],
"messages": [],
"result": {
"variant": {
"id": "hero",
"neverRequireSignedURLs": true,
"options": {
"fit": "scale-down",
"height": 768,
"metadata": "none",
"width": 1366
}
}
},
"success": true
}
19 changes: 19 additions & 0 deletions testdata/fixtures/images_variants/single_list.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"errors": [],
"messages": [],
"result": {
"variants": {
"hero": {
"id": "hero",
"neverRequireSignedURLs": true,
"options": {
"fit": "scale-down",
"height": 768,
"metadata": "none",
"width": 1366
}
}
}
},
"success": true
}

0 comments on commit 84c3e22

Please sign in to comment.