Skip to content

Commit

Permalink
refactor: handle invalid environment key (#103)
Browse files Browse the repository at this point in the history
* refactor: handle invalid environment key

* Avoid breaking change

* fix example

* revert example changes
  • Loading branch information
craigpastro authored Nov 7, 2023
1 parent 48c37cf commit 3c23c48
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 1 deletion.
5 changes: 5 additions & 0 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"errors"
"fmt"
"strings"
"sync/atomic"
"time"

Expand Down Expand Up @@ -54,6 +55,10 @@ func NewClient(apiKey string, options ...Option) *Client {
c.client.SetLogger(c.log)

if c.config.localEvaluation {
if !strings.HasPrefix(apiKey, "ser.") {
panic("In order to use local evaluation, please generate a server key in the environment settings page.")
}

go c.pollEnvironment(c.ctxLocalEval)
}
// Initialize analytics processor
Expand Down
7 changes: 7 additions & 0 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@ import (
"github.com/stretchr/testify/assert"
)

func TestClientErrorsIfLocalEvaluationWithNonServerSideKey(t *testing.T) {
// When, Then
assert.Panics(t, func() {
_ = flagsmith.NewClient("key", flagsmith.WithLocalEvaluation(context.Background()))
})
}

func TestClientUpdatesEnvironmentOnStartForLocalEvaluation(t *testing.T) {
// Given
ctx := context.Background()
Expand Down
2 changes: 1 addition & 1 deletion fixtures/fixture.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
)

const BaseURL = "http://localhost:8000/api/v1/"
const EnvironmentAPIKey = "test_key"
const EnvironmentAPIKey = "ser.test_key"
const Feature1Value = "some_value"
const Feature1Name = "feature_1"
const Feature1ID = 1
Expand Down

0 comments on commit 3c23c48

Please sign in to comment.