Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/v4backend #115

Open
wants to merge 25 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
3c46560
v4 compat changes
consolethinks Oct 1, 2024
f44116d
fix tests for v4 changes
consolethinks Oct 2, 2024
62389a4
add response body check to ChechMetadataValidity
consolethinks Oct 2, 2024
1114b60
change local_api_server address to match changes in scicatlive
consolethinks Oct 4, 2024
f7ca9db
v4 changes for ingestion command
consolethinks Oct 4, 2024
d864d4c
fix tests for v4 changes
consolethinks Oct 4, 2024
9d8c99b
v4 fixes for datasetArchiver
consolethinks Oct 4, 2024
2dfb02c
v4 updates for datasetGetProposal command
consolethinks Oct 4, 2024
7dfa34c
fix createJob test for v4 changes
consolethinks Oct 4, 2024
335a371
BE v4 changes
Oct 7, 2024
3831c94
general code cleanup: better error handling, preallocating some slices
consolethinks Oct 8, 2024
4757845
fix auth test
consolethinks Oct 8, 2024
fc006ee
move transferType.go to cliutils
consolethinks Oct 10, 2024
c3b8c15
createJob update to v4 BE
consolethinks Oct 10, 2024
22c6991
fix bearer token mapping to header
consolethinks Oct 10, 2024
919ea52
fix createJob_test for v4 changes
consolethinks Oct 10, 2024
f2c0564
v4 fixes for getProposal
consolethinks Oct 10, 2024
a5c70ba
cleanup and update datasetRetriever for v4
consolethinks Oct 11, 2024
5bbab98
remove unused parameter
consolethinks Oct 11, 2024
3c832ee
small fixes for datasetRetriever
consolethinks Oct 11, 2024
bbd0f58
fix ownerGroup setting when requesting archival jobs
consolethinks Oct 18, 2024
dcb57b2
write test for transfer type
consolethinks Oct 18, 2024
48b81b5
use /users/{id}/userIdentity endpoint
consolethinks Oct 18, 2024
ca8c254
changes to conform with CI checks
consolethinks Oct 18, 2024
adc4cb1
check auth token insertion
consolethinks Oct 18, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions cmd/commands/authenticate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ func (m *MockAuthenticator) AuthenticateUser(httpClient *http.Client, APIServer
return map[string]string{"username": "testuser", "password": "testpass"}, []string{"group1", "group2"}
}

