Compare commits

...

2 Commits

7 changed files with 180 additions and 30 deletions

View File

@ -0,0 +1,32 @@
package configloader
import (
"errors"
"net/http"
"strconv"
)
// Validates an institution ID.
// A valid institution ID returns a HTTP 200 response code when queried
// <instance_url> + "/institution/" + institution_id.
// Returns either the entered instance URL or an error.
func ValidateInstitutionId(institutionId int, instanceUrl string) (int, error) {
if institutionId < 1 {
return 0, errors.New("Institution ID cannot be negative")
}
resp, err := http.Get(instanceUrl + "/institution/" + strconv.Itoa(institutionId))
if err != nil {
return 0, err
}
if resp.StatusCode != 200 {
return 0, errors.New("The institution page does not respond with HTTP 200, the institution does not seem to exist")
}
return institutionId, nil
}

View File

@ -0,0 +1,28 @@
package configloader
import (
"testing"
)
// Ensures institution ID cannot be negative.
// does not exist.
func TestValidateInstitutionIdReturnsErrorOnNegativeInput(t *testing.T) {
_, err := ValidateInstitutionId(-1, "https://hessen.museum-digital.de")
if err == nil {
t.Fatalf("ValidateInstitutionId() accepts should not accept negative IDs (test input: -1)")
}
}
// Ensures non-existing IDs are rejected.
func TestValidateInstitutionIdReturnsErrorOnNonExistingId(t *testing.T) {
_, err := ValidateInstitutionId(300000, "https://hessen.museum-digital.de")
if err == nil {
t.Fatalf("An institution 300000 should not exist in museum-digital:hessen (otherwise, adjust the test). A non-existing ID should be rejected.")
}
}

View File

@ -0,0 +1,35 @@
package configloader
import (
"os"
"io/fs"
"errors"
"path/filepath"
)
// Checks if a directory is the entered
// directory can be used as an source directory
// for export data. As such, it does need to
// exist, and it needs to not contain unexpected files.
// Returns the directory name or an error.
func ValidateUploadDir(path string) (string, error) {
// Check the path actually exists
pathInfo, err := os.Stat(path)
if errors.Is(err, fs.ErrNotExist) {
return "", err
}
// Check the input path is a directory
if !pathInfo.IsDir() {
return "", errors.New("The upload directory " + path + " is not a directory")
}
// Return absolute path
absPath, absPathErr := filepath.Abs(path)
if (absPathErr != nil) {
return "", errors.New("Invalid path")
}
return absPath, nil
}

View File

@ -0,0 +1,61 @@
package configloader
import (
"testing"
"os"
"path/filepath"
)
// ValidateUploadDir() should return an error if the entered directory
// does not exist.
func TestValidateUploadDirFailsNonExistentDir(t *testing.T) {
returnVal, err := ValidateUploadDir("/this-directory-surely-does-not-exist")
if err == nil {
t.Fatalf("ValidateUploadDir() accepts non-existent directory")
}
if returnVal != "" {
t.Fatalf("Return valid must be empty on failure")
}
}
// Test to ensure that an error is thrown if a file is entered, not a directory.
func TestValidateUploadDirFailsWithDirContainingUnexpectedFiles(t *testing.T) {
tmpDir := os.TempDir()
os.OpenFile(tmpDir + "/empty-file-for-testing-that-dirs-are-not-accepted.txt", os.O_RDONLY|os.O_CREATE, 0666)
returnVal, err := ValidateUploadDir(tmpDir + "/empty-file-for-testing-that-dirs-are-not-accepted.txt")
if err == nil {
t.Fatalf("ValidateUploadDir() accepts fails as upload directory")
}
if returnVal != "" {
t.Fatalf("Return valid must be empty on failure")
}
}
func TestValidateUploadDirSucceedsWithValidDir(t *testing.T) {
tmpDir := os.TempDir()
testDir := filepath.Join(tmpDir, "/existing-dir-for-import")
mkdirErr := os.MkdirAll(testDir, os.ModePerm)
if mkdirErr != nil {
panic("Test failure: Failed to create test dir")
}
returnVal, err := ValidateUploadDir(tmpDir + "/existing-dir-for-import")
if err != nil {
t.Fatalf("ValidateUploadDir() fails with an existing, valid directory")
}
if returnVal == "" {
t.Fatalf("Return valid must be empty on failure")
}
}

View File

@ -1,4 +1,4 @@
package configLoader package configloader
type MDWebDavUploaderConfig struct { type MDWebDavUploaderConfig struct {
InstanceLink string `json:"instance"` InstanceLink string `json:"instance"`
@ -23,61 +23,45 @@ func getConfigFilepath() string {
} }
// Loads configuration from the configuration file (located using
// getConfigFilepath()).
func loadFromFile() MDWebDavUploaderConfig {
}
// Returns a uniform filepath for the cache for the list of // Returns a uniform filepath for the cache for the list of
// available parsers. // available parsers.
func getParserCacheFilepath() string { 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 // Stores a parser list to a cache file (located via
// getParserCacheFilepath()). // getParserCacheFilepath()).
func storeParserList(parserList []ParserListItem) { func StoreParserList(parserList []ParserListItem) {
} }
// Returns a list of the available parsers. // Returns a list of the available parsers.
func listParsers() []ParserListItem { func ListParsers() []ParserListItem {
} }
// Checks if inputParser is in the list of available parsers. // Checks if inputParser is in the list of available parsers.
// Returns the parser name or an error. // Returns the parser name or an error.
func validateParser(inputParser string) (string, error) { func ValidateParser(inputParser string) (string, error) {
}
// Checks if a directory is the entered directory can be used
// as an source directory for export data. As such, it does need
// to exist.
// Returns the directory name or an error.
func validateDir(dir string) (string, error) {
} }
// Validates an input mail address. // Validates an input mail address.
// Returns the valid mail address or an eQueries therror. // Returns the valid mail address or an eQueries therror.
func validateMail(mail string) (string, error) { func ValidateMail(mail string) (string, error) {
} }
// Validates an entered instance URL. // Validates an entered instance URL.
// Returns the entered URL in a unified form (minus optional // Returns the entered URL in a unified form (minus optional
// trailing slashes and the /musdb suffix) or an error. // trailing slashes and the /musdb suffix) or an error.
func validateInstanceLink(instanceUrl string) (string, error) { func ValidateInstanceLink(instanceUrl string) (string, error) {
}
// Validates an institution ID.
// A valid institution ID returns a HTTP 200 response code when queried
// <instance_url> + "/institution/" + institution_id.
// Returns either the entered instance URL or an error.
func validateInstitutionId(institutionId int, instanceUrl string) (int, error) {
} }

View File

@ -1,6 +1,6 @@
package setup package setup
import "gitea.armuli.eu/museum-digital/museum-digital-webdav-uploader/src/configLoader" import "gitea.armuli.eu/museum-digital/museum-digital-webdav-uploader/src/configloader"
// Fetches user information from the relevant musdb API: // Fetches user information from the relevant musdb API:
// https://demo.museum-digital.org/musdb/swagger/#/user/userReadOwnData // https://demo.museum-digital.org/musdb/swagger/#/user/userReadOwnData
@ -18,7 +18,10 @@ func getWebDavAccessTokenFromApi(instanceUrl string) (string, error) {
func setup() error { func setup() error {
// Request input data // Request input data
validateInstanceLink() validateInstanceLink()
// Get user name
// Get password
} }

7
src/setup/setup_test.go Normal file
View File

@ -0,0 +1,7 @@
package setup
import (
"testing"
)