Skip to content

Commit

Permalink
fix: exception for health check url
Browse files Browse the repository at this point in the history
  • Loading branch information
nick-bisonai committed Jul 8, 2024
1 parent 5437aea commit 4141459
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 1 deletion.
22 changes: 21 additions & 1 deletion node/pkg/dal/tests/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,12 +94,32 @@ func TestShouldFailWithoutApiKey(t *testing.T) {
testItems.Controller.Start(ctx)
go testItems.App.Listen(":8090")
resp, err := request.RequestRaw(request.WithEndpoint("http://localhost:8090/api/v1"))
if err != nil {
t.Fatalf("error getting latest data: %v", err)
}

assert.Equal(t, 200, resp.StatusCode)

sampleSubmissionData, err := generateSampleSubmissionData(
testItems.TmpConfig.ID,
int64(15),
time.Now(),
1,
"test-aggregate",
)
if err != nil {
t.Fatalf("error generating sample submission data: %v", err)
}

aggregator.SetLatestGlobalAggregateAndProof(ctx, testItems.TmpConfig.ID, sampleSubmissionData.GlobalAggregate, sampleSubmissionData.Proof)

result, err := request.RequestRaw(request.WithEndpoint("http://localhost:8090/api/v1/dal/latest-data-feeds/test-aggregate"))

if err != nil {
t.Fatalf("error getting latest data: %v", err)
}

assert.Equal(t, 401, resp.StatusCode)
assert.Equal(t, 401, result.StatusCode)
}

func TestApiGetLatest(t *testing.T) {
Expand Down
3 changes: 3 additions & 0 deletions node/pkg/dal/tests/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,9 @@ func setup(ctx context.Context) (func() error, *TestItems, error) {
testItems.Collector = api.ApiController.Collector

v1 := app.Group("/api/v1")
v1.Get("/", func(c *fiber.Ctx) error {
return c.SendString("Orakl Node DAL API")
})
api.Routes(v1)

return cleanup(ctx, testItems), testItems, nil
Expand Down
7 changes: 7 additions & 0 deletions node/pkg/dal/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ func validator(ctx *fiber.Ctx, s string) (bool, error) {
return false, fmt.Errorf("invalid api key")
}

func authFilter(c *fiber.Ctx) bool {
originalURL := strings.ToLower(c.OriginalURL())
return originalURL == "/api/v1"
}

func Setup(ctx context.Context) (*fiber.App, error) {
_, err := db.GetPool(ctx)
if err != nil {
Expand All @@ -52,13 +57,15 @@ func Setup(ctx context.Context) (*fiber.App, error) {

app.Use(recover.New(
recover.Config{

EnableStackTrace: true,
StackTraceHandler: CustomStackTraceHandler,
},
))

app.Use(cors.New())
app.Use(keyauth.New(keyauth.Config{
Next: authFilter,
KeyLookup: "header:X-API-Key",
Validator: validator,
}))
Expand Down

0 comments on commit 4141459

Please sign in to comment.