Skip to content

Commit

Permalink
passing tests, but still confused
Browse files Browse the repository at this point in the history
  • Loading branch information
johnswanson committed Jul 24, 2018
1 parent fa8041b commit 8140754
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 43 deletions.
31 changes: 16 additions & 15 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ import (
"io/ioutil"
"strings"

"fmt"
"github.com/CircleCI-Public/circleci-cli/client"
"github.com/CircleCI-Public/circleci-cli/logger"
"github.com/pkg/errors"
"github.com/spf13/viper"
"fmt"
)

// GQLResponseErrors is a slice of errors returned by the GraphQL server. Each
Expand Down Expand Up @@ -47,7 +47,7 @@ type PublishOrbResponse struct {
type CreateNamespaceResponse struct {
Namespace struct {
CreatedAt string
Id string
Id string
}

GQLResponseErrors
Expand Down Expand Up @@ -196,7 +196,7 @@ func createNamespaceById(ctx context.Context, logger *logger.Logger, name string
}
errors {
message
type
type
}
}
}`
Expand All @@ -216,7 +216,7 @@ func createNamespaceById(ctx context.Context, logger *logger.Logger, name string
return &response.CreateNamespace.CreateNamespaceResponse, err
}

func GetOrganization(ctx context.Context, logger *logger.Logger, organizationName string, organizationVcs string) (string, error){
func GetOrganization(ctx context.Context, logger *logger.Logger, organizationName string, organizationVcs string) (string, error) {
var response struct {
Organization struct {
Id string
Expand Down Expand Up @@ -251,17 +251,18 @@ func GetOrganization(ctx context.Context, logger *logger.Logger, organizationNam
return response.Organization.Id, err
}

func CreateNamespace(ctx context.Context, logger *logger.Logger, name string, organizationName string, organizationVcs string) (*CreateNamespaceResponse, error){
organizationId, err := GetOrganization(ctx, logger, organizationName, organizationVcs)
if err != nil {
return nil, err
}
func CreateNamespace(ctx context.Context, logger *logger.Logger, name string, organizationName string, organizationVcs string) (*CreateNamespaceResponse, error) {
organizationId, err := GetOrganization(ctx, logger, organizationName, organizationVcs)
if err != nil {
return nil, err
}
fmt.Println(organizationId)

namespace, err := createNamespaceById(ctx, logger, name, organizationId)
namespace, err := createNamespaceById(ctx, logger, name, organizationId)

if err != nil {
return nil, err
}
if err != nil {
return nil, err
}

return namespace, err
}
return namespace, err
}
5 changes: 1 addition & 4 deletions cmd/cmd_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,8 @@ type MockRequestResponse struct {
// Test helpers

func appendPostHandler(server *ghttp.Server, authToken string, combineHandlers ...MockRequestResponse) {
var handlers []http.HandlerFunc

for _, handler := range combineHandlers {
handlers = append(handlers,
server.AppendHandlers(
ghttp.CombineHandlers(
ghttp.VerifyRequest("POST", "/"),
ghttp.VerifyHeader(http.Header{
Expand All @@ -62,7 +60,6 @@ func appendPostHandler(server *ghttp.Server, authToken string, combineHandlers .
),
)
}
server.AppendHandlers(handlers...)
}

type tmpFile struct {
Expand Down
85 changes: 61 additions & 24 deletions cmd/orb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,13 @@ var _ = Describe("Orb integration tests", func() {
}
}`

expectedOrganizationRequest := `{}`
expectedOrganizationRequest := `{
"query": "\n\t\t\tquery($organizationName: String!, $organizationVcs: VCSType!) {\n\t\t\t\torganization(\n\t\t\t\t\tname: $organizationName\n\t\t\t\t\tvcsType: $organizationVcs\n\t\t\t\t) {\n\t\t\t\t\tid\n\t\t\t\t}\n\t\t\t}",
"variables": {
"organizationName": "test-org",
"organizationVcs": "BITBUCKET"
}
}`

gqlNsResponse := `{
"createNamespace": {
Expand All @@ -331,22 +337,31 @@ var _ = Describe("Orb integration tests", func() {
}`

expectedNsRequest := `{
"query": "\n\t\t\tmutation($name: String!, $organizationId: UUID!) {\n\t\t\t\tcreateNamespace(\n\t\t\t\t\tname: $name,\n\t\t\t\t\torganizationId: $organizationId\n\t\t\t\t) {\n\t\t\t\t\tnamespace {\n\t\t\t\t\t\tcreatedAt\n\t\t\t\t\t\tid\n\t\t\t\t\t}\n\t\t\t\t\terrors {\n\t\t\t\t\t\tmessage\n\t\t\t\t\t\ttype\t\t\t\t\t\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}",
"variables": {
"name": "foo-ns",
"organizationId": "70cb2cb6-f960-4135-84a9-db88221c6573"
}
}`

