Skip to content

Commit

Permalink
Trying to get issue in e2e
Browse files Browse the repository at this point in the history
  • Loading branch information
michalkrzyz committed Oct 23, 2024
1 parent ca50f9c commit f4ab534
Show file tree
Hide file tree
Showing 5 changed files with 195 additions and 56 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# SPDX-FileCopyrightText: 2024 SAP SE or an SAP affiliate company and Greenhouse contributors
# SPDX-License-Identifier: Apache-2.0

query ($filter: IssueFilter, $first: Int, $after: String) {
Issues (
filter: $filter,
first: $first,
after: $after
) {
totalCount
edges {
node {
id
type
primaryName
description
lastModified
metadata {
created_at
created_by
deleted_at
updated_at
updated_by
}
}
cursor
}
}
}
104 changes: 104 additions & 0 deletions internal/e2e/common/issue.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
// SPDX-FileCopyrightText: 2024 SAP SE or an SAP affiliate company and Greenhouse contributors
// SPDX-License-Identifier: Apache-2.0

package e2e_common

import (
"context"
"fmt"
"os"

"github.com/cloudoperators/heureka/internal/api/graphql/graph/model"
util2 "github.com/cloudoperators/heureka/pkg/util"

"github.com/machinebox/graphql"
. "github.com/onsi/gomega"
"github.com/sirupsen/logrus"
)

type Issue struct {
PrimaryName string
Description string
Type string
}

func QueryCreateIssue(port string, issue Issue) *model.Issue {
client := graphql.NewClient(fmt.Sprintf("http://localhost:%s/query", port))

//@todo may need to make this more fault proof?! What if the test is executed from the root dir? does it still work?
b, err := os.ReadFile("../api/graphql/graph/queryCollection/issue/create.graphql")
Expect(err).To(BeNil())
str := string(b)
req := graphql.NewRequest(str)

req.Var("input", map[string]interface{}{
"primaryName": issue.PrimaryName,
"description": issue.Description,
"type": issue.Type,
})

req.Header.Set("Cache-Control", "no-cache")
ctx := context.Background()

var respData struct {
Issue model.Issue `json:"createIssue"`
}
if err := util2.RequestWithBackoff(func() error { return client.Run(ctx, req, &respData) }); err != nil {
logrus.WithError(err).WithField("request", req).Fatalln("Error while unmarshaling")
}
return &respData.Issue
}

func QueryUpdateIssue(port string, issue Issue, iid string) *model.Issue {
// create a queryCollection (safe to share across requests)
client := graphql.NewClient(fmt.Sprintf("http://localhost:%s/query", port))

//@todo may need to make this more fault proof?! What if the test is executed from the root dir? does it still work?
b, err := os.ReadFile("../api/graphql/graph/queryCollection/issue/update.graphql")
Expect(err).To(BeNil())
str := string(b)
req := graphql.NewRequest(str)

req.Var("id", iid)
req.Var("input", map[string]string{
"description": issue.Description,
"type": issue.Type,
})

req.Header.Set("Cache-Control", "no-cache")
ctx := context.Background()

var respData struct {
Issue model.Issue `json:"updateIssue"`
}
if err := util2.RequestWithBackoff(func() error { return client.Run(ctx, req, &respData) }); err != nil {
logrus.WithError(err).WithField("request", req).Fatalln("Error while unmarshaling")
}
return &respData.Issue
}

func QueryGetIssue(port string, issuePrimaryName string) *model.IssueConnection {
// create a queryCollection (safe to share across requests)
client := graphql.NewClient(fmt.Sprintf("http://localhost:%s/query", port))

//@todo may need to make this more fault proof?! What if the test is executed from the root dir? does it still work?
b, err := os.ReadFile("../api/graphql/graph/queryCollection/issue/listIssues.graphql")
Expect(err).To(BeNil())
str := string(b)
req := graphql.NewRequest(str)

req.Var("filter", map[string]string{"primaryName": issuePrimaryName})
req.Var("first", 1)
req.Var("after", "0")

req.Header.Set("Cache-Control", "no-cache")
ctx := context.Background()

var respData struct {
Issues model.IssueConnection `json:"Issues"`
}
if err := util2.RequestWithBackoff(func() error { return client.Run(ctx, req, &respData) }); err != nil {
logrus.WithError(err).WithField("request", req).Fatalln("Error while unmarshaling")
}
return &respData.Issues
}
16 changes: 8 additions & 8 deletions internal/e2e/common/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,9 @@ func ExpectNonSystemUserUniqueUserIds(v, expectedV []*string) {
}