func (m *MockAuthenticator) GetUserInfoFromToken(httpClient *http.Client, APIServer string, token string) (map[string]string, []string) {
return map[string]string{"username": "tokenuser", "password": "tokenpass"}, []string{"group3", "group4"}
func (m *MockAuthenticator) GetUserInfoFromToken(httpClient *http.Client, APIServer string, token string) (map[string]string, []string, error) {
return map[string]string{"username": "tokenuser", "password": "tokenpass"}, []string{"group3", "group4"}, nil
}

func TestAuthenticate(t *testing.T) {
Expand Down
88 changes: 54 additions & 34 deletions datasetIngestor/checkMetadata_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package datasetIngestor

import (
"fmt"
"net/http"
"net/http/httptest"
"reflect"
"strings"
"testing"
"time"
)

func TestGetHost(t *testing.T) {
Expand All @@ -25,24 +26,32 @@ func TestGetHost(t *testing.T) {

func TestCheckMetadata(t *testing.T) {
// Define mock parameters for the function
var TEST_API_SERVER string = "https://dacat-qa.psi.ch/api/v3" // TODO: Test Improvement. Change this to a mock server. At the moment, tests will fail if we change this to a mock server.
var APIServer = TEST_API_SERVER
var metadatafile1 = "testdata/metadata.json"
var metadatafile2 = "testdata/metadata-short.json"

// Mock HTTP client
client := &http.Client{
Timeout: 5 * time.Second, // Set a timeout for requests
Transport: &http.Transport{
// Customize the transport settings if needed (e.g., proxy, TLS config)
// For a dummy client, default settings are usually sufficient
},
CheckRedirect: func(req *http.Request, via []*http.Request) error {
// Customize how redirects are handled if needed
// For a dummy client, default behavior is usually sufficient
return http.ErrUseLastResponse // Use the last response for redirects
},
}
var numRequests uint = 0
// Create a mock HTTP server
server := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
// Increment the request count for each request
numRequests++

// Check if the request method is POST
if req.Method != http.MethodPost {
t.Errorf("Expected POST request, got %s", req.Method)
}

// Check if the request URL is correct
expectedURL := "/datasets/isValid"
if req.URL.String() != expectedURL {
t.Errorf("Expected request to %s, got %s", expectedURL, req.URL.String())
}

rw.WriteHeader(http.StatusOK)
rw.Header().Set("Content-Type", "application/json")
fmt.Fprintf(rw, `{"valid":true}`)
}))
// Close the server when test finishes
defer server.Close()

// Mock user map
user := map[string]string{
Expand All @@ -54,7 +63,7 @@ func TestCheckMetadata(t *testing.T) {
accessGroups := []string{"group1", "group2"}

// Call the function with mock parameters
metaDataMap, sourceFolder, beamlineAccount, err := ReadAndCheckMetadata(client, APIServer, metadatafile1, user, accessGroups)
metaDataMap, sourceFolder, beamlineAccount, err := ReadAndCheckMetadata(server.Client(), server.URL, metadatafile1, user, accessGroups)
if err != nil {
t.Error("Error in CheckMetadata function: ", err)
}
Expand Down Expand Up @@ -101,7 +110,7 @@ func TestCheckMetadata(t *testing.T) {
}

// test with the second metadata file
metaDataMap2, sourceFolder2, beamlineAccount2, err := ReadAndCheckMetadata(client, APIServer, metadatafile2, user, accessGroups)
metaDataMap2, sourceFolder2, beamlineAccount2, err := ReadAndCheckMetadata(server.Client(), server.URL, metadatafile2, user, accessGroups)
if err != nil {
t.Error("Error in CheckMetadata function: ", err)
}
Expand All @@ -126,23 +135,34 @@ func TestCheckMetadata(t *testing.T) {

func TestCheckMetadata_CrashCase(t *testing.T) {
// Define mock parameters for the function
var TEST_API_SERVER string = "https://dacat-qa.psi.ch/api/v3" // TODO: Test Improvement. Change this to a mock server. At the moment, tests will fail if we change this to a mock server.
var APIServer = TEST_API_SERVER
var metadatafile3 = "testdata/metadata_illegal.json"

// Mock HTTP client
client := &http.Client{
Timeout: 5 * time.Second, // Set a timeout for requests
Transport: &http.Transport{
// Customize the transport settings if needed (e.g., proxy, TLS config)
// For a dummy client, default settings are usually sufficient
},
CheckRedirect: func(req *http.Request, via []*http.Request) error {
// Customize how redirects are handled if needed
// For a dummy client, default behavior is usually sufficient
return http.ErrUseLastResponse // Use the last response for redirects
},
}
var numRequests uint = 0
// Create a mock HTTP server
server := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
// Increment the request count for each request
numRequests++

// Check if the request method is POST
if req.Method != http.MethodPost {
t.Errorf("Expected POST request, got %s", req.Method)
}

// Check if the request URL is correct
expectedURL := "/datasets/isValid"
if req.URL.String() != expectedURL {
t.Errorf("Expected request to %s, got %s", expectedURL, req.URL.String())
}

rw.WriteHeader(http.StatusOK)
rw.Header().Set("Content-Type", "application/json")
fmt.Fprintf(rw, `{"valid":false}`)
}))
// Close the server when test finishes
defer server.Close()

// Create a mock HTTP client
client := server.Client()

// Mock user map
user := map[string]string{
Expand All @@ -154,7 +174,7 @@ func TestCheckMetadata_CrashCase(t *testing.T) {
accessGroups := []string{"group1", "group2"}

// Call the function that should return an error
_, _, _, err := ReadAndCheckMetadata(client, APIServer, metadatafile3, user, accessGroups)
_, _, _, err := ReadAndCheckMetadata(client, server.URL, metadatafile3, user, accessGroups)

// Check that the function returned the expected error
if err == nil {
Expand Down
42 changes: 3 additions & 39 deletions datasetIngestor/ingestDataset_test.go
minottic marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
package datasetIngestor

import (
"bytes"
"io"
"log"
"net/http"
"net/http/httptest"
"os"
"strings"
"testing"
"time"
Expand Down Expand Up @@ -76,7 +73,7 @@ func TestSendIngestCommand(t *testing.T) {
// Create a mock server
server := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
// Respond with a fixed dataset ID when a new dataset is created
if strings.HasPrefix(req.URL.Path, "/RawDatasets") || strings.HasPrefix(req.URL.Path, "/DerivedDatasets") || strings.HasPrefix(req.URL.Path, "/Datasets") {
if strings.HasPrefix(req.URL.Path, "/datasets") {
rw.Write([]byte(`{"pid": "test-dataset-id"}`))
} else {
// Respond with a 200 status code when a new data block is created
Expand All @@ -96,39 +93,6 @@ func TestSendIngestCommand(t *testing.T) {
}
}

func TestGetEndpoint(t *testing.T) {
// Redirect log output to a buffer
var buf bytes.Buffer
log.SetOutput(&buf)
defer func() {
log.SetOutput(os.Stderr)
}()

testCases := []struct {
dsType string
want string
}{
{"raw", "/RawDatasets"},
{"derived", "/DerivedDatasets"},
{"base", "/Datasets"},
{"unknown", ""},
}

for _, tc := range testCases {
got, err := getEndpoint(tc.dsType)
if err != nil && tc.dsType != "unknown" {
t.Errorf("getEndpoint(%q) returned unexpected error: %v", tc.dsType, err)
}
if got != tc.want {
t.Errorf("getEndpoint(%q) = %q; want %q", tc.dsType, got, tc.want)
}
if tc.dsType == "unknown" && err == nil {
t.Errorf("Expected error for unknown dataset type not found")
}
buf.Reset()
}
}

func TestSendRequest(t *testing.T) {
// Create a test server
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
Expand All @@ -140,7 +104,7 @@ func TestSendRequest(t *testing.T) {
client := &http.Client{}

// Call the sendRequest function
resp, err := sendRequest(client, "GET", ts.URL, nil)
resp, err := sendRequest(client, "GET", ts.URL, "", nil)
if err != nil {
t.Errorf("received unexpected error: %v", err)
}
Expand Down Expand Up @@ -209,7 +173,7 @@ func TestCreateOrigDatablocks(t *testing.T) {
}

// Check if the request URL is correct
expectedURL := "/OrigDatablocks?access_token=testToken"
expectedURL := "/origdatablocks"
if req.URL.String() != expectedURL {
t.Errorf("Expected request to %s, got %s", expectedURL, req.URL.String())
}
Expand Down