integration

package
v0.0.0-...-362a82f Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 8, 2023 License: GPL-3.0 Imports: 11 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var CreateVerifyDeleteKeys = newScenario(
	"CreateVerifyDeleteKeys",
	func(ctx context.Context, env Env) {
		createApiResponse := Step[map[string]any]{
			Name:   "Create API",
			Method: "POST",
			Url:    fmt.Sprintf("%s/v1/apis.createApi", env.BaseUrl),
			Header: map[string]string{
				"Content-Type":  "application/json",
				"Authorization": fmt.Sprintf("Bearer %s", env.RootKey),
			},
			Body: map[string]any{
				"name": "scenario-test-pls-delete",
			},
			Assertions: []assertion{
				assertStatus(200),
				assertBodyExists("apiId"),
				assertHeaderExists("Unkey-Trace-Id"),
			},
		}.Run(ctx, make(map[string]any))

		defer Step[map[string]any]{
			Name:   "Delete API",
			Method: "POST",
			Url:    fmt.Sprintf("%s/v1/apis.removeApi", env.BaseUrl),
			Header: map[string]string{
				"Content-Type":  "application/json",
				"Authorization": fmt.Sprintf("Bearer %s", env.RootKey),
			},
			Body: map[string]any{
				"apiId": createApiResponse["apiId"],
			},
			Assertions: []assertion{
				assertStatus(200),
				assertHeaderExists("Unkey-Trace-Id"),
			},
		}.Run(ctx, make(map[string]any))

		for i := 0; i < 5; i++ {
			keyResponse := Step[map[string]any]{
				Name:   "Create Key",
				Method: "POST",
				Url:    fmt.Sprintf("%s/v1/keys.createKey", env.BaseUrl),
				Header: map[string]string{
					"Content-Type":  "application/json",
					"Authorization": fmt.Sprintf("Bearer %s", env.RootKey),
				},
				Body: map[string]any{
					"apiId": createApiResponse["apiId"],
				},
				Assertions: []assertion{
					assertStatus(200),
					assertBodyExists("key"),
					assertBodyExists("keyId"),
					assertHeaderExists("Unkey-Trace-Id"),
				},
			}.Run(ctx, make(map[string]any))

			Step[map[string]any]{
				Name:   "Verify Key",
				Method: "POST",
				Url:    fmt.Sprintf("%s/v1/keys.verifyKey", env.BaseUrl),
				Header: map[string]string{
					"Content-Type": "application/json",
				},
				Body: map[string]any{
					"key": keyResponse["key"],
				},
				Assertions: []assertion{
					assertStatus(200),
					assertBody("valid", true),
					assertHeaderExists("Unkey-Trace-Id"),
				},
			}.Run(ctx, make(map[string]any))

			Step[map[string]any]{
				Name:   "Revoke Key",
				Method: "POST",
				Url:    fmt.Sprintf("%s/v1/keys.removeKey", env.BaseUrl),
				Header: map[string]string{
					"Content-Type":  "application/json",
					"Authorization": fmt.Sprintf("Bearer %s", env.RootKey),
				},
				Body: map[string]any{
					"keyId": keyResponse["keyId"],
				},
				Assertions: []assertion{
					assertStatus(200),
					assertHeaderExists("Unkey-Trace-Id"),
				},
			}.Run(ctx, make(map[string]any))

		}

	})
