Skip to content

Commit

Permalink
Test and refactor GetProposal (#68)
Browse files Browse the repository at this point in the history
* Add a unit test for `GetProposal`

* Refactor GetProposal

Changes:
1. Instead of using log.Fatal which will terminate the program, the function now returns an error if something goes wrong.
q. Used fmt.Sprintf for string concatenation as this is more readable and efficient when concatenating multiple strings.
  • Loading branch information
kavir1698 authored May 22, 2024
1 parent 50f564f commit 3ccda9c
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 34 deletions.
6 changes: 5 additions & 1 deletion cmd/datasetGetProposal/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,11 @@ func main() {

auth := &datasetUtils.RealAuthenticator{}
user, accessGroups := datasetUtils.Authenticate(auth, 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)

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"])
}
}

0 comments on commit 3ccda9c

Please sign in to comment.