Skip to content

Commit

Permalink
test: Fix CI tests
Browse files Browse the repository at this point in the history
  • Loading branch information
JulesFaucherre committed May 10, 2023
1 parent b55e3de commit fdd5f69
Show file tree
Hide file tree
Showing 2 changed files with 166 additions and 45 deletions.
44 changes: 23 additions & 21 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -599,20 +599,20 @@ type OrbIntrospectionResponse struct {
func orbQueryHandleOwnerId(cl *graphql.Client) bool {
query := `
query ValidateOrb {
__schema {
queryType {
fields(includeDeprecated: true) {
__schema {
queryType {
fields(includeDeprecated: true) {
name
args {
name
__typename
type {
name
args {
name
__typename
type {
name
}
}
}
}
}
}
}
}`
request := graphql.NewRequest(query)
response := OrbIntrospectionResponse{}
Expand All @@ -621,6 +621,8 @@ query ValidateOrb {
return false
}

request.SetToken(cl.Token)

// Find the orbConfig query method, look at its arguments, if it has the "ownerId" argument, return true
for _, field := range response.Schema.Query.Fields {
if field.Name == "orbConfig" {
Expand Down Expand Up @@ -1323,18 +1325,18 @@ func OrbSetOrbListStatus(cl *graphql.Client, namespace string, orb string, list
var response OrbSetOrbListStatusResponse

query := `
mutation($orbId: UUID!, $list: Boolean!) {
setOrbListStatus(
orbId: $orbId,
list: $list
) {
listed
errors {
message
type
}
}
mutation($orbId: UUID!, $list: Boolean!) {
setOrbListStatus(
orbId: $orbId,
list: $list
) {
listed
errors {
message
type
}
}
}
`

request := graphql.NewRequest(query)
Expand Down
167 changes: 143 additions & 24 deletions cmd/orb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,8 @@ See a full explanation and documentation on orbs here: https://circleci.com/docs
It("works", func() {
By("setting up a mock server")

mockOrbIntrospection(true, "", tempSettings)

gqlResponse := `{
"orbConfig": {
"sourceYaml": "{}",
Expand Down Expand Up @@ -145,6 +147,7 @@ See a full explanation and documentation on orbs here: https://circleci.com/docs
Status: http.StatusOK,
Request: string(expected),
Response: gqlResponse})

session, err := gexec.Start(command, GinkgoWriter, GinkgoWriter)

Expect(err).ShouldNot(HaveOccurred())
Expand Down Expand Up @@ -175,6 +178,8 @@ See a full explanation and documentation on orbs here: https://circleci.com/docs
By("setting up a mock server")
orb.Write([]byte(`{}`))

mockOrbIntrospection(true, "", tempSettings)

gqlResponse := `{
"orbConfig": {
"sourceYaml": "{}",
Expand Down Expand Up @@ -222,6 +227,9 @@ See a full explanation and documentation on orbs here: https://circleci.com/docs

It("works", func() {
By("setting up a mock server")

mockOrbIntrospection(true, "", tempSettings)

gqlResponse := `{
"orbConfig": {
"sourceYaml": "{}",
Expand Down Expand Up @@ -255,6 +263,8 @@ See a full explanation and documentation on orbs here: https://circleci.com/docs
It("prints errors if invalid", func() {
By("setting up a mock server")

mockOrbIntrospection(true, "", tempSettings)

gqlResponse := `{
"orbConfig": {
"sourceYaml": "hello world",
Expand Down Expand Up @@ -300,6 +310,8 @@ See a full explanation and documentation on orbs here: https://circleci.com/docs
It("works", func() {
By("setting up a mock server")

mockOrbIntrospection(true, "", tempSettings)

gqlResponse := `{
"orbConfig": {
"outputYaml": "hello world",
Expand Down Expand Up @@ -332,6 +344,8 @@ See a full explanation and documentation on orbs here: https://circleci.com/docs
It("prints errors if invalid", func() {
By("setting up a mock server")

mockOrbIntrospection(true, "", tempSettings)

gqlResponse := `{
"orbConfig": {
"outputYaml": "hello world",
Expand Down Expand Up @@ -1469,13 +1483,29 @@ You can now register versions of %s using %s.`,
}
}`, list)

expectedOrbRequest := fmt.Sprintf(`{
"query": "\n\t\tmutation($orbId: UUID!, $list: Boolean!) {\n\t\t\tsetOrbListStatus(\n\t\t\t\torbId: $orbId,\n\t\t\t\tlist: $list\n\t\t\t) {\n\t\t\t\tlisted\n\t\t\t\terrors { \n\t\t\t\t\tmessage\n\t\t\t\t\ttype \n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t",
"variables": {
"list": %t,
"orbId": "bb604b45-b6b0-4b81-ad80-796f15eddf87"
}
}`, list)
orbRequest := map[string]interface{}{
"query": `
mutation($orbId: UUID!, $list: Boolean!) {
setOrbListStatus(
orbId: $orbId,
list: $list
) {
listed
errors {
message
type
}
}
}
`,
"variables": map[string]interface{}{
"list": list,
"orbId": "bb604b45-b6b0-4b81-ad80-796f15eddf87",
},
}

expectedOrbRequest, err := json.Marshal(orbRequest)
Expect(err).ToNot(HaveOccurred())

tempSettings.AppendPostHandler(token, clitest.MockRequestResponse{
Status: http.StatusOK,
Expand All @@ -1484,7 +1514,7 @@ You can now register versions of %s using %s.`,

tempSettings.AppendPostHandler(token, clitest.MockRequestResponse{
Status: http.StatusOK,
Request: expectedOrbRequest,
Request: string(expectedOrbRequest),
Response: gqlOrbResponse})

session, err := gexec.Start(command, GinkgoWriter, GinkgoWriter)
Expand Down Expand Up @@ -1537,13 +1567,29 @@ You can now register versions of %s using %s.`,
}
}`

expectedOrbRequest := fmt.Sprintf(`{
"query": "\n\t\tmutation($orbId: UUID!, $list: Boolean!) {\n\t\t\tsetOrbListStatus(\n\t\t\t\torbId: $orbId,\n\t\t\t\tlist: $list\n\t\t\t) {\n\t\t\t\tlisted\n\t\t\t\terrors { \n\t\t\t\t\tmessage\n\t\t\t\t\ttype \n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t",
"variables": {
"list": %t,
"orbId": "bb604b45-b6b0-4b81-ad80-796f15eddf87"
}
}`, list)
orbRequest := map[string]interface{}{
"query": `
mutation($orbId: UUID!, $list: Boolean!) {
setOrbListStatus(
orbId: $orbId,
list: $list
) {
listed
errors {
message
type
}
}
}
`,
"variables": map[string]interface{}{
"list": list,
"orbId": "bb604b45-b6b0-4b81-ad80-796f15eddf87",
},
}

expectedOrbRequest, err := json.Marshal(orbRequest)
Expect(err).ToNot(HaveOccurred())

tempSettings.AppendPostHandler(token, clitest.MockRequestResponse{
Status: http.StatusOK,
Expand All @@ -1552,7 +1598,7 @@ You can now register versions of %s using %s.`,

tempSettings.AppendPostHandler(token, clitest.MockRequestResponse{
Status: http.StatusOK,
Request: expectedOrbRequest,
Request: string(expectedOrbRequest),
Response: gqlOrbResponse})

session, err := gexec.Start(command, GinkgoWriter, GinkgoWriter)
Expand Down Expand Up @@ -1686,13 +1732,29 @@ You can now register versions of %s using %s.`,
}
}`

expectedOrbRequest := fmt.Sprintf(`{
"query": "\n\t\tmutation($orbId: UUID!, $list: Boolean!) {\n\t\t\tsetOrbListStatus(\n\t\t\t\torbId: $orbId,\n\t\t\t\tlist: $list\n\t\t\t) {\n\t\t\t\tlisted\n\t\t\t\terrors { \n\t\t\t\t\tmessage\n\t\t\t\t\ttype \n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t",
"variables": {
"list": %t,
"orbId": "bb604b45-b6b0-4b81-ad80-796f15eddf87"
}
}`, list)
orbRequest := map[string]interface{}{
"query": `
mutation($orbId: UUID!, $list: Boolean!) {
setOrbListStatus(
orbId: $orbId,
list: $list
) {
listed
errors {
message
type
}
}
}
`,
"variables": map[string]interface{}{
"list": list,
"orbId": "bb604b45-b6b0-4b81-ad80-796f15eddf87",
},
}

expectedOrbRequest, err := json.Marshal(orbRequest)
Expect(err).ToNot(HaveOccurred())

tempSettings.AppendPostHandler(token, clitest.MockRequestResponse{
Status: http.StatusOK,
Expand All @@ -1701,7 +1763,7 @@ You can now register versions of %s using %s.`,

tempSettings.AppendPostHandler(token, clitest.MockRequestResponse{
Status: http.StatusOK,
Request: expectedOrbRequest,
Request: string(expectedOrbRequest),
Response: gqlOrbResponse})

session, err := gexec.Start(command, GinkgoWriter, GinkgoWriter)
Expand Down Expand Up @@ -3453,3 +3515,60 @@ func mockOrbSource(source, orbVersion, token string, tempSettings *clitest.TempS
Response: response,
})
}

func mockOrbIntrospection(isValid bool, token string, tempSettings *clitest.TempSettings) {
args := []map[string]interface{}{
{
"name": "orbYaml",
},
}
if isValid {
args = append(args, map[string]interface{}{
"name": "ownerId",
})
}

responseStruct := map[string]interface{}{
"__schema": map[string]interface{}{
"queryType": map[string]interface{}{
"fields": []map[string]interface{}{
{
"name": "orbConfig",
"args": args,
},
},
},
},
}
response, err := json.Marshal(responseStruct)
Expect(err).ToNot(HaveOccurred())

requestStruct := map[string]interface{}{
"query": `
query ValidateOrb {
__schema {
queryType {
fields(includeDeprecated: true) {
name
args {
name
__typename
type {
name
}
}
}
}
}
}`,
"variables": map[string]interface{}{},
}
request, err := json.Marshal(requestStruct)
Expect(err).ToNot(HaveOccurred())

tempSettings.AppendPostHandler(token, clitest.MockRequestResponse{
Status: http.StatusOK,
Request: string(request),
Response: string(response),
})
}

0 comments on commit fdd5f69

Please sign in to comment.