From e367896883ea5429b7be0e825831791043a52599 Mon Sep 17 00:00:00 2001 From: andronache Date: Wed, 28 Apr 2021 17:45:36 +0300 Subject: [PATCH] Fixed issue for V1ProcessEvent not using the processed tenant and added tests for it --- engine/attributes.go | 2 + engine/attributes_test.go | 101 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 103 insertions(+) diff --git a/engine/attributes.go b/engine/attributes.go index b30d48e49..78a90a36f 100644 --- a/engine/attributes.go +++ b/engine/attributes.go @@ -477,6 +477,7 @@ func (alS *AttributeService) V1ProcessEvent(args *AttrArgsProcessEvent, (eNV[utils.MetaVars].(utils.MapStorage))[utils.ProcessRuns] = i + 1 var evRply *AttrSProcessEventReply evRply, err = alS.processEvent(tnt, args, eNV, dynDP, lastID) + if err != nil { if err != utils.ErrNotFound { err = utils.NewErrServerError(err) @@ -486,6 +487,7 @@ func (alS *AttributeService) V1ProcessEvent(args *AttrArgsProcessEvent, break } args.CGREvent.Tenant = evRply.CGREvent.Tenant + tnt = evRply.CGREvent.Tenant lastID = evRply.MatchedProfiles[0] matchedIDs = append(matchedIDs, lastID) for _, fldName := range evRply.AlteredFields { diff --git a/engine/attributes_test.go b/engine/attributes_test.go index 93b32b6d2..0f46e296e 100644 --- a/engine/attributes_test.go +++ b/engine/attributes_test.go @@ -24,6 +24,7 @@ import ( "log" "os" "reflect" + "sort" "strings" "testing" @@ -172,3 +173,103 @@ func TestAttributesV1GetAttributeForEvent2(t *testing.T) { t.Errorf("\nexpected: <%+v>, \nreceived: <%+v>", experr, err) } } + +func TestAttributesV1ProcessEvent(t *testing.T) { + cfg := config.NewDefaultCGRConfig() + cfg.FilterSCfg().ResourceSConns = []string{} + conMng := &ConnManager{} + db := NewInternalDB(nil, nil, true) + dm := NewDataManager(db, nil, conMng) + filterS := NewFilterS(cfg, conMng, dm) + attr := &AttributeProfile{ + Tenant: "cgrates.org", + ID: "ATTR_CHANGE_TENANT_FROM_USER", + Contexts: []string{utils.MetaAny}, + FilterIDs: []string{"*string:~*req.Account:dan@itsyscom.com|adrian@itsyscom.com"}, + ActivationInterval: nil, + Attributes: []*Attribute{ + { + FilterIDs: nil, + Path: "*tenant", + Type: "*variable", + Value: config.NewRSRParsersMustCompile("~*req.Account:s/(.*)@(.*)/${1}.${2}/", utils.InfieldSep), + }, + { + FilterIDs: nil, + Path: "*req.Account", + Type: "*variable", + Value: config.NewRSRParsersMustCompile("~*req.Account:s/(dan)@(.*)/${1}.${2}/:s/(adrian)@(.*)/andrei.${2}/", utils.InfieldSep), + }, + { + FilterIDs: nil, + Path: "*tenant", + Type: "*composed", + Value: config.NewRSRParsersMustCompile(".co.uk", utils.InfieldSep), + }, + }, + Blocker: false, + Weight: 20, + } + err := dm.SetAttributeProfile(attr, true) + if err != nil { + t.Error(err) + } + + attr2 := &AttributeProfile{ + Tenant: "adrian.itsyscom.com.co.uk", + ID: "ATTR_MATCH_TENANT", + Contexts: []string{utils.MetaAny}, + ActivationInterval: nil, + Attributes: []*Attribute{ + { + FilterIDs: nil, + Path: "*req.Password", + Type: utils.MetaConstant, + Value: config.NewRSRParsersMustCompile("CGRATES.ORG", utils.InfieldSep), + }, + }, + Blocker: false, + Weight: 20, + } + + err = dm.SetAttributeProfile(attr2, true) + if err != nil { + t.Error(err) + } + + alS := NewAttributeService(dm, filterS, cfg) + args := &AttrArgsProcessEvent{ + CGREvent: &utils.CGREvent{ + Tenant: "cgrates.org", + ID: "123", + Event: map[string]interface{}{ + utils.AccountField: "adrian@itsyscom.com", + }, + }, + ProcessRuns: utils.IntPointer(2), + } + rply := &AttrSProcessEventReply{} + expected := &AttrSProcessEventReply{ + MatchedProfiles: []string{"ATTR_CHANGE_TENANT_FROM_USER", "ATTR_MATCH_TENANT"}, + AlteredFields: []string{"*req.Account", "*req.Password", "*tenant"}, + CGREvent: &utils.CGREvent{ + Tenant: "adrian.itsyscom.com.co.uk", + ID: "123", + Time: nil, + Event: map[string]interface{}{ + utils.AccountField: "andrei.itsyscom.com", + "Password": "CGRATES.ORG", + }, + APIOpts: map[string]interface{}{}, + }, + blocker: false, + } + err = alS.V1ProcessEvent(args, rply) + sort.Strings(rply.AlteredFields) + if err != nil { + t.Errorf("\nExpected <%+v>, \nReceived <%+v>", nil, err) + } + if !reflect.DeepEqual(expected, rply) { + t.Errorf("\nExpected <%+v>, \nReceived <%+v>", utils.ToJSON(expected), utils.ToJSON(rply)) + } +}