diff --git a/command/report/client.go b/command/report/client.go index 1671f602..f01285e6 100644 --- a/command/report/client.go +++ b/command/report/client.go @@ -3,6 +3,7 @@ package report import ( "context" "crypto/tls" + "fmt" "net/http" "time" @@ -67,20 +68,20 @@ func (c *Client) CompressionEnabled() (bool, error) { return false, nil } -// const createArtifactMutationNew = `mutation($input: CreateArtifactInput!) { -// createArtifact(input: $input) { -// ok -// message -// error -// } -// }` - -// const createArtifactMutationOld = `mutation($input: CreateArtifactInput!) { -// createArtifact(input: $input) { -// ok -// error -// } -// }` +const createArtifactMutationNew = `mutation($input: CreateArtifactInput!) { + createArtifact(input: $input) { + ok + message + error + } +}` + +const createArtifactMutationOld = `mutation($input: CreateArtifactInput!) { + createArtifact(input: $input) { + ok + error + } +}` type CreateArtifactResponse struct { CreateArtifact CreateArtifact `json:"createArtifact"` @@ -105,7 +106,7 @@ type CreateArtifactInput struct { } func (c *Client) SendReportNew(input *CreateArtifactInput) (*CreateArtifactResponse, error) { - req := graphql.NewRequest(reportGraphqlQuery) + req := graphql.NewRequest(createArtifactMutationNew) req.Var("input", input) resp := CreateArtifactResponse{} @@ -114,12 +115,12 @@ func (c *Client) SendReportNew(input *CreateArtifactInput) (*CreateArtifactRespo if err != nil { return nil, err } - + fmt.Println(resp) return &resp, nil } func (c *Client) SendReportOld(input *CreateArtifactInput) (*CreateArtifactResponse, error) { - req := graphql.NewRequest(reportGraphqlQueryOld) + req := graphql.NewRequest(createArtifactMutationOld) req.Var("input", input) resp := CreateArtifactResponse{} diff --git a/command/report/client_test.go b/command/report/client_test.go index 6ab84fe8..771d0b4d 100644 --- a/command/report/client_test.go +++ b/command/report/client_test.go @@ -1,9 +1,15 @@ package report import ( + "encoding/json" + "io" "net/http" "net/http/httptest" + "os" "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) func TestClient_CompressionEnabled(t *testing.T) { @@ -60,3 +66,165 @@ func TestClient_CompressionEnabled(t *testing.T) { }) } } + +type artifactRequest struct { + Query string `json:"query"` + Variables struct { + Input *CreateArtifactInput `json:"input"` + } +} + +type artifactResponse struct { + Data struct { + CreateArtifact CreateArtifact `json:"createArtifact"` + } `json:"data"` +} + +func TestClient_SendReportNew(t *testing.T) { + data, err := os.ReadFile("./tests/golden_files/report_graphql_request_body.json") + require.NoError(t, err, "failed to read test data") + + request := artifactRequest{} + err = json.Unmarshal(data, &request) + require.NoError(t, err, "failed to unmarshal test data") + + input := &CreateArtifactInput{ + AccessToken: request.Variables.Input.AccessToken, + CommitOID: request.Variables.Input.CommitOID, + ReporterName: request.Variables.Input.ReporterName, + ReporterVersion: request.Variables.Input.ReporterVersion, + Data: request.Variables.Input.Data, + AnalyzerShortcode: request.Variables.Input.AnalyzerShortcode, + AnalyzerType: request.Variables.Input.AnalyzerType, + Metadata: request.Variables.Input.Metadata, + } + + t.Run("server success", func(t *testing.T) { + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + body, err := io.ReadAll(r.Body) + require.NoError(t, err, "failed to read request body") + + payload := artifactRequest{} + err = json.Unmarshal(body, &payload) + require.NoError(t, err, "failed to unmarshal request body") + + assert.Equal(t, createArtifactMutationNew, payload.Query) + + response := artifactResponse{ + Data: struct { + CreateArtifact CreateArtifact `json:"createArtifact"` + }{ + CreateArtifact: CreateArtifact{ + Ok: true, + }, + }, + } + + body, err = json.Marshal(response) + require.NoError(t, err, "failed to marshal response") + w.WriteHeader(http.StatusOK) + w.Header().Set("Content-Type", "application/json") + if _, err := w.Write(body); err != nil { + t.Error(err) + } + })) + + client := NewGraphQLClient(server.URL, false) + response, err := client.SendReportNew(input) + + require.NoError(t, err, "failed to send report") + assert.True(t, response.CreateArtifact.Ok, "report failed") + }) + + t.Run("server error", func(t *testing.T) { + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + require.NoError(t, err, "failed to marshal response") + w.WriteHeader(http.StatusInternalServerError) + + })) + + client := NewGraphQLClient(server.URL, false) + response, err := client.SendReportNew(input) + + require.Error(t, err, "expected error") + require.Nil(t, response, "expected nil response") + }) + +} + +func TestClient_SendReportOld(t *testing.T) { + data, err := os.ReadFile("./tests/golden_files/report_graphql_request_body.json") + require.NoError(t, err, "failed to read test data") + + request := artifactRequest{} + err = json.Unmarshal(data, &request) + require.NoError(t, err, "failed to unmarshal test data") + + input := &CreateArtifactInput{ + AccessToken: request.Variables.Input.AccessToken, + CommitOID: request.Variables.Input.CommitOID, + ReporterName: request.Variables.Input.ReporterName, + ReporterVersion: request.Variables.Input.ReporterVersion, + Data: request.Variables.Input.Data, + AnalyzerShortcode: request.Variables.Input.AnalyzerShortcode, + AnalyzerType: request.Variables.Input.AnalyzerType, + Metadata: request.Variables.Input.Metadata, + } + + t.Run("server success", func(t *testing.T) { + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + body, err := io.ReadAll(r.Body) + require.NoError(t, err, "failed to read request body") + + payload := artifactRequest{} + err = json.Unmarshal(body, &payload) + require.NoError(t, err, "failed to unmarshal request body") + + assert.Equal(t, createArtifactMutationOld, payload.Query) + + response := artifactResponse{ + Data: struct { + CreateArtifact CreateArtifact `json:"createArtifact"` + }{ + CreateArtifact: CreateArtifact{ + Ok: true, + Message: "test-message", + Error: "test-error", + }, + }, + } + + body, err = json.Marshal(response) + require.NoError(t, err, "failed to marshal response") + + w.WriteHeader(http.StatusOK) + w.Header().Set("Content-Type", "application/json") + if _, err := w.Write(body); err != nil { + t.Error(err) + } + })) + + client := NewGraphQLClient(server.URL, false) + response, err := client.SendReportOld(input) + + require.NoError(t, err, "failed to send report") + assert.True(t, response.CreateArtifact.Ok, "report failed") + assert.Equal(t, "test-message", response.CreateArtifact.Message, "unexpected message") + assert.Equal(t, "test-error", response.CreateArtifact.Error, "unexpected error") + }) + + t.Run("server error", func(t *testing.T) { + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + require.NoError(t, err, "failed to marshal response") + w.WriteHeader(http.StatusInternalServerError) + + })) + + client := NewGraphQLClient(server.URL, false) + response, err := client.SendReportOld(input) + + require.Error(t, err, "expected error") + require.Nil(t, response, "expected nil response") + }) + +}