View Source
var ListKeys = newScenario(
	"ListKeys",
	func(ctx context.Context, env Env) {
		createApiResponse := Step[map[string]any]{
			Name:   "Create API",
			Method: "POST",
			Url:    fmt.Sprintf("%s/v1/apis.createApi", env.BaseUrl),
			Header: map[string]string{
				"Content-Type":  "application/json",
				"Authorization": fmt.Sprintf("Bearer %s", env.RootKey),
			},
			Body: map[string]any{
				"name": "scenario-test-pls-delete",
			},
			Assertions: []assertion{
				assertStatus(200),
				assertBodyExists("apiId"),
				assertHeaderExists("Unkey-Trace-Id"),
			},
		}.Run(ctx, make(map[string]any))

		defer Step[map[string]any]{
			Name:   "Delete API",
			Method: "POST",
			Url:    fmt.Sprintf("%s/v1/apis.removeApi", env.BaseUrl),
			Header: map[string]string{
				"Content-Type":  "application/json",
				"Authorization": fmt.Sprintf("Bearer %s", env.RootKey),
			},
			Body: map[string]any{
				"apiId": createApiResponse["apiId"],
			},
			Assertions: []assertion{
				assertStatus(200),
				assertHeaderExists("Unkey-Trace-Id"),
			},
		}.Run(ctx, make(map[string]any))

		for i := 0; i < 5; i++ {
			keyResponse := Step[map[string]any]{
				Name:   "Create Key",
				Method: "POST",
				Url:    fmt.Sprintf("%s/v1/keys.createKey", env.BaseUrl),
				Header: map[string]string{
					"Content-Type":  "application/json",
					"Authorization": fmt.Sprintf("Bearer %s", env.RootKey),
				},
				Body: map[string]any{
					"apiId": createApiResponse["apiId"],
				},
				Assertions: []assertion{
					assertStatus(200),
					assertBodyExists("key"),
					assertBodyExists("keyId"),
					assertHeaderExists("Unkey-Trace-Id"),
				},
			}.Run(ctx, make(map[string]any))

			defer Step[map[string]any]{
				Name:   "Revoke Key",
				Method: "POST",
				Url:    fmt.Sprintf("%s/v1/keys.removeKey", env.BaseUrl),
				Header: map[string]string{
					"Content-Type":  "application/json",
					"Authorization": fmt.Sprintf("Bearer %s", env.RootKey),
				},
				Body: map[string]any{
					"keyId": keyResponse["keyId"],
				},
				Assertions: []assertion{
					assertStatus(200),
					assertHeaderExists("Unkey-Trace-Id"),
				},
			}.Run(ctx, make(map[string]any))

		}

		listKeys := Step[map[string]any]{
			Name:   "List Keys",
			Method: "GET",
			Url:    fmt.Sprintf("%s/v1/apis/%s/keys", env.BaseUrl, createApiResponse["apiId"]),
			Header: map[string]string{
				"Content-Type":  "application/json",
				"Authorization": fmt.Sprintf("Bearer %s", env.RootKey),
			},
			Assertions: []assertion{
				assertStatus(200),
				assertBodyExists("keys"),
				assertHeaderExists("Unkey-Trace-Id"),
			},
		}
		foundKeys := listKeys.Run(ctx, make(map[string]any))

		if len(foundKeys["keys"].([]any)) != 5 {
			listKeys.fail("expected 5 keys, got %d", len(foundKeys["keys"].([]any)))
		}

	})
View Source
var UpdateRemaining = newScenario(
	"UpdateRemaining",
	func(ctx context.Context, env Env) {
		createApiResponse := Step[map[string]any]{
			Name:   "Create API",
			Method: "POST",
			Url:    fmt.Sprintf("%s/v1/apis.createApi", env.BaseUrl),
			Header: map[string]string{
				"Content-Type":  "application/json",
				"Authorization": fmt.Sprintf("Bearer %s", env.RootKey),
			},
			Body: map[string]any{
				"name": "scenario-test-pls-delete",
			},
			Assertions: []assertion{
				assertStatus(200),
				assertBodyExists("apiId"),
				assertHeaderExists("Unkey-Trace-Id"),
			},
		}.Run(ctx, make(map[string]any))

		defer Step[map[string]any]{
			Name:   "Delete API",
			Method: "POST",
			Url:    fmt.Sprintf("%s/v1/apis.removeApi", env.BaseUrl),
			Header: map[string]string{
				"Content-Type":  "application/json",
				"Authorization": fmt.Sprintf("Bearer %s", env.RootKey),
			},
			Body: map[string]any{
				"apiId": createApiResponse["apiId"],
			},
			Assertions: []assertion{
				assertStatus(200),
				assertHeaderExists("Unkey-Trace-Id"),
			},
		}.Run(ctx, make(map[string]any))

		keyResponse := Step[map[string]any]{
			Name:   "Create Key",
			Method: "POST",
			Url:    fmt.Sprintf("%s/v1/keys.createKey", env.BaseUrl),
			Header: map[string]string{
				"Content-Type":  "application/json",
				"Authorization": fmt.Sprintf("Bearer %s", env.RootKey),
			},
			Body: map[string]any{
				"apiId":     createApiResponse["apiId"],
				"remaining": 5,
			},
			Assertions: []assertion{
				assertStatus(200),
				assertBodyExists("key"),
				assertBodyExists("keyId"),
				assertHeaderExists("Unkey-Trace-Id"),
			},
		}.Run(ctx, make(map[string]any))

		for i := 4; i >= 0; i-- {

			Step[map[string]any]{
				Name:   "Verify Key",
				Method: "POST",
				Url:    fmt.Sprintf("%s/v1/keys.verifyKey", env.BaseUrl),
				Header: map[string]string{
					"Content-Type": "application/json",
				},
				Body: map[string]any{
					"key": keyResponse["key"],
				},
				Assertions: []assertion{
					assertStatus(200),
					assertBody("valid", true),
					assertBody("remaining", float64(i)),
					assertHeaderExists("Unkey-Trace-Id"),
				},
			}.Run(ctx, make(map[string]any))
		}
		Step[map[string]any]{
			Name:   "Verify Key - should fail",
			Method: "POST",
			Url:    fmt.Sprintf("%s/v1/keys.verifyKey", env.BaseUrl),
			Header: map[string]string{
				"Content-Type": "application/json",
			},
			Body: map[string]any{
				"key": keyResponse["key"],
			},
			Assertions: []assertion{
				assertStatus(200),
				assertBody("valid", false),
				assertBody("code", "KEY_USAGE_EXCEEDED"),
				assertBody("remaining", float64(0)),
				assertHeaderExists("Unkey-Trace-Id"),
			},
		}.Run(ctx, make(map[string]any))

		Step[map[string]any]{
			Name:   "Set remaining to 5",
			Method: "POST",
			Url:    fmt.Sprintf("%s/v1/keys.updateKey", env.BaseUrl),
			Header: map[string]string{
				"Content-Type":  "application/json",
				"Authorization": fmt.Sprintf("Bearer %s", env.RootKey),
			},
			Body: map[string]any{
				"keyId":     keyResponse["keyId"],
				"remaining": 5,
			},
			Assertions: []assertion{
				assertStatus(200),
				assertHeaderExists("Unkey-Trace-Id"),
			},
		}.Run(ctx, make(map[string]any))

		Step[map[string]any]{
			Name:   "Verify key after update",
			Method: "POST",
			Url:    fmt.Sprintf("%s/v1/keys.verifyKey", env.BaseUrl),
			Header: map[string]string{
				"Content-Type": "application/json",
			},
			Body: map[string]any{
				"key": keyResponse["key"],
			},
			Assertions: []assertion{
				assertStatus(200),
				assertBody("valid", true),
				assertBody("remaining", float64(4)),
				assertHeaderExists("Unkey-Trace-Id"),
			},
		}.Run(ctx, make(map[string]any))

		Step[map[string]any]{
			Name:   "Revoke Key",
			Method: "POST",
			Url:    fmt.Sprintf("%s/v1/keys.removeKey", env.BaseUrl),
			Header: map[string]string{
				"Content-Type":  "application/json",
				"Authorization": fmt.Sprintf("Bearer %s", env.RootKey),
			},
			Body: map[string]any{
				"keyId": keyResponse["keyId"],
			},
			Assertions: []assertion{
				assertStatus(200),
				assertHeaderExists("Unkey-Trace-Id"),
			},
		}.Run(ctx, make(map[string]any))

	})

Functions

This section is empty.

Types

type AssertRequest

type AssertRequest struct {
	Status int
	Header http.Header
	Body   string
}

type Env

type Env struct {
	BaseUrl string
	RootKey string
	ApiId   string
}

type Scenario

type Scenario struct {
	Name   string
	StepFn stepFn
}

func (Scenario) Run

func (s Scenario) Run(ctx context.Context, env Env)

type Step

type Step[T any] struct {
	Name   string
	Body   map[string]any
	Url    string
	Method string
	Header map[string]string

	Assertions []assertion
}

func (Step[T]) Run

func (s Step[T]) Run(ctx context.Context, res T) T

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL