From b53922bfa1a540fd7c96c6cbddbe9f65b58fab37 Mon Sep 17 00:00:00 2001 From: DanB Date: Thu, 27 Aug 2015 14:01:24 +0200 Subject: [PATCH] Postpaid calls being checked by GetDerivedMaxSessionTime, should exist and have cost for the authorized call, fixes #140, fixes #138 --- engine/responder.go | 5 +- engine/responder_test.go | 4 +- general_tests/auth_test.go | 140 +++++++++++++++++++++++++++++++++++ general_tests/placeholder.go | 2 - 4 files changed, 145 insertions(+), 6 deletions(-) create mode 100644 general_tests/auth_test.go delete mode 100644 general_tests/placeholder.go diff --git a/engine/responder.go b/engine/responder.go index d270e0846..73cfe6697 100644 --- a/engine/responder.go +++ b/engine/responder.go @@ -195,7 +195,7 @@ func (rs *Responder) GetDerivedMaxSessionTime(ev *StoredCdr, reply *float64) err } dcs, _ = dcs.AppendDefaultRun() for _, dc := range dcs { - if !utils.IsSliceMember([]string{utils.META_PREPAID, utils.META_PSEUDOPREPAID, utils.PREPAID, utils.PSEUDOPREPAID}, ev.GetReqType(dc.ReqTypeField)) { // Only consider prepaid and pseudoprepaid for MaxSessionTime + if utils.IsSliceMember([]string{utils.META_RATED, utils.RATED}, ev.GetReqType(dc.ReqTypeField)) { // Only consider prepaid and pseudoprepaid for MaxSessionTime continue } runFilters, _ := utils.ParseRSRFields(dc.RunFilters, utils.INFIELD_SEP) @@ -236,6 +236,9 @@ func (rs *Responder) GetDerivedMaxSessionTime(ev *StoredCdr, reply *float64) err *reply = 0 return err } + if utils.IsSliceMember([]string{utils.META_POSTPAID, utils.POSTPAID}, ev.GetReqType(dc.ReqTypeField)) { // Only consider prepaid and pseudoprepaid for MaxSessionTime + continue + } // Set maxCallDuration, smallest out of all forked sessions if maxCallDuration == -1.0 { // first time we set it /not initialized yet maxCallDuration = remainingDuration diff --git a/engine/responder_test.go b/engine/responder_test.go index eb7260bd2..c2dcd91db 100644 --- a/engine/responder_test.go +++ b/engine/responder_test.go @@ -18,7 +18,6 @@ along with this program. If not, see package engine import ( - "log" "reflect" "testing" "time" @@ -37,7 +36,7 @@ func init() { // Test internal abilites of GetDerivedChargers func TestResponderGetDerivedChargers(t *testing.T) { - cfgedDC := utils.DerivedChargers{&utils.DerivedCharger{RunId: "responder1", ReqTypeField: "test", DirectionField: "test", TenantField: "test", + cfgedDC := utils.DerivedChargers{&utils.DerivedCharger{RunId: "responder1", ReqTypeField: utils.META_DEFAULT, DirectionField: "test", TenantField: "test", CategoryField: "test", AccountField: "test", SubjectField: "test", DestinationField: "test", SetupTimeField: "test", AnswerTimeField: "test", UsageField: "test"}} rsponder = &Responder{} attrs := &utils.AttrDerivedChargers{Tenant: "cgrates.org", Category: "call", Direction: "*out", Account: "responder_test", Subject: "responder_test"} @@ -115,7 +114,6 @@ func TestGetDerivedMaxSessionTime(t *testing.T) { t.Errorf("Expecting: %+v, received: %+v ", charger1, dcs) } if err := rsponder.GetDerivedMaxSessionTime(cdr, &maxSessionTime); err != nil { - log.Printf("CD: %+v", cdr) t.Error(err) } else if maxSessionTime != 1e+10 { // Smallest one, 10 seconds t.Error("Unexpected maxSessionTime received: ", maxSessionTime) diff --git a/general_tests/auth_test.go b/general_tests/auth_test.go new file mode 100644 index 000000000..b8d4d5c1a --- /dev/null +++ b/general_tests/auth_test.go @@ -0,0 +1,140 @@ +/* +Real-time Charging System for Telecom & ISP environments +Copyright (C) 2012-2015 ITsysCOM GmbH + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see +*/ + +package general_tests + +import ( + "testing" + "time" + + "github.com/cgrates/cgrates/cache2go" + "github.com/cgrates/cgrates/config" + "github.com/cgrates/cgrates/engine" + "github.com/cgrates/cgrates/utils" +) + +var ratingDbAuth engine.RatingStorage +var acntDbAuth engine.AccountingStorage +var rsponder *engine.Responder + +func TestAuthSetStorage(t *testing.T) { + ratingDbAuth, _ = engine.NewMapStorageJson() + engine.SetRatingStorage(ratingDbAuth) + acntDbAuth, _ = engine.NewMapStorageJson() + engine.SetAccountingStorage(acntDbAuth) + cfg, _ := config.NewDefaultCGRConfig() + config.SetCgrConfig(cfg) + rsponder = new(engine.Responder) + +} + +func TestAuthLoadCsv(t *testing.T) { + timings := `` + destinations := `DST_GERMANY_LANDLINE,49` + rates := `RT_1CENTWITHCF,0.02,0.01,60s,60s,0s` + destinationRates := `DR_GERMANY,DST_GERMANY_LANDLINE,RT_1CENTWITHCF,*up,8,, +DR_ANY_1CNT,*any,RT_1CENTWITHCF,*up,8,,` + ratingPlans := `RP_1,DR_GERMANY,*any,10 +RP_ANY,DR_ANY_1CNT,*any,10` + ratingProfiles := `*out,cgrates.org,call,testauthpostpaid1,2013-01-06T00:00:00Z,RP_1,, +*out,cgrates.org,call,testauthpostpaid2,2013-01-06T00:00:00Z,RP_1,*any, +*out,cgrates.org,call,*any,2013-01-06T00:00:00Z,RP_ANY,,` + sharedGroups := `` + lcrs := `` + actions := `TOPUP10_AC,*topup_reset,,,*monetary,*out,,*any,,,*unlimited,,0,10,10` + actionPlans := `TOPUP10_AT,TOPUP10_AC,*asap,10` + actionTriggers := `` + accountActions := `cgrates.org,testauthpostpaid1,*out,TOPUP10_AT,` + derivedCharges := `` + cdrStats := `` + users := `` + aliases := `` + csvr := engine.NewTpReader(ratingDbAuth, acntDbAuth, engine.NewStringCSVStorage(',', destinations, timings, rates, destinationRates, ratingPlans, ratingProfiles, + sharedGroups, lcrs, actions, actionPlans, actionTriggers, accountActions, derivedCharges, cdrStats, users, aliases), "", "", 10) + if err := csvr.LoadAll(); err != nil { + t.Fatal(err) + } + csvr.WriteToDatabase(false, false) + if acnt, err := acntDbAuth.GetAccount("*out:cgrates.org:testauthpostpaid1"); err != nil { + t.Error(err) + } else if acnt == nil { + t.Error("No account saved") + } + ratingDbAuth.CacheRatingAll() + acntDbAuth.CacheAccountingAll() + + if cachedDests := cache2go.CountEntries(utils.DESTINATION_PREFIX); cachedDests != 1 { + t.Error("Wrong number of cached destinations found", cachedDests) + } + if cachedRPlans := cache2go.CountEntries(utils.RATING_PLAN_PREFIX); cachedRPlans != 2 { + t.Error("Wrong number of cached rating plans found", cachedRPlans) + } + if cachedRProfiles := cache2go.CountEntries(utils.RATING_PROFILE_PREFIX); cachedRProfiles != 3 { + t.Error("Wrong number of cached rating profiles found", cachedRProfiles) + } + if cachedActions := cache2go.CountEntries(utils.ACTION_PREFIX); cachedActions != 1 { + t.Error("Wrong number of cached actions found", cachedActions) + } +} + +func TestAuthPostpaidNoAcnt(t *testing.T) { + cdr := &engine.StoredCdr{TOR: utils.VOICE, ReqType: utils.META_POSTPAID, Direction: "*out", Tenant: "cgrates.org", + Category: "call", Account: "nonexistent", Subject: "testauthpostpaid1", + Destination: "4986517174963", SetupTime: time.Date(2015, 8, 27, 11, 26, 0, 0, time.UTC)} + var maxSessionTime float64 + if err := rsponder.GetDerivedMaxSessionTime(cdr, &maxSessionTime); err == nil || err != utils.ErrNotFound { + t.Error(err) + } +} + +func TestAuthPostpaidNoDestination(t *testing.T) { + // Test subject which does not have destination attached + cdr := &engine.StoredCdr{TOR: utils.VOICE, ReqType: utils.META_POSTPAID, Direction: "*out", Tenant: "cgrates.org", + Category: "call", Account: "testauthpostpaid1", Subject: "testauthpostpaid1", + Destination: "441231234", SetupTime: time.Date(2015, 8, 27, 11, 26, 0, 0, time.UTC)} + var maxSessionTime float64 + if err := rsponder.GetDerivedMaxSessionTime(cdr, &maxSessionTime); err == nil { + t.Error("Expecting error for destination not allowed to subject") + } +} + +func TestAuthPostpaidFallbackDest(t *testing.T) { + // Test subject which has fallback for destination + cdr := &engine.StoredCdr{TOR: utils.VOICE, ReqType: utils.META_POSTPAID, Direction: "*out", Tenant: "cgrates.org", + Category: "call", Account: "testauthpostpaid1", Subject: "testauthpostpaid2", + Destination: "441231234", SetupTime: time.Date(2015, 8, 27, 11, 26, 0, 0, time.UTC)} + var maxSessionTime float64 + if err := rsponder.GetDerivedMaxSessionTime(cdr, &maxSessionTime); err != nil { + t.Error(err) + } else if maxSessionTime != -1 { + t.Error("Unexpected maxSessionTime received: ", maxSessionTime) + } +} + +func TestAuthPostpaidWithDestination(t *testing.T) { + // Test subject which does not have destination attached + cdr := &engine.StoredCdr{TOR: utils.VOICE, ReqType: utils.META_POSTPAID, Direction: "*out", Tenant: "cgrates.org", + Category: "call", Account: "testauthpostpaid1", Subject: "testauthpostpaid1", + Destination: "4986517174963", SetupTime: time.Date(2015, 8, 27, 11, 26, 0, 0, time.UTC)} + var maxSessionTime float64 + if err := rsponder.GetDerivedMaxSessionTime(cdr, &maxSessionTime); err != nil { + t.Error(err) + } else if maxSessionTime != -1 { + t.Error("Unexpected maxSessionTime received: ", maxSessionTime) + } +} diff --git a/general_tests/placeholder.go b/general_tests/placeholder.go deleted file mode 100644 index ea7cac8b2..000000000 --- a/general_tests/placeholder.go +++ /dev/null @@ -1,2 +0,0 @@ -package general_tests -