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

Test and refactor GetProposal #68

Merged
merged 3 commits into from
May 22, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
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
5 changes: 4 additions & 1 deletion cmd/datasetGetProposal/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,10 @@ func main() {
}

user, accessGroups := datasetUtils.Authenticate(client, APIServer, token, userpass)
proposal := datasetUtils.GetProposal(client, APIServer, ownerGroup, user, accessGroups)
proposal, err := datasetUtils.GetProposal(client, APIServer, ownerGroup, user, accessGroups)
if err != nil {
log.Fatalf("Error: %v\n", err)
}
// proposal is of type map[string]interface{}

if len(proposal) > 0 {
Expand Down
5 changes: 4 additions & 1 deletion datasetIngestor/checkMetadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,10 @@ func augmentMissingMetadata(user map[string]string, metaDataMap map[string]inter
if !ok {
return fmt.Errorf("ownerGroup is not a string")
}
proposal := datasetUtils.GetProposal(client, APIServer, ownerGroup, user, accessGroups)
proposal, err := datasetUtils.GetProposal(client, APIServer, ownerGroup, user, accessGroups)
if err != nil {
log.Fatalf("Error: %v\n", err)
}
if val, ok := proposal["pi_email"]; ok {
piEmail, ok := val.(string)
if !ok {
Expand Down
69 changes: 37 additions & 32 deletions datasetUtils/getProposal.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,57 +2,62 @@ package datasetUtils

import (
"encoding/json"
"io/ioutil"
"log"
"io"
"fmt"
"net/http"
)

func GetProposal(client *http.Client, APIServer string, ownerGroup string, user map[string]string,
accessGroups []string) (proposal map[string]interface{}) {
// Check if ownerGroup is in accessGroups list. No longer needed, done on server side and
// takes also accessGroup for beamline accounts into account
/*
GetProposal retrieves a proposal from a given API server.

Parameters:
- client: An *http.Client object used to send the request.
- APIServer: A string representing the base URL of the API server.
- ownerGroup: A string representing the owner group of the proposal.
- user: A map containing user information, including an access token.
- accessGroups: A slice of strings representing the access groups of the user.

// if user["displayName"] != "ingestor" {
// validOwner := false
// for _, b := range accessGroups {
// if b == ownerGroup {
// validOwner = true
// break
// }
// }
// if validOwner {
// log.Printf("OwnerGroup information %s verified successfully.\n", ownerGroup)
// } else {
// log.Fatalf("You are not member of the ownerGroup %s which is needed to access this data", ownerGroup)
// }
// }
The function constructs a filter based on the ownerGroup, then sends a GET request to the API server with the filter and user's access token. The response is then parsed into a map and returned.

filter := `{"where":{"ownerGroup":"` + ownerGroup + `"}}`
url := APIServer + "/Proposals?access_token=" + user["accessToken"]
If the request or JSON unmarshalling fails, the function will log the error and terminate the program.

// fmt.Printf("=== resulting filter:%s\n", filter)
Returns:
- A map representing the proposal. If no proposal is found, an empty map is returned.
*/
func GetProposal(client *http.Client, APIServer string, ownerGroup string, user map[string]string,
accessGroups []string) (map[string]interface{}, error) {

filter := fmt.Sprintf(`{"where":{"ownerGroup":"%s"}}`, ownerGroup)
url := fmt.Sprintf("%s/Proposals?access_token=%s", APIServer, user["accessToken"])

req, err := http.NewRequest("GET", url, nil)
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", "application/json")
req.Header.Set("filter", filter)

kavir1698 marked this conversation as resolved.
Show resolved Hide resolved
resp, err := client.Do(req)
if err != nil {
log.Fatal(err)
return nil, err
}
defer resp.Body.Close()
body, _ := ioutil.ReadAll(resp.Body)

// fmt.Printf("response Object:\n%v\n", string(body))


body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}

var respObj []map[string]interface{}
err = json.Unmarshal(body, &respObj)
if err != nil {
log.Fatal(err)
return nil, err
}
// the first element contains the actual map

respMap := make(map[string]interface{})
if len(respObj) > 0 {
respMap = respObj[0]
}
return respMap

return respMap, nil
}
32 changes: 32 additions & 0 deletions datasetUtils/getProposal_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package datasetUtils

import (
"net/http"
"net/http/httptest"
"testing"
)

func TestGetProposal(t *testing.T) {
// Create a mock server
server := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
// Send response to be tested
rw.Write([]byte(`[{"proposal": "test proposal"}]`))
}))
// Close the server when test finishes
defer server.Close()

// Create a client
client := &http.Client{}

// Create a user
user := make(map[string]string)
user["accessToken"] = "testToken"

// Call GetProposal
proposal, _ := GetProposal(client, server.URL, "testOwnerGroup", user, []string{"testAccessGroup"})

// Check the proposal
if proposal["proposal"] != "test proposal" {
t.Errorf("Expected proposal 'test proposal', got '%s'", proposal["proposal"])
}
}
Loading