appendPostHandler(testServer, token, MockRequestResponse{
Status: http.StatusOK,
Request: expectedNsRequest,
Response: gqlNsResponse,
}, MockRequestResponse{
Status: http.StatusOK,
Request: expectedOrganizationRequest,
Response: gqlOrganizationResponse,
})
"query": "\n\t\t\tmutation($name: String!, $organizationId: UUID!) {\n\t\t\t\tcreateNamespace(\n\t\t\t\t\tname: $name,\n\t\t\t\t\torganizationId: $organizationId\n\t\t\t\t) {\n\t\t\t\t\tnamespace {\n\t\t\t\t\t\tcreatedAt\n\t\t\t\t\t\tid\n\t\t\t\t\t}\n\t\t\t\t\terrors {\n\t\t\t\t\t\tmessage\n\t\t\t\t\t\ttype\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}",
"variables": {
"name": "foo-ns",
"organizationId": "bb604b45-b6b0-4b81-ad80-796f15eddf87"
}
}`

combineHandlers := []MockRequestResponse{
{
Status: http.StatusOK,
Request: expectedOrganizationRequest,
Response: gqlOrganizationResponse,
},
{
Status: http.StatusOK,
Request: expectedNsRequest,
Response: gqlNsResponse,
},
}
// TODO: figure out WTF this works
for _, handler := range combineHandlers {
appendPostHandler(testServer, token, handler)
}
// TODO: ... and this does not:
// appendPostHandler(testServer, token, combineHandlers...)

By("running the command")
session, err := gexec.Start(command, GinkgoWriter, GinkgoWriter)
Expand All @@ -359,6 +374,21 @@ var _ = Describe("Orb integration tests", func() {
It("prints all errors returned by the GraphQL API", func() {
By("setting up a mock server")

gqlOrganizationResponse := `{
"organization": {
"name": "test-org",
"id": "bb604b45-b6b0-4b81-ad80-796f15eddf87"
}
}`

expectedOrganizationRequest := `{
"query": "\n\t\t\tquery($organizationName: String!, $organizationVcs: VCSType!) {\n\t\t\t\torganization(\n\t\t\t\t\tname: $organizationName\n\t\t\t\t\tvcsType: $organizationVcs\n\t\t\t\t) {\n\t\t\t\t\tid\n\t\t\t\t}\n\t\t\t}",
"variables": {
"organizationName": "test-org",
"organizationVcs": "BITBUCKET"
}
}`

gqlResponse := `{
"createNamespace": {
"errors": [
Expand All @@ -370,18 +400,25 @@ var _ = Describe("Orb integration tests", func() {
}`

expectedRequestJson := `{
"query": "\n\t\t\tmutation($name: String!, $organizationId: UUID!) {\n\t\t\t\tcreateNamespace(\n\t\t\t\t\tname: $name,\n\t\t\t\t\torganizationId: $organizationId\n\t\t\t\t) {\n\t\t\t\t\tnamespace {\n\t\t\t\t\t\tcreatedAt\n\t\t\t\t\t\tid\n\t\t\t\t\t}\n\t\t\t\t\terrors {\n\t\t\t\t\t\tmessage\n\t\t\t\t\t\ttype\t\t\t\t\t\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}",
"query": "\n\t\t\tmutation($name: String!, $organizationId: UUID!) {\n\t\t\t\tcreateNamespace(\n\t\t\t\t\tname: $name,\n\t\t\t\t\torganizationId: $organizationId\n\t\t\t\t) {\n\t\t\t\t\tnamespace {\n\t\t\t\t\t\tcreatedAt\n\t\t\t\t\t\tid\n\t\t\t\t\t}\n\t\t\t\t\terrors {\n\t\t\t\t\t\tmessage\n\t\t\t\t\t\ttype\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}",
"variables": {
"name": "foo-ns",
"organizationId": "70cb2cb6-f960-4135-84a9-db88221c6573"
"organizationId": "bb604b45-b6b0-4b81-ad80-796f15eddf87"
}
}`

appendPostHandler(testServer, token, MockRequestResponse{
Status: http.StatusOK,
Request: expectedRequestJson,
Response: gqlResponse,
})
appendPostHandler(testServer, token,
MockRequestResponse{
Status: http.StatusOK,
Request: expectedOrganizationRequest,
Response: gqlOrganizationResponse,
})
appendPostHandler(testServer, token,
MockRequestResponse{
Status: http.StatusOK,
Request: expectedRequestJson,
Response: gqlResponse,
})

By("running the command")
session, err := gexec.Start(command, GinkgoWriter, GinkgoWriter)
Expand Down

0 comments on commit 8140754

Please sign in to comment.