Skip to content

Commit

Permalink
Merge pull request #8 from subiradhikari/FFM-744
Browse files Browse the repository at this point in the history
FFM-744: Changes to send target in authentication request
  • Loading branch information
rushabh-harness authored Apr 29, 2021
2 parents 3dace62 + a144d95 commit 1dd9e5f
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 43 deletions.
19 changes: 16 additions & 3 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ type CfClient struct {
mux sync.RWMutex
api rest.ClientWithResponsesInterface
sdkKey string
auth rest.AuthenticationRequest
config *config
environmentID string
token string
Expand Down Expand Up @@ -81,7 +82,7 @@ func NewCfClient(sdkKey string, options ...ConfigOption) (*CfClient, error) {
}
}

go client.authenticate(ctx)
go client.authenticate(ctx, client.config.target)

go client.retrieve(ctx)

Expand Down Expand Up @@ -162,8 +163,19 @@ func (c *CfClient) streamConnect() {
}
}

func (c *CfClient) authenticate(ctx context.Context) {

func (c *CfClient) authenticate(ctx context.Context, target evaluation.Target) {
t := struct {
Anonymous *bool `json:"anonymous,omitempty"`
Attributes *map[string]interface{} `json:"attributes,omitempty"`
Identifier string `json:"identifier"`
Name *string `json:"name,omitempty"`
}{
target.Anonymous,
target.Attributes,
target.Identifier,
target.Name,
}
c.auth.Target = &t
c.mux.RLock()
defer c.mux.RUnlock()

Expand All @@ -176,6 +188,7 @@ func (c *CfClient) authenticate(ctx context.Context) {

response, err := httpClient.AuthenticateWithResponse(ctx, rest.AuthenticateJSONRequestBody{
ApiKey: c.sdkKey,
Target: c.auth.Target,
})
if err != nil {
c.config.Logger.Error(err)
Expand Down
9 changes: 5 additions & 4 deletions dto/target_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,17 +69,18 @@ func (b *targetBuilder) Name(name string) TargetBuilderInterface {

// Anonymous target object
func (b *targetBuilder) Anonymous(value bool) TargetBuilderInterface {
b.Target.Anonymous = value
b.Target.Anonymous = &value
return b
}

// Custom object
func (b *targetBuilder) Custom(key string, value interface{}) TargetBuilderInterface {
m := make(map[string]interface{})
if b.Target.Attributes == nil {
b.Target.Attributes = make(map[string]interface{})
b.Target.Attributes = &m
}

b.Target.Attributes[key] = value
m[key] = value
b.Target.Attributes = &m
return b
}

Expand Down
27 changes: 15 additions & 12 deletions evaluation/feature_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -435,12 +435,13 @@ func TestFeatureConfig_IntVariation(t *testing.T) {
}

func TestServingRules_GetVariationName(t *testing.T) {

f := false
dev := "dev"
harness := "Harness"
onVariationIdentifier := "v1"
offVariationIdentifier := "v2"

m := make(map[string]interface{})
m["email"] = "[email protected]"
segment := &Segment{
Identifier: "beta",
Name: "beta",
Expand All @@ -450,12 +451,11 @@ func TestServingRules_GetVariationName(t *testing.T) {
Tags: nil,
Version: 1,
}

target := &Target{
Identifier: harness,
Name: &harness,
Anonymous: false,
Attributes: nil,
Anonymous: &f,
Attributes: &m,
}
type args struct {
target *Target
Expand Down Expand Up @@ -532,14 +532,17 @@ func TestServingRules_GetVariationName(t *testing.T) {
}

func TestFeatureConfig_Evaluate(t *testing.T) {
f := false
harness := "Harness"
v1 := "v1"
v2 := "v2"
m := make(map[string]interface{})
m["email"] = "[email protected]"
target := Target{
Identifier: harness,
Name: nil,
Anonymous: false,
Attributes: nil,
Anonymous: &f,
Attributes: &m,
}
type fields struct {
DefaultServe Serve
Expand Down Expand Up @@ -620,6 +623,7 @@ func TestFeatureConfig_Evaluate(t *testing.T) {
}

func TestClause_Evaluate(t *testing.T) {
f := false
type fields struct {
Attribute string
ID string
Expand All @@ -632,14 +636,13 @@ func TestClause_Evaluate(t *testing.T) {
segments Segments
operator types.ValueType
}

m := make(map[string]interface{})
m["email"] = "[email protected]"
target := Target{
Identifier: "john",
Name: nil,
Anonymous: false,
Attributes: map[string]interface{}{
"email": "[email protected]",
},
Anonymous: &f,
Attributes: &m,
}
tests := []struct {
name string
Expand Down
18 changes: 10 additions & 8 deletions evaluation/reflection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,16 @@ import (
)

func TestGetStructFieldValue(t *testing.T) {
m := make(map[string]interface{})
m["email"] = "[email protected]"
identifier := "john"
name := "John"
f := false
target := Target{
Identifier: identifier,
Name: &name,
Anonymous: false,
Attributes: map[string]interface{}{
"email": "[email protected]",
},
Anonymous: &f,
Attributes: &m,
}
type args struct {
target interface{}
Expand Down Expand Up @@ -55,13 +56,14 @@ func TestGetStructFieldValue(t *testing.T) {
func Test_caseInsensitiveFieldByName(t *testing.T) {
identifier := "john"
name := "John"
m := make(map[string]interface{})
m["email"] = "[email protected]"
f := false
target := Target{
Identifier: identifier,
Name: &name,
Anonymous: false,
Attributes: map[string]interface{}{
"email": "[email protected]",
},
Anonymous: &f,
Attributes: &m,
}
type args struct {
v reflect.Value
Expand Down
10 changes: 5 additions & 5 deletions evaluation/segment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@ func TestSegment_Evaluate(t *testing.T) {
type args struct {
target *Target
}

f := false
m := make(map[string]interface{})
m["email"] = "[email protected]"
target := Target{
Identifier: "john",
Name: nil,
Anonymous: false,
Attributes: map[string]interface{}{
"email": "[email protected]",
},
Anonymous: &f,
Attributes: &m,
}

tests := []struct {
Expand Down
7 changes: 4 additions & 3 deletions evaluation/target.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,15 @@ import (
type Target struct {
Identifier string
Name *string
Anonymous bool
Attributes map[string]interface{}
Anonymous *bool
Attributes *map[string]interface{}
}

// GetAttrValue returns value from target with specified attribute
func (t Target) GetAttrValue(attr string) reflect.Value {
var value reflect.Value
attrVal, ok := t.Attributes[attr] // first check custom attributes
attrs := *t.Attributes
attrVal, ok := attrs[attr] // first check custom attributes
if ok {
value = reflect.ValueOf(attrVal)
} else {
Expand Down
20 changes: 12 additions & 8 deletions evaluation/target_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (

func TestTarget_GetOperator(t1 *testing.T) {
harness := "Harness"
m := make(map[string]interface{})
m["anonymous"] = false
type fields struct {
Identifier string
Name *string
Expand All @@ -29,7 +31,7 @@ func TestTarget_GetOperator(t1 *testing.T) {
Name *string
Anonymous bool
Attributes map[string]interface{}
}{Identifier: "harness", Name: &harness, Anonymous: false, Attributes: nil},
}{Identifier: "harness", Name: &harness, Anonymous: false, Attributes: m},
args: struct{ attr string }{attr: "anonymous"}, want: types.Boolean(false)},
{name: "string operator", fields: struct {
Identifier string
Expand Down Expand Up @@ -63,8 +65,8 @@ func TestTarget_GetOperator(t1 *testing.T) {
t := Target{
Identifier: val.fields.Identifier,
Name: val.fields.Name,
Anonymous: val.fields.Anonymous,
Attributes: val.fields.Attributes,
Anonymous: &val.fields.Anonymous,
Attributes: &val.fields.Attributes,
}
if got := t.GetOperator(val.args.attr); !reflect.DeepEqual(got, val.want) {
t1.Errorf("GetOperator() = %v, want %v", got, val.want)
Expand Down Expand Up @@ -115,8 +117,8 @@ func TestTarget_GetAttrValue(t1 *testing.T) {
t := Target{
Identifier: val.fields.Identifier,
Name: val.fields.Name,
Anonymous: val.fields.Anonymous,
Attributes: val.fields.Attributes,
Anonymous: &val.fields.Anonymous,
Attributes: &val.fields.Attributes,
}
if got := t.GetAttrValue(val.args.attr); !reflect.DeepEqual(got.Interface(), val.want.Interface()) {
t1.Errorf("GetAttrValue() = %v, want %v", got, val.want)
Expand All @@ -126,6 +128,8 @@ func TestTarget_GetAttrValue(t1 *testing.T) {
}

func TestTarget_GetOperator1(t1 *testing.T) {
m := make(map[string]interface{})
m["anonymous"] = false
type fields struct {
Identifier string
Name *string
Expand All @@ -148,7 +152,7 @@ func TestTarget_GetOperator1(t1 *testing.T) {
Name *string
Anonymous bool
Attributes map[string]interface{}
}{Identifier: "john", Name: &name, Anonymous: false, Attributes: types.JSON{}},
}{Identifier: "john", Name: &name, Anonymous: false, Attributes: types.JSON{"anonymous": false}},
args: struct{ attr string }{attr: "anonymous"}, want: types.Boolean(false)},
{name: "string operator", fields: struct {
Identifier string
Expand Down Expand Up @@ -182,8 +186,8 @@ func TestTarget_GetOperator1(t1 *testing.T) {
t := Target{
Identifier: val.fields.Identifier,
Name: val.fields.Name,
Anonymous: val.fields.Anonymous,
Attributes: val.fields.Attributes,
Anonymous: &val.fields.Anonymous,
Attributes: &val.fields.Attributes,
}
if got := t.GetOperator(val.args.attr); !reflect.DeepEqual(got, val.want) {
t1.Errorf("GetOperator() = %v, want %v", got, val.want)
Expand Down

0 comments on commit 1dd9e5f

Please sign in to comment.