Skip to content

Commit

Permalink
cock
Browse files Browse the repository at this point in the history
- fixed GetPackage returning zero value instead of nil
- fully implemented ValidateManifest
- slightly improve how ValidateManifest is tested
  • Loading branch information
Owen3H committed Apr 12, 2024
1 parent ace5e80 commit 344c2ca
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 26 deletions.
5 changes: 4 additions & 1 deletion experimental/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package experimental
import (
"encoding/base64"
"fmt"
"reflect"
"strings"

"github.com/the-egg-corp/thundergo/util"
Expand Down Expand Up @@ -52,5 +53,7 @@ func GetCommunity(nameOrId string) (*Community, bool, error) {
// If an error occurred or it was not found, the result will be nil.
func GetPackage(author string, name string) (*Package, error) {
endpoint := fmt.Sprint("api/experimental/package/", author, "/", name)
return util.JsonGetRequest[*Package](endpoint)
pkg, err := util.JsonGetRequest[Package](endpoint)

return lo.Ternary(reflect.ValueOf(pkg).IsZero(), nil, &pkg), err
}
77 changes: 64 additions & 13 deletions experimental/submission.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ import (
"errors"
"image"
_ "image/png"
"regexp"
"strings"

"github.com/hashicorp/go-version"
"github.com/the-egg-corp/thundergo/util"
)

Expand All @@ -22,7 +25,7 @@ type PackageSubmissionMetadata struct {
type ManifestMetadata struct {
Name string `json:"name"`
VersionNumber string `json:"version_number"`
WebsiteURL string `json:"website_url"`
WebsiteURL *string `json:"website_url"`
Description string `json:"description"`
Dependencies []string `json:"dependencies"`
}
Expand All @@ -32,6 +35,10 @@ type IconValidatorParams struct {
ImageData []byte
}

func NewErr(msg string) error {
return errors.New(msg)
}

// TODO: Implement this
func SubmitPackage(data []byte) (bool, error) {
return false, nil
Expand All @@ -42,27 +49,64 @@ func ValidateReadme(data []byte) (bool, error) {
return false, nil
}

// TODO: Implement this
func ValidateManifest(data []byte) (bool, []string) {
func ValidateManifest(author string, data []byte) (bool, []string, error) {
var manifest ManifestMetadata
json.Unmarshal(data, &manifest)

var errors []string

AddIfEmpty(&errors, &manifest.Name, "required manifest property 'name' is empty or unspecified")
AddIfEmpty(&errors, &manifest.VersionNumber, "required manifest property 'version_number' is empty or unspecified")
AddIfEmpty(&errors, &manifest.Description, "required manifest property 'description' is empty or unspecified")
err := json.Unmarshal(data, &manifest)
if err != nil {
errors = append(errors, err.Error())
}

pkg, _ := GetPackage(author, manifest.Name)
if pkg == nil {
return false, nil, NewErr("package not found under the specified author")
}

AddIfEmpty(&errors, &manifest.Name, "required property 'name' is empty or unspecified")
AddIfInvalid(&errors, &manifest.Name, "property 'name' must contain only valid characters (a-z A-Z 0-9 _)")
AddIfEmpty(&errors, &manifest.Description, "required property 'description' is empty or unspecified")

verEmpty := AddIfEmpty(&errors, &manifest.VersionNumber, "required property 'version_number' is empty or unspecified")
if !verEmpty {
sv, _ := util.CheckSemVer(manifest.VersionNumber)
AddIfFalse(&errors, &sv, "property 'version_number' does not follow semantic versioning (major.minor.patch)")

if !sv {
return false, errors, nil
}

sv, _ := util.CheckSemVer(manifest.VersionNumber)
AddIfFalse(&errors, &sv, "manifest version does not follow semantic versioning (major.minor.patch)")
verA, _ := version.NewSemver(manifest.VersionNumber)
verB, _ := version.NewSemver(pkg.Latest.VersionNumber)

return len(errors) < 1, errors
if verA.LessThanOrEqual(verB) {
errors = append(errors, "property 'version_number' must be higher than the latest")
}
}

if manifest.WebsiteURL == nil {
errors = append(errors, "required property 'website_url' is unspecified")
} else {
url := strings.ToLower(*manifest.WebsiteURL)
if !(strings.HasPrefix(url, "http://") || strings.HasPrefix(url, "https://")) {
errors = append(errors, "property 'website_url' must be a valid URL")
}
}

if manifest.Dependencies == nil {
errors = append(errors, "manifest property 'dependencies' is required")
}

return len(errors) < 1, errors, nil
}

func AddIfEmpty(arr *[]string, str *string, errStr string) {
if *str == "" || str == nil {
func AddIfEmpty(arr *[]string, str *string, errStr string) bool {
empty := *str == "" || str == nil
if empty {
*arr = append(*arr, errStr)
}

return empty
}

func AddIfFalse(arr *[]string, val *bool, errStr string) {
Expand All @@ -71,6 +115,13 @@ func AddIfFalse(arr *[]string, val *bool, errStr string) {
}
}

func AddIfInvalid(arr *[]string, str *string, errStr string) {
matched, _ := regexp.MatchString(`^[a-zA-Z0-9_]+$`, *str)
if !matched {
*arr = append(*arr, errStr)
}
}

// Decodes image data and validates that the image is a PNG and the dimensions are 256x256.
//
// Additionally, if the file name is specified, it will validate that it is named correctly.
Expand Down
8 changes: 4 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ go 1.22.1
require (
github.com/samber/lo v1.39.0
github.com/sanity-io/litter v1.5.5
github.com/hashicorp/go-version v1.6.0
github.com/dustin/go-humanize v1.0.1
github.com/go-resty/resty/v2 v2.12.0
)

require golang.org/x/net v0.22.0 // indirect

require (
github.com/dustin/go-humanize v1.0.1 // indirect
github.com/go-resty/resty/v2 v2.12.0
golang.org/x/exp v0.0.0-20220303212507-bbda1eaf7a17 // indirect
golang.org/x/net v0.22.0 // indirect
)
3 changes: 2 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkp
github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto=
github.com/go-resty/resty/v2 v2.12.0 h1:rsVL8P90LFvkUYq/V5BTVe203WfRIU4gvcf+yfzJzGA=
github.com/go-resty/resty/v2 v2.12.0/go.mod h1:o0yGPrkS3lOe1+eFajk6kBW8ScXzwU3hD69/gt2yB/0=
github.com/hashicorp/go-version v1.6.0 h1:feTTfFNnjP967rlCxM/I9g701jU+RN74YKx2mOkIeek=
github.com/hashicorp/go-version v1.6.0/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09ZGVZPK5anwXA=
github.com/pmezard/go-difflib v0.0.0-20151028094244-d8ed2627bdf0 h1:GD+A8+e+wFkqje55/2fOVnZPkoDIu1VooBWfNrnY8Uo=
github.com/pmezard/go-difflib v0.0.0-20151028094244-d8ed2627bdf0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/samber/lo v1.39.0 h1:4gTz1wUhNYLhFSKl6O+8peW0v2F4BCY034GRpU9WnuA=
Expand Down Expand Up @@ -53,7 +55,6 @@ golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8=
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
golang.org/x/time v0.5.0/go.mod h1:3BpzKBy/shNhVucY/MWOyx10tF3SFh9QdLuxbVysPQM=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
Expand Down
28 changes: 21 additions & 7 deletions tests/submission_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package tests

import (
"fmt"
"os"
"testing"

Expand All @@ -13,15 +14,15 @@ func TestValidateIcon(t *testing.T) {
icon, err := os.ReadFile("../test_icon.png")

if err != nil {
t.Fatalf(err.Error())
t.Fatal(err.Error())
}

valid, err := TSGO.ValidateIcon(TSGO.IconValidatorParams{
ImageData: icon,
})

if err != nil {
t.Fatalf(err.Error())
t.Fatal(err.Error())
}

util.PrettyPrint(valid)
Expand All @@ -32,15 +33,28 @@ func TestValidateManifest(t *testing.T) {
data, err := os.ReadFile("../test_manifest.json")

if err != nil {
t.Fatalf(err.Error())
t.Fatal(err.Error())
}

valid, errs := TSGO.ValidateManifest(data)
valid, errs, err := TSGO.ValidateManifest("Owen3H", data)

if err != nil {
t.Fatalf(err.Error())
t.Fatal(err.Error())
}

util.PrettyPrint(valid)
util.PrettyPrint(errs)
fmt.Println("Valid: ", valid)

if len(errs) > 0 {
util.PrettyPrint(errs)

if valid {
t.Fatal("errors were returned, but manifest is still valid")
}

return
}

if !valid {
t.Fatal("manifest was marked as invalid despite empty errors array")
}
}

0 comments on commit 344c2ca

Please sign in to comment.