type User struct {
Id string
Type entity.UserType
Name string
UniqueUserID string
Type entity.UserType
Name string
}

func QueryCreateUser(port string, user User) *model.User {
Expand All @@ -96,7 +96,7 @@ func QueryCreateUser(port string, user User) *model.User {
req := graphql.NewRequest(str)

req.Var("input", map[string]string{
"uniqueUserId": user.Id,
"uniqueUserId": user.UniqueUserID,
"type": entity.GetUserTypeString(user.Type),
"name": user.Name,
})
Expand All @@ -113,7 +113,7 @@ func QueryCreateUser(port string, user User) *model.User {
return &respData.User
}

func QueryUpdateUser(port string, user User) *model.User {
func QueryUpdateUser(port string, user User, uid string) *model.User {
// create a queryCollection (safe to share across requests)
client := graphql.NewClient(fmt.Sprintf("http://localhost:%s/query", port))

Expand All @@ -123,7 +123,7 @@ func QueryUpdateUser(port string, user User) *model.User {
str := string(b)
req := graphql.NewRequest(str)

req.Var("id", user.Id)
req.Var("id", uid)
req.Var("input", map[string]string{
"name": user.Name,
"type": entity.GetUserTypeString(user.Type),
Expand All @@ -141,7 +141,7 @@ func QueryUpdateUser(port string, user User) *model.User {
return &respData.User
}

func QueryGetUser(port string, userId string) *model.UserConnection {
func QueryGetUser(port string, uniqueUserId string) *model.UserConnection {
// create a queryCollection (safe to share across requests)
client := graphql.NewClient(fmt.Sprintf("http://localhost:%s/query", port))

Expand All @@ -151,7 +151,7 @@ func QueryGetUser(port string, userId string) *model.UserConnection {
str := string(b)
req := graphql.NewRequest(str)

req.Var("filter", map[string]string{"uniqueUserId": userId})
req.Var("filter", map[string]string{"uniqueUserId": uniqueUserId})
req.Var("first", 1)
req.Var("after", "0")

Expand Down
98 changes: 52 additions & 46 deletions internal/e2e/metadata_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@

package e2e_test

/*
import (
"fmt"
"time"

"github.com/cloudoperators/heureka/internal/api/graphql/graph/model"
Expand All @@ -19,34 +19,37 @@ import (
)

const (
testUniqueUserId = "1"
testUserType = entity.HumanUserType
testUpdatedUserType = entity.TechnicalUserType
testUserName = "Joe"
testUpdatedUserName = "Donald"
testCreatedBy = "Creator"
testUpdatedBy = "Updater"
dbDateLayout = "2006-01-02 15:04:05 -0700 MST"
testIssuePrimaryName = "PN-001"
testCreatedIssueDescription = "Created Issue"
testUpdatedIssueDescription = "Updated Issue"
dbDateLayout = "2006-01-02 15:04:05 -0700 MST"
)

func createTestUser(port string) {
user := e2e_common.QueryCreateUser(port, e2e_common.User{Id: testUniqueUserId, Type: testUserType, Name: testUserName})
Expect(*user.Name).To(Equal(testUserName))
Expect(*user.UniqueUserID).To(Equal(testUniqueUserId))
Expect(entity.UserType(user.Type)).To(Equal(testUserType))
}
var (
testCreatedIssueType = entity.IssueTypeVulnerability.String()
testUpdatedIssueType = entity.IssueTypePolicyViolation.String()
)

func updateTestUser(port string) {
user := e2e_common.QueryUpdateUser(port, e2e_common.User{Id: testUniqueUserId, Type: testUpdatedUserType, Name: testUpdatedUserName})
Expect(*user.Name).To(Equal(testUpdatedUserName))
Expect(*user.UniqueUserID).To(Equal(testUniqueUserId))
Expect(entity.UserType(user.Type)).To(Equal(testUpdatedUserType))
func createTestIssue(port string) string {
issue := e2e_common.QueryCreateIssue(port, e2e_common.Issue{PrimaryName: testIssuePrimaryName, Description: testCreatedIssueDescription, Type: testCreatedIssueType})
Expect(*issue.PrimaryName).To(Equal(testIssuePrimaryName))
Expect(*issue.Description).To(Equal(testCreatedIssueDescription))
Expect(issue.Type.String()).To(Equal(testCreatedIssueType))
fmt.Println("AAAAAAAAAAAAAAAAAA ", *issue)
return issue.ID
}
func updateTestIssue(port string, iid string) {
issue := e2e_common.QueryUpdateIssue(port, e2e_common.Issue{PrimaryName: testIssuePrimaryName, Description: testUpdatedIssueDescription, Type: testUpdatedIssueType}, iid)
Expect(*issue.PrimaryName).To(Equal(testIssuePrimaryName))
Expect(*issue.Description).To(Equal(testUpdatedIssueDescription))
Expect(issue.Type.String()).To(Equal(testUpdatedIssueType))
}

func getTestUser(port string) model.User {
users := e2e_common.QueryGetUser(port, testUniqueUserId)
Expect(users.TotalCount).To(Equal(1))
return *users.Edges[0].Node
func getTestIssue(port string) model.Issue {
issues := e2e_common.QueryGetIssue(port, testIssuePrimaryName)
Expect(issues.TotalCount).To(Equal(1))
fmt.Println("AAAAAAAAAAAAAAAAAA ", *issues.Edges[0].Node)
return *issues.Edges[0].Node
}

var _ = Describe("Creating and updating entity via API", Label("e2e", "Entities"), func() {
Expand All @@ -67,53 +70,56 @@ var _ = Describe("Creating and updating entity via API", Label("e2e", "Entities"
s.BlockingStop()
})

When("New user is created via API", func() {
var user model.User
When("New issue is created via API", func() {
var issue model.Issue
BeforeEach(func() {
createTestUser(cfg.Port)
user = getTestUser(cfg.Port)
createTestIssue(cfg.Port)
issue = getTestIssue(cfg.Port)
})
It("shall assign CreatedBy and CreatedAt metadata fields and shall keep nil in UpdatedBy, UpdatedAt and DeltedAt metadata fields", func() {
Expect(entity.UserType(user.Type)).To(Equal(testUserType))
Expect(user.Metadata).To(Not(BeNil()))
Expect(*user.Metadata.CreatedBy).To(Equal(testCreatedBy))
Expect(*issue.Description).To(Equal(testCreatedIssueDescription))
Expect(issue.Type.String()).To(Equal(testCreatedIssueType))

createdAt, err := time.Parse(dbDateLayout, *user.Metadata.CreatedAt)
Expect(issue.Metadata).To(Not(BeNil()))
Expect(*issue.Metadata.CreatedBy).To(Equal(e2e_common.SystemUserId))

createdAt, err := time.Parse(dbDateLayout, *issue.Metadata.CreatedAt)
Expect(err).Should(BeNil())
Expect(createdAt).Should(BeTemporally("~", time.Now().UTC(), 3*time.Second))

Expect(*user.Metadata.UpdatedBy).To(BeEmpty())
Expect(*issue.Metadata.UpdatedBy).To(BeEmpty())

updatedAt, err := time.Parse(dbDateLayout, *user.Metadata.UpdatedAt)
updatedAt, err := time.Parse(dbDateLayout, *issue.Metadata.UpdatedAt)
Expect(err).Should(BeNil())
Expect(updatedAt).To(Equal(createdAt))
})
})
When("User is updated via API", func() {
var user model.User
When("Issue is updated via API", func() {
var issue model.Issue
BeforeEach(func() {
createTestUser(cfg.Port)
iid := createTestIssue(cfg.Port)
time.Sleep(1100 * time.Millisecond)
updateTestUser(cfg.Port)
user = getTestUser(cfg.Port)
updateTestIssue(cfg.Port, iid)
issue = getTestIssue(cfg.Port)
})
It("shall assign UpdatedBy and UpdatedAt metadata fields and shall keep nil in DeletedAt metadata field", func() {
Expect(entity.UserType(user.Type)).To(Equal(testUpdatedUserType))
Expect(user.Metadata).To(Not(BeNil()))
Expect(*user.Metadata.CreatedBy).To(Equal(testCreatedBy))
Expect(*issue.Description).To(Equal(testUpdatedIssueDescription))
Expect(issue.Type.String()).To(Equal(testUpdatedIssueType))

Expect(issue.Metadata).To(Not(BeNil()))
Expect(*issue.Metadata.CreatedBy).To(Equal(e2e_common.SystemUserId))

createdAt, err := time.Parse(dbDateLayout, *user.Metadata.CreatedAt)
createdAt, err := time.Parse(dbDateLayout, *issue.Metadata.CreatedAt)
Expect(err).Should(BeNil())
Expect(createdAt).Should(BeTemporally("~", time.Now().UTC(), 3*time.Second))

Expect(*user.Metadata.UpdatedBy).To(Equal(testUpdatedBy))
Expect(*issue.Metadata.UpdatedBy).To(Equal(e2e_common.SystemUserId))

updatedAt, err := time.Parse(dbDateLayout, *user.Metadata.UpdatedAt)
updatedAt, err := time.Parse(dbDateLayout, *issue.Metadata.UpdatedAt)
Expect(err).Should(BeNil())
Expect(updatedAt).Should(BeTemporally("~", time.Now().UTC(), 2*time.Second))
Expect(updatedAt).Should(BeTemporally(">", createdAt))
})
})

})
*/
4 changes: 2 additions & 2 deletions internal/e2e/user_query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ var _ = Describe("Creating User via API", Label("e2e", "Users"), func() {

Context("and a mutation query is performed", Label("create.graphql"), func() {
It("creates new user", func() {
respUser := e2e_common.QueryCreateUser(cfg.Port, e2e_common.User{Id: user.UniqueUserID, Type: user.Type, Name: user.Name})
respUser := e2e_common.QueryCreateUser(cfg.Port, e2e_common.User{UniqueUserID: user.UniqueUserID, Type: user.Type, Name: user.Name})
Expect(*respUser.Name).To(Equal(user.Name))
Expect(*respUser.UniqueUserID).To(Equal(user.UniqueUserID))
Expect(entity.UserType(respUser.Type)).To(Equal(user.Type))
Expand Down Expand Up @@ -272,7 +272,7 @@ var _ = Describe("Updating User via API", Label("e2e", "Users"), func() {
It("updates user", func() {
user := seedCollection.UserRows[0].AsUser()
user.Name = "Sauron"
respUser := e2e_common.QueryUpdateUser(cfg.Port, e2e_common.User{Id: fmt.Sprintf("%d", user.Id), Name: user.Name, Type: user.Type})
respUser := e2e_common.QueryUpdateUser(cfg.Port, e2e_common.User{UniqueUserID: user.UniqueUserID, Name: user.Name, Type: user.Type}, fmt.Sprintf("%d", user.Id))
Expect(*respUser.Name).To(Equal(user.Name))
Expect(*respUser.UniqueUserID).To(Equal(user.UniqueUserID))
Expect(entity.UserType(respUser.Type)).To(Equal(user.Type))
Expand Down

0 comments on commit f4ab534

Please sign in to comment.