From 64f7a41a074e843c777c14ae37ab9b04ee8ba8d3 Mon Sep 17 00:00:00 2001 From: Victor Seva Date: Tue, 16 May 2023 11:20:35 +0200 Subject: [PATCH] MT#33031 support reseller preferences Change-Id: I3e715932181ba895dee79155bfd4969ddeed7cd0 --- ngcp/ngcp.lua | 12 ++- ngcp/rep.lua | 39 ++++++++ tests/ngcp.lua | 24 ++++- tests/ngcp_rep.lua | 230 +++++++++++++++++++++++++++++++++++++++++++ tests_v/rep_vars.lua | 95 ++++++++++++++++++ 5 files changed, 398 insertions(+), 2 deletions(-) create mode 100644 ngcp/rep.lua create mode 100644 tests/ngcp_rep.lua create mode 100644 tests_v/rep_vars.lua diff --git a/ngcp/ngcp.lua b/ngcp/ngcp.lua index 7e31195..5c70f03 100644 --- a/ngcp/ngcp.lua +++ b/ngcp/ngcp.lua @@ -1,5 +1,5 @@ -- --- Copyright 2013-2022 SipWise Team +-- Copyright 2013-2023 SipWise Team -- -- 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 @@ -19,6 +19,7 @@ -- local NGCPXAvp = require 'ngcp.xavp' +local NGCPResellerPrefs = require 'ngcp.rep' local NGCPContractPrefs = require 'ngcp.cp' local NGCPProfilePrefs = require 'ngcp.pprof' local NGCPPeerPrefs = require 'ngcp.pp' @@ -56,10 +57,19 @@ function NGCP:init(config) peer = NGCPPeerPrefs:new(self.config), real = NGCPRealPrefs:new(self.config), contract = NGCPContractPrefs:new(self.config), + reseller = NGCPResellerPrefs:new(self.config), fax = NGCPFaxPrefs:new(self.config), } end +function NGCP:caller_reseller_load(id) + return self.prefs.reseller:caller_load(id) +end + +function NGCP:callee_reseller_load(id) + return self.prefs.reseller:callee_load(id) +end + function NGCP:caller_contract_load(contract, ip) local keys = self.prefs.contract:caller_load(contract, ip) diff --git a/ngcp/rep.lua b/ngcp/rep.lua new file mode 100644 index 0000000..c43d1c1 --- /dev/null +++ b/ngcp/rep.lua @@ -0,0 +1,39 @@ +-- +-- Copyright 2013-2023 SipWise Team +-- +-- 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 package 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 . +-- . +-- On Debian systems, the complete text of the GNU General +-- Public License version 3 can be found in "/usr/share/common-licenses/GPL-3". +-- +local utils = require 'ngcp.utils' +local NGCPPrefs = require 'ngcp.pref' + +-- class NGCPResellerPrefs +local NGCPResellerPrefs = utils.inheritsFrom(NGCPPrefs) + +NGCPResellerPrefs.__class__ = 'NGCPResellerPrefs' +NGCPResellerPrefs.group = "reseller_prefs" +NGCPResellerPrefs.db_table = "reseller_preferences" +NGCPResellerPrefs.query = "SELECT * FROM %s WHERE uuid = '%s'" +-- luacheck: globals KSR +function NGCPResellerPrefs:new(config) + local instance = NGCPResellerPrefs:create() + -- creates xavp usr + instance:init(config) + return instance +end + +-- class +return NGCPResellerPrefs diff --git a/tests/ngcp.lua b/tests/ngcp.lua index cccbae2..3a2eaaf 100644 --- a/tests/ngcp.lua +++ b/tests/ngcp.lua @@ -46,6 +46,25 @@ package.preload['luasql.mysql'] = function () end end +local out = [[ +caller_contract_prefs:{dummy={"caller"}} +callee_contract_prefs:{dummy={"callee"}} +caller_usr_prefs:{dummy={"caller"}} +callee_usr_prefs:{dummy={"callee"}} +caller_peer_prefs:{dummy={"caller"}} +callee_peer_prefs:{dummy={"callee"}} +caller_dom_prefs:{dummy={"caller"}} +callee_dom_prefs:{dummy={"callee"}} +caller_prof_prefs:{dummy={"caller"}} +callee_prof_prefs:{dummy={"callee"}} +caller_fax_prefs:{dummy={"caller"}} +callee_fax_prefs:{dummy={"callee"}} +caller_reseller_prefs:{dummy={"caller"}} +callee_reseller_prefs:{dummy={"callee"}} +caller_real_prefs:{dummy={"caller"}} +callee_real_prefs:{dummy={"callee"}} +]] + local NGCP = require 'ngcp.ngcp' local NGCPConfig = require 'ngcp.config' local NGCPDomainPrefs = require 'ngcp.dp' @@ -122,6 +141,9 @@ TestNGCP = {} --class lu.assertNotNil(self.ngcp.prefs.fax) lu.assertEquals(KSR.pv.get("$xavp(caller_fax_prefs=>dummy)"),"caller") lu.assertEquals(KSR.pv.get("$xavp(callee_fax_prefs=>dummy)"),"callee") + lu.assertNotNil(self.ngcp.prefs.reseller) + lu.assertEquals(KSR.pv.get("$xavp(caller_reseller_prefs=>dummy)"),"caller") + lu.assertEquals(KSR.pv.get("$xavp(callee_reseller_prefs=>dummy)"),"callee") end function TestNGCP:test_log_pref() @@ -659,7 +681,7 @@ TestNGCP = {} --class end function TestNGCP:test_tostring() - lu.assertEquals(tostring(self.ngcp), 'caller_contract_prefs:{dummy={"caller"}}\ncallee_contract_prefs:{dummy={"callee"}}\ncaller_peer_prefs:{dummy={"caller"}}\ncallee_peer_prefs:{dummy={"callee"}}\ncaller_dom_prefs:{dummy={"caller"}}\ncallee_dom_prefs:{dummy={"callee"}}\ncaller_prof_prefs:{dummy={"caller"}}\ncallee_prof_prefs:{dummy={"callee"}}\ncaller_fax_prefs:{dummy={"caller"}}\ncallee_fax_prefs:{dummy={"callee"}}\ncaller_usr_prefs:{dummy={"caller"}}\ncallee_usr_prefs:{dummy={"callee"}}\ncaller_real_prefs:{dummy={"caller"}}\ncallee_real_prefs:{dummy={"callee"}}\n') + lu.assertEquals(out, tostring(self.ngcp)) end -- class TestNGCP --EOF diff --git a/tests/ngcp_rep.lua b/tests/ngcp_rep.lua new file mode 100644 index 0000000..860c4cc --- /dev/null +++ b/tests/ngcp_rep.lua @@ -0,0 +1,230 @@ +-- +-- Copyright 2013-2023 SipWise Team +-- +-- 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 package 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 . +-- . +-- On Debian systems, the complete text of the GNU General +-- Public License version 3 can be found in "/usr/share/common-licenses/GPL-3". +-- +local lu = require('luaunit') +local lemock = require('lemock') +local utils = require 'ngcp.utils' +local utable = utils.table +local REPFetch = require 'tests_v.rep_vars' + + +local ksrMock = require 'mocks.ksr' +KSR = ksrMock:new() + +local mc,env,con +local rep_vars = REPFetch:new() + +package.loaded.luasql = nil +package.preload['luasql.mysql'] = function () + local luasql = {} + luasql.mysql = function () + return env + end +end +local NGCPConfig = require 'ngcp.config' +local NGCPResellerPrefs = require 'ngcp.rep' +-- luacheck: ignore TestNGCPResellerPrefs +TestNGCPResellerPrefs = {} --class + + function TestNGCPResellerPrefs:setUp() + mc = lemock.controller() + env = mc:mock() + con = mc:mock() + self.cur = mc:mock() + + package.loaded.luasql = nil + package.preload['luasql.mysql'] = function () + local luasql = {} + luasql.mysql = function () + return env + end + end + + self.config = NGCPConfig:new() + self.config.env = env + self.config.getDBConnection = function () + return con + end + + self.d = NGCPResellerPrefs:new(self.config) + rep_vars:reset() + end + + function TestNGCPResellerPrefs:tearDown() + KSR.pv.vars = {} + end + + function TestNGCPResellerPrefs:test_init() + --print("TestNGCPResellerPrefs:test_init") + lu.assertEquals(self.d.db_table, "reseller_preferences") + end + + function TestNGCPResellerPrefs:get_defaults(level, set) + local keys_expected = {} + local defaults = self.d.config:get_defaults('reseller') + + if set then + keys_expected = utable.deepcopy(set) + for _,v in pairs(keys_expected) do + KSR.log("dbg", string.format("removed key:%s is been loaded.", v)) + defaults[v] = nil + end + end + + for k,v in pairs(defaults) do + utable.add(keys_expected, k) + lu.assertEquals(KSR.pv.get("$xavp("..level.."_reseller_prefs=>"..k..")"), v) + end + return keys_expected + end + + function TestNGCPResellerPrefs:test_caller_load_empty() + lu.assertEvalToTrue(self.d.config) + mc:replay() + lu.assertEquals(self.d:caller_load(), {}) + lu.assertEquals(self.d:caller_load(''), {}) + mc:verify() + end + + function TestNGCPResellerPrefs:test_callee_load_empty() + lu.assertEvalToTrue(self.d.config) + mc:replay() + lu.assertEquals(self.d:callee_load(), {}) + lu.assertEquals(self.d:callee_load(''), {}) + mc:verify() + end + + function TestNGCPResellerPrefs:test_caller_load() + lu.assertEvalToTrue(self.d.config) + con:execute("SELECT * FROM reseller_preferences WHERE uuid = '1'") ;mc :returns(self.cur) + self.cur:fetch(mc.ANYARGS) ;mc :returns(rep_vars:val("r_1")) + self.cur:fetch(mc.ANYARGS) ;mc :returns(rep_vars:val("r_1")) + self.cur:fetch(mc.ANYARGS) ;mc :returns(nil) + self.cur:close() + + mc:replay() + local keys = self.d:caller_load("1") + mc:verify() + + local lkeys = { + "concurrent_max_in", + "concurrent_max_out" + } + + lu.assertItemsEquals(keys, lkeys) + lu.assertEquals(KSR.pv.get("$xavp(caller_reseller_prefs=>dummy)"), "caller") + lu.assertEquals(KSR.pv.get("$xavp(caller_reseller_prefs=>concurrent_max_in)"), 10) + lu.assertEquals(KSR.pv.get("$xavp(caller_reseller_prefs=>concurrent_max_out)"), 5) + end + + function TestNGCPResellerPrefs:test_callee_load() + lu.assertEvalToTrue(self.d.config) + con:execute("SELECT * FROM reseller_preferences WHERE uuid = '2'") ;mc :returns(self.cur) + self.cur:fetch(mc.ANYARGS) ;mc :returns(rep_vars:val("r_2")) + self.cur:fetch(mc.ANYARGS) ;mc :returns(rep_vars:val("r_2")) + self.cur:fetch(mc.ANYARGS) ;mc :returns(rep_vars:val("r_2")) + self.cur:fetch(mc.ANYARGS) ;mc :returns(nil) + self.cur:close() + + mc:replay() + local keys = self.d:callee_load("2") + mc:verify() + + local lkeys = { + "concurrent_max", + "concurrent_max_in", + "concurrent_max_out" + } + + lu.assertItemsEquals(keys, lkeys) + lu.assertEquals(KSR.pv.get("$xavp(callee_reseller_prefs=>dummy)"), "callee") + lu.assertEquals(KSR.pv.get("$xavp(callee_reseller_prefs=>concurrent_max)"), 5) + lu.assertEquals(KSR.pv.get("$xavp(callee_reseller_prefs=>concurrent_max_in)"), 5) + lu.assertEquals(KSR.pv.get("$xavp(callee_reseller_prefs=>concurrent_max_out)"), 2) + end + + function TestNGCPResellerPrefs:test_clean() + local xavp = NGCPResellerPrefs:xavp('callee') + xavp("testid",1) + xavp("foo","foo") + lu.assertEquals(KSR.pv.get("$xavp(callee_reseller_prefs=>testid)"),1) + lu.assertEquals(KSR.pv.get("$xavp(callee_reseller_prefs=>foo)"),"foo") + lu.assertEquals(KSR.pv.get("$xavp(caller_reseller_prefs=>dummy)"),"caller") + lu.assertEquals(KSR.pv.get("$xavp(callee_reseller_prefs=>dummy)"),"callee") + self.d:clean() + lu.assertEquals(KSR.pv.get("$xavp(caller_reseller_prefs=>dummy)"),"caller") + lu.assertEquals(KSR.pv.get("$xavp(callee_reseller_prefs=>dummy)"),"callee") + lu.assertNil(KSR.pv.get("$xavp(reseller)")) + end + + function TestNGCPResellerPrefs:test_callee_clean() + local callee_xavp = NGCPResellerPrefs:xavp('callee') + callee_xavp("testid",1) + callee_xavp("foo","foo") + local caller_xavp = NGCPResellerPrefs:xavp('caller') + caller_xavp("other",1) + caller_xavp("otherfoo","foo") + lu.assertEquals(KSR.pv.get("$xavp(callee_reseller_prefs=>testid)"),1) + lu.assertEquals(KSR.pv.get("$xavp(callee_reseller_prefs=>foo)"),"foo") + lu.assertEquals(KSR.pv.get("$xavp(caller_reseller_prefs=>dummy)"),"caller") + lu.assertEquals(KSR.pv.get("$xavp(caller_reseller_prefs=>other)"),1) + lu.assertEquals(KSR.pv.get("$xavp(caller_reseller_prefs=>otherfoo)"),"foo") + lu.assertEquals(KSR.pv.get("$xavp(callee_reseller_prefs=>dummy)"),"callee") + self.d:clean('callee') + lu.assertEquals(KSR.pv.get("$xavp(caller_reseller_prefs=>dummy)"),'caller') + lu.assertNil(KSR.pv.get("$xavp(callee_reseller_prefs=>testid)")) + lu.assertNil(KSR.pv.get("$xavp(callee_reseller_prefs=>foo)")) + lu.assertEquals(KSR.pv.get("$xavp(caller_reseller_prefs=>other)"),1) + lu.assertEquals(KSR.pv.get("$xavp(caller_reseller_prefs=>otherfoo)"),"foo") + lu.assertEquals(KSR.pv.get("$xavp(callee_reseller_prefs=>dummy)"),"callee") + end + + function TestNGCPResellerPrefs:test_caller_clean() + local callee_xavp = NGCPResellerPrefs:xavp('callee') + callee_xavp("testid",1) + callee_xavp("foo","foo") + local caller_xavp = NGCPResellerPrefs:xavp('caller') + caller_xavp("other",1) + caller_xavp("otherfoo","foo") + lu.assertEquals(KSR.pv.get("$xavp(callee_reseller_prefs=>testid)"),1) + lu.assertEquals(KSR.pv.get("$xavp(callee_reseller_prefs=>foo)"),"foo") + lu.assertEquals(KSR.pv.get("$xavp(caller_reseller_prefs=>dummy)"),"caller") + lu.assertEquals(KSR.pv.get("$xavp(caller_reseller_prefs=>other)"),1) + lu.assertEquals(KSR.pv.get("$xavp(caller_reseller_prefs=>otherfoo)"),"foo") + lu.assertEquals(KSR.pv.get("$xavp(callee_reseller_prefs=>dummy)"),"callee") + self.d:clean('caller') + lu.assertEquals(KSR.pv.get("$xavp(caller_reseller_prefs=>dummy)"),"caller") + lu.assertNil(KSR.pv.get("$xavp(caller_reseller_prefs=>other)")) + lu.assertNil(KSR.pv.get("$xavp(caller_reseller_prefs=>otherfoo)")) + lu.assertEquals(KSR.pv.get("$xavp(callee_reseller_prefs=>testid)"),1) + lu.assertEquals(KSR.pv.get("$xavp(callee_reseller_prefs=>foo)"),"foo") + lu.assertEquals(KSR.pv.get("$xavp(callee_reseller_prefs=>dummy)"),"callee") + end + + function TestNGCPResellerPrefs:test_tostring() + local callee_xavp = NGCPResellerPrefs:xavp('callee') + callee_xavp("testid",1) + callee_xavp("foo","foo") + local caller_xavp = NGCPResellerPrefs:xavp('caller') + caller_xavp("other",1) + caller_xavp("otherfoo","foo") + lu.assertEquals(tostring(self.d), 'caller_reseller_prefs:{other={1},otherfoo={"foo"},dummy={"caller"}}\ncallee_reseller_prefs:{dummy={"callee"},testid={1},foo={"foo"}}\n') + end +-- class TestNGCPResellerPrefs +--EOF diff --git a/tests_v/rep_vars.lua b/tests_v/rep_vars.lua new file mode 100644 index 0000000..64ce9b2 --- /dev/null +++ b/tests_v/rep_vars.lua @@ -0,0 +1,95 @@ +-- +-- Copyright 2013 SipWise Team +-- +-- 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 package 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 . +-- . +-- On Debian systems, the complete text of the GNU General +-- Public License version 3 can be found in "/usr/share/common-licenses/GPL-3". +-- +local rep_vars = { + r_1 = { + { + id = 1, + uuid = "1", + username = "0", + domain = nil, + attribute = "concurrent_max_out", + type = 1, + value = "5", + last_modified = "0000-00-00 00:00:00" + }, + { + id = 2, + uuid = "1", + username = "0", + domain = nil, + attribute = "concurrent_max_in", + type = 1, + value = "10", + last_modified = "0000-00-00 00:00:00" + }, + }, + r_2 = { + { + id = 3, + uuid = "2", + username = "0", + domain = nil, + attribute = "concurrent_max_out", + type = 1, + value = "2", + last_modified = "0000-00-00 00:00:00" + }, + { + id = 4, + uuid = "2", + username = "0", + domain = nil, + attribute = "concurrent_max_in", + type = 1, + value = "5", + last_modified = "0000-00-00 00:00:00" + }, + { + id = 5, + uuid = "2", + username = "0", + domain = nil, + attribute = "concurrent_max", + type = 1, + value = "5", + last_modified = "0000-00-00 00:00:00" + }, + } +} + +local REPFetch = { + __class__ = 'REPFetch', + _i = 1 +} + function REPFetch:new() + local t = {} + return setmetatable(t, { __index = REPFetch }) + end + + function REPFetch:val(uuid) + self._i = self._i + 1 + return rep_vars[uuid][self._i-1] + end + + function REPFetch:reset() + self._i = 1 + end + +return REPFetch