Skip to content

Commit

Permalink
add actual images for tests and check mimetype (#89)
Browse files Browse the repository at this point in the history
* add actual image and check mimetype

* fix test
  • Loading branch information
shrimalmadhur authored Dec 19, 2023
1 parent 8ec3204 commit 68f22d2
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 11 deletions.
19 changes: 19 additions & 0 deletions types/operator_metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,18 @@ package types
import (
"errors"
"fmt"
"io"
"net/http"
"net/url"
"path/filepath"
"regexp"
"strings"
)

const (
PngMimeType = "image/png"
)

// OperatorMetadata is the metadata operator uploads while registering
// itself to eigenlayer
type OperatorMetadata struct {
Expand Down Expand Up @@ -119,6 +125,19 @@ func isImageURL(urlString string) error {
// Check if the extension is in the list of image extensions
for _, imgExt := range imageExtensions {
if strings.EqualFold(extension, imgExt) {
imageResponse, err := http.Get(urlString)
if err != nil {
return err
}
imageBytes, err := io.ReadAll(imageResponse.Body)
if err != nil {
return err
}

contentType := http.DetectContentType(imageBytes)
if contentType != PngMimeType {
return errors.New("invalid image format. only png is supported")
}
return nil
}
}
Expand Down
31 changes: 21 additions & 10 deletions types/operator_metadata_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ func TestOperatorMetadata(t *testing.T) {
metadata: OperatorMetadata{
Name: "test",
Description: "test",
Logo: "https://test.com/test.png",
Logo: "https://goerli-operator-metadata.s3.amazonaws.com/eigenlayer.png",
Twitter: "https://twitter.com/test",
Website: "https://test.com",
},
Expand All @@ -24,7 +24,7 @@ func TestOperatorMetadata(t *testing.T) {
metadata: OperatorMetadata{
Name: "",
Description: "test",
Logo: "https://test.com/test.png",
Logo: "https://goerli-operator-metadata.s3.amazonaws.com/eigenlayer.png",
Twitter: "https://twitter.com/test",
Website: "https://test.com",
},
Expand All @@ -35,7 +35,7 @@ func TestOperatorMetadata(t *testing.T) {
metadata: OperatorMetadata{
Name: "test",
Description: "",
Logo: "https://test.com/test.png",
Logo: "https://goerli-operator-metadata.s3.amazonaws.com/eigenlayer.png",
Twitter: "https://twitter.com/test",
Website: "https://test.com",
},
Expand All @@ -52,12 +52,23 @@ func TestOperatorMetadata(t *testing.T) {
},
wantErr: true,
},
{
name: "Invalid metadata - invalid mime type with correct extension",
metadata: OperatorMetadata{
Name: "test",
Description: "My operator",
Logo: "https://goerli-operator-metadata.s3.amazonaws.com/cat.png",
Twitter: "https://twitter.com/test",
Website: "https://test.com",
},
wantErr: true,
},
{
name: "Invalid metadata - description > 500 characters",
metadata: OperatorMetadata{
Name: "test",
Description: "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.",
Logo: "https://test.com/test.png",
Logo: "https://goerli-operator-metadata.s3.amazonaws.com/eigenlayer.png",
Twitter: "https://twitter.com/test",
Website: "https://test.com",
},
Expand Down Expand Up @@ -101,7 +112,7 @@ func TestOperatorMetadata(t *testing.T) {
metadata: OperatorMetadata{
Name: "test",
Description: "test",
Logo: "https://test.com/test.png",
Logo: "https://goerli-operator-metadata.s3.amazonaws.com/eigenlayer.png",
Twitter: "https://twitter.com/test",
Website: "https",
},
Expand All @@ -112,7 +123,7 @@ func TestOperatorMetadata(t *testing.T) {
metadata: OperatorMetadata{
Name: "test",
Description: "test",
Logo: "https://test.com/test.png",
Logo: "https://goerli-operator-metadata.s3.amazonaws.com/eigenlayer.png",
Twitter: "https://twitter.com/test",
Website: "https:/test.com",
},
Expand All @@ -123,7 +134,7 @@ func TestOperatorMetadata(t *testing.T) {
metadata: OperatorMetadata{
Name: "test",
Description: "test",
Logo: "https://test.com/test.png",
Logo: "https://goerli-operator-metadata.s3.amazonaws.com/eigenlayer.png",
Twitter: "https://twitter.com/test",
Website: "ps://test.com",
},
Expand All @@ -134,7 +145,7 @@ func TestOperatorMetadata(t *testing.T) {
metadata: OperatorMetadata{
Name: "test",
Description: "test",
Logo: "https://test.com/test.png",
Logo: "https://goerli-operator-metadata.s3.amazonaws.com/eigenlayer.png",
Twitter: "http",
Website: "https://test.com",
},
Expand All @@ -145,7 +156,7 @@ func TestOperatorMetadata(t *testing.T) {
metadata: OperatorMetadata{
Name: "test",
Description: "test",
Logo: "https://test.com/test.png",
Logo: "https://goerli-operator-metadata.s3.amazonaws.com/eigenlayer.png",
Twitter: "ht://twitter.com/test",
Website: "https:/test.com",
},
Expand All @@ -156,7 +167,7 @@ func TestOperatorMetadata(t *testing.T) {
metadata: OperatorMetadata{
Name: "test",
Description: "test",
Logo: "https://test.com/test.png",
Logo: "https://goerli-operator-metadata.s3.amazonaws.com/eigenlayer.png",
Twitter: "https://twitt",
Website: "ps://test.com",
},
Expand Down
2 changes: 1 addition & 1 deletion types/operator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ func TestOperatorValidate(t *testing.T) {
metadata := OperatorMetadata{
Name: "test",
Description: "test",
Logo: "https://test.com/test.png",
Logo: "https://goerli-operator-metadata.s3.amazonaws.com/eigenlayer.png",
Twitter: "https://twitter.com/test",
Website: "https://test.com",
}
Expand Down

0 comments on commit 68f22d2

Please sign in to comment.