Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: preliminary tests and merge fix for authv2 #3584

Merged
merged 22 commits into from
Sep 24, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 0 additions & 18 deletions core/http/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,24 +31,6 @@ import (
"github.com/rs/zerolog/log"
)

func readAuthHeader(c *fiber.Ctx) string {
authHeader := c.Get("Authorization")

// elevenlabs
xApiKey := c.Get("xi-api-key")
if xApiKey != "" {
authHeader = "Bearer " + xApiKey
}

// anthropic
xApiKey = c.Get("x-api-key")
if xApiKey != "" {
authHeader = "Bearer " + xApiKey
}

return authHeader
}

// Embed a directory
//
//go:embed static/*
Expand Down
43 changes: 43 additions & 0 deletions core/http/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ import (
"github.com/sashabaranov/go-openai/jsonschema"
)

const apiKey = "joshua"

const testPrompt = `### System:
You are an AI assistant that follows instruction extremely well. Help as much as you can.

Expand Down Expand Up @@ -101,6 +103,7 @@ func postModelApplyRequest(url string, request modelApplyRequest) (response map[
return
}
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Authorization", "Bearer "+apiKey)

// Make the request
client := &http.Client{}
Expand Down Expand Up @@ -140,6 +143,7 @@ func postRequestJSON[B any](url string, bodyJson *B) error {
}

req.Header.Set("Content-Type", "application/json")
req.Header.Set("Authorization", "Bearer "+apiKey)

client := &http.Client{}
resp, err := client.Do(req)
Expand Down Expand Up @@ -175,6 +179,7 @@ func postRequestResponseJSON[B1 any, B2 any](url string, reqJson *B1, respJson *
}

req.Header.Set("Content-Type", "application/json")
req.Header.Set("Authorization", "Bearer "+apiKey)

client := &http.Client{}
resp, err := client.Do(req)
Expand All @@ -195,6 +200,35 @@ func postRequestResponseJSON[B1 any, B2 any](url string, reqJson *B1, respJson *
return json.Unmarshal(body, respJson)
}

func postInvalidRequest(url string) (error, int) {

req, err := http.NewRequest("POST", url, bytes.NewBufferString("invalid request"))
if err != nil {
return err, -1
}

req.Header.Set("Content-Type", "application/json")

client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return err, -1
}

defer resp.Body.Close()

body, err := io.ReadAll(resp.Body)
if err != nil {
return err, -1
}

if resp.StatusCode < 200 || resp.StatusCode >= 400 {
return fmt.Errorf("unexpected status code: %d, body: %s", resp.StatusCode, string(body)), resp.StatusCode
}

return nil, resp.StatusCode
}

//go:embed backend-assets/*
var backendAssets embed.FS

Expand Down Expand Up @@ -260,6 +294,7 @@ var _ = Describe("API test", func() {
config.WithContext(c),
config.WithGalleries(galleries),
config.WithModelPath(modelDir),
config.WithApiKeys([]string{apiKey}),
config.WithBackendAssets(backendAssets),
config.WithBackendAssetsOutput(backendAssetsDir))...)
Expect(err).ToNot(HaveOccurred())
Expand Down Expand Up @@ -295,6 +330,14 @@ var _ = Describe("API test", func() {
Expect(err).To(HaveOccurred())
})

Context("Auth Tests", func() {
It("Should fail if the api key is missing", func() {
err, sc := postInvalidRequest("http://127.0.0.1:9090/models/available")
Expect(err).ToNot(BeNil())
Expect(sc).To(Equal(403))
})
})

Context("Applying models", func() {

It("applies models from a gallery", func() {
Expand Down
Loading