Add add config validators
This commit is contained in:
parent
dfe1bdfeb4
commit
d4c83e27d2
62
src/configloader/ListParsers.go
Normal file
62
src/configloader/ListParsers.go
Normal file
@ -0,0 +1,62 @@
|
||||
package configloader
|
||||
|
||||
import _ "embed"
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"slices"
|
||||
)
|
||||
|
||||
type ParserListItem struct {
|
||||
Title string `json:"title"`
|
||||
Comment string `json:"comment"`
|
||||
Authors []string `json:"authors"`
|
||||
Links []string `json:"links"`
|
||||
}
|
||||
|
||||
// Load parser list from JSON retrieved from musdb's API
|
||||
// See https://<subdomain>.museum-digital.org/musdb/api/meta/list_import_parsers
|
||||
// The embed module requires the variable to not be bound to a function.
|
||||
|
||||
//go:embed parser_list.json
|
||||
var rawParserList []byte
|
||||
|
||||
// Returns a list of the available parsers.
|
||||
func ListParsers() []ParserListItem {
|
||||
|
||||
parserListFromApi := []ParserListItem{}
|
||||
// parserListFromApi := new(ApiParserListFormat)
|
||||
json.Unmarshal(rawParserList, &parserListFromApi)
|
||||
print(parserListFromApi)
|
||||
|
||||
return parserListFromApi
|
||||
|
||||
}
|
||||
|
||||
// Checks if inputParser is in the list of available parsers.
|
||||
// Returns the parser name or an error.
|
||||
func ValidateParser(inputParser string) (string, error) {
|
||||
|
||||
parserList := ListParsers()
|
||||
parserTitles := []string{}
|
||||
|
||||
for _, p := range(parserList) {
|
||||
parserTitles = append(parserTitles, p.Title)
|
||||
}
|
||||
|
||||
if slices.Contains(parserTitles, inputParser) {
|
||||
return inputParser, nil
|
||||
}
|
||||
|
||||
// Accepted invalid modes to clean up:
|
||||
// ParserLido > Lido
|
||||
// lido > Lido
|
||||
cleaned := inputParser[6:]
|
||||
if slices.Contains(parserTitles, cleaned) {
|
||||
return cleaned, nil
|
||||
}
|
||||
|
||||
return "", errors.New("Invalid parser selected")
|
||||
|
||||
}
|
55
src/configloader/ListParsers_test.go
Normal file
55
src/configloader/ListParsers_test.go
Normal file
@ -0,0 +1,55 @@
|
||||
package configloader
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
// Tests that the list of parsers is not empty.
|
||||
func TestListParserReturnsNonEmptyResult(t *testing.T) {
|
||||
|
||||
parserList := ListParsers()
|
||||
if len(parserList) == 0 {
|
||||
t.Fatalf("The list of returned parsers is empty")
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// Ensures that validating a parser will not accept an invalid parser name.
|
||||
func TestInvalidParserIsNotAccepted(t *testing.T) {
|
||||
|
||||
parser, err := ValidateParser("Liddasfasdo")
|
||||
if parser != "" {
|
||||
t.Fatalf("ValidateParser() should return an empty string + error")
|
||||
}
|
||||
if err == nil {
|
||||
t.Fatalf("ValidateParser() did not return an error where it should")
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// Ensures that a parser is valid at its expected name.
|
||||
func TestValidParserIsAccepted(t *testing.T) {
|
||||
|
||||
parser, err := ValidateParser("Lido")
|
||||
if parser != "Lido" {
|
||||
t.Fatalf("Test expected validated parser for input 'Lido' to be 'Lido'")
|
||||
}
|
||||
if err != nil {
|
||||
t.Fatalf("ValidateParser() returned an error where it should not")
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// Prepending the prefix "Parser" to a parser name should work as well.
|
||||
// In this case, the prefix should be removed by ValidateParser().
|
||||
func TestValidParserIsAcceptedAndCleaned(t *testing.T) {
|
||||
|
||||
parser, err := ValidateParser("ParserLido")
|
||||
if parser != "Lido" {
|
||||
t.Fatalf("Test expected validated parser for input 'ParserLido' to be 'Lido'")
|
||||
}
|
||||
if err != nil {
|
||||
t.Fatalf("ValidateParser() returned an error where it should not")
|
||||
}
|
||||
|
||||
}
|
47
src/configloader/ValidateInstanceLink.go
Normal file
47
src/configloader/ValidateInstanceLink.go
Normal file
@ -0,0 +1,47 @@
|
||||
package configloader
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"net/url"
|
||||
"net/http"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// Validates a museum-digital instance URL to upload to.
|
||||
// The link must be a valid URL. As it can be expected that people will
|
||||
// enter broken values, the URL should be reduced to its hostname and
|
||||
// rebuilt from there.
|
||||
func ValidateInstanceLink(instanceUrl string) (string, error) {
|
||||
|
||||
parsed, err := url.Parse(instanceUrl)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
// Check the hostname contains "museum-digital.de" or "museum-digital.org"
|
||||
if !strings.HasSuffix(parsed.Host, "museum-digital.de") && !strings.HasSuffix(parsed.Host, "museum-digital.org") {
|
||||
return "", errors.New("This tool only supports uploading to XYZ.museum-digital.de or XYZ.museum-digital.org")
|
||||
}
|
||||
|
||||
// Check /musdb is available on the md instance
|
||||
musdbUrl := url.URL{
|
||||
Scheme: "https",
|
||||
Host: parsed.Host,
|
||||
Path: "/musdb",
|
||||
}
|
||||
resp, err := http.Get(musdbUrl.String())
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
if resp.StatusCode > 399 {
|
||||
return "", errors.New("The museum-digital subdomain does not contain a musdb path")
|
||||
}
|
||||
|
||||
// https should be assumed and enforced.
|
||||
url := url.URL{
|
||||
Scheme: "https",
|
||||
Host: parsed.Host,
|
||||
}
|
||||
return url.String(), nil
|
||||
|
||||
}
|
55
src/configloader/ValidateInstanceLink_test.go
Normal file
55
src/configloader/ValidateInstanceLink_test.go
Normal file
@ -0,0 +1,55 @@
|
||||
package configloader
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
// Test that ValidateInstanceLink() fails on non-URLs.
|
||||
func TestValidateInstanceFailsOnNonUrl(t *testing.T) {
|
||||
|
||||
_, err := ValidateInstanceLink("abcmuseum-digital.org")
|
||||
|
||||
if err == nil {
|
||||
t.Fatalf("ValidateInstanceLink() does not return an error on a non-URL")
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// Test that ValidateInstanceLink() fails on links outside of md.
|
||||
func TestValidateInstanceLinkFailsOutsideOfMd(t *testing.T) {
|
||||
|
||||
_, err := ValidateInstanceLink("test.museum-digital.de")
|
||||
|
||||
if err == nil {
|
||||
t.Fatalf("ValidateInstanceLink() accepts URLs outside of the main museum-digital")
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// Test that ValidateInstanceLink() fails on valid md aggregated instances
|
||||
// (uploads can only happen to regional instances / those that have a dedicated
|
||||
// instance of musdb).
|
||||
func TestValidateInstanceLinkFailsWithAggregatedInstance(t *testing.T) {
|
||||
|
||||
_, err := ValidateInstanceLink("https://nat.museum-digital.de")
|
||||
|
||||
if err == nil {
|
||||
t.Fatalf("ValidateInstanceLink() accepts URLs of aggregated instance")
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// Test that ValidateInstanceLink() works with a valid instance of md.
|
||||
func TestValidateInstanceWorks(t *testing.T) {
|
||||
|
||||
result, err := ValidateInstanceLink("https://hessen.museum-digital.de")
|
||||
|
||||
if err != nil {
|
||||
t.Fatalf("ValidateInstanceLink() returns an error where it should work")
|
||||
}
|
||||
if result != "https://hessen.museum-digital.de" {
|
||||
t.Fatalf("Output of ValidateInstanceLink() is not https://hessen.museum-digital.de where it should be")
|
||||
}
|
||||
|
||||
|
||||
}
|
15
src/configloader/ValidateMail.go
Normal file
15
src/configloader/ValidateMail.go
Normal file
@ -0,0 +1,15 @@
|
||||
package configloader
|
||||
|
||||
import "net/mail"
|
||||
|
||||
// Validates an input mail address.
|
||||
// Returns the valid mail address or an eQueries therror.
|
||||
func ValidateMail(email string) (string, error) {
|
||||
|
||||
_, err := mail.ParseAddress(email)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return email, nil
|
||||
|
||||
}
|
27
src/configloader/ValidateMail_test.go
Normal file
27
src/configloader/ValidateMail_test.go
Normal file
@ -0,0 +1,27 @@
|
||||
package configloader
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
// Test that ValidateMail() returns the mail address if it is valid.
|
||||
func TestValidateMailAcceptsValidMailAddress(t *testing.T) {
|
||||
|
||||
_, err := ValidateMail("test@example.com")
|
||||
|
||||
if err != nil {
|
||||
t.Fatalf("ValidateMail() returns an error on a valid mail address")
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// Test that ValidateMail() filters out invalid mail addresses.
|
||||
func TestValidateMailDoesNotAcceptInvalidMailAddress(t *testing.T) {
|
||||
|
||||
_, err := ValidateMail("test")
|
||||
|
||||
if err == nil {
|
||||
t.Fatalf("ValidateMail() does not prevent entering an invalid mail address")
|
||||
}
|
||||
|
||||
}
|
@ -11,11 +11,6 @@ type MDWebDavUploaderConfig struct {
|
||||
PublishOnImport bool `json:"visible"`
|
||||
}
|
||||
|
||||
type ParserListItem struct {
|
||||
name string
|
||||
|
||||
}
|
||||
|
||||
// Returns a uniform filepath for the configuration of this tool.
|
||||
// To be compatible across operating systems, this will be a JSON
|
||||
// file in the same directory as the current programm.
|
||||
@ -23,45 +18,8 @@ func getConfigFilepath() string {
|
||||
|
||||
}
|
||||
|
||||
// Returns a uniform filepath for the cache for the list of
|
||||
// available parsers.
|
||||
func getParserCacheFilepath() string {
|
||||
|
||||
}
|
||||
|
||||
// Loads configuration from the configuration file (located using
|
||||
// getConfigFilepath()).
|
||||
func LoadFromFile() MDWebDavUploaderConfig {
|
||||
|
||||
}
|
||||
|
||||
// Stores a parser list to a cache file (located via
|
||||
// getParserCacheFilepath()).
|
||||
func StoreParserList(parserList []ParserListItem) {
|
||||
|
||||
}
|
||||
|
||||
// Returns a list of the available parsers.
|
||||
func ListParsers() []ParserListItem {
|
||||
|
||||
}
|
||||
|
||||
|
||||
// Checks if inputParser is in the list of available parsers.
|
||||
// Returns the parser name or an error.
|
||||
func ValidateParser(inputParser string) (string, error) {
|
||||
|
||||
}
|
||||
|
||||
// Validates an input mail address.
|
||||
// Returns the valid mail address or an eQueries therror.
|
||||
func ValidateMail(mail string) (string, error) {
|
||||
|
||||
}
|
||||
|
||||
// Validates an entered instance URL.
|
||||
// Returns the entered URL in a unified form (minus optional
|
||||
// trailing slashes and the /musdb suffix) or an error.
|
||||
func ValidateInstanceLink(instanceUrl string) (string, error) {
|
||||
|
||||
}
|
||||
|
1
src/configloader/parser_list.json
Normal file
1
src/configloader/parser_list.json
Normal file
File diff suppressed because one or more lines are too long
Loading…
x
Reference in New Issue
Block a user