blob: e875c43be2701b18e74d62b58076198a8b1ca0f0 [file] [log] [blame]
Matteo Scandoload0c1752018-08-09 15:47:16 -07001# Copyright 2017-present Open Networking Foundation
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15import unittest
Scott Bakerc2a633d2019-04-01 19:27:41 -070016from mock import patch, Mock
Matteo Scandoload0c1752018-08-09 15:47:16 -070017import json
18
Scott Bakerc2a633d2019-04-01 19:27:41 -070019import os
20import sys
Matteo Scandoload0c1752018-08-09 15:47:16 -070021
Scott Bakerc2a633d2019-04-01 19:27:41 -070022test_path = os.path.abspath(os.path.dirname(os.path.realpath(__file__)))
23
Matteo Scandoload0c1752018-08-09 15:47:16 -070024
25class TestSubscriberAuthEvent(unittest.TestCase):
26
27 def setUp(self):
28
29 self.sys_path_save = sys.path
Matteo Scandoload0c1752018-08-09 15:47:16 -070030
31 # Setting up the config module
32 from xosconfig import Config
33 config = os.path.join(test_path, "../test_config.yaml")
34 Config.clear()
35 Config.init(config, "synchronizer-config-schema.yaml")
36 from multistructlog import create_logger
37 log = create_logger(Config().get('logging'))
38 # END Setting up the config module
39
Scott Baker71d20472019-02-01 12:05:35 -080040 from xossynchronizer.mock_modelaccessor_build import mock_modelaccessor_config
41 mock_modelaccessor_config(test_path, [("att-workflow-driver", "att-workflow-driver.xproto"),
42 ("olt-service", "volt.xproto"),
Matteo Scandoload6c8942019-02-14 13:02:08 -080043 ("rcord", "rcord.xproto")])
Matteo Scandoload0c1752018-08-09 15:47:16 -070044
Scott Baker71d20472019-02-01 12:05:35 -080045 import xossynchronizer.modelaccessor
46 import mock_modelaccessor
Scott Bakerc2a633d2019-04-01 19:27:41 -070047 reload(mock_modelaccessor) # in case nose2 loaded it in a previous test
Scott Baker71d20472019-02-01 12:05:35 -080048 reload(xossynchronizer.modelaccessor) # in case nose2 loaded it in a previous test
49
50 from xossynchronizer.modelaccessor import model_accessor
51 from auth_event import SubscriberAuthEventStep
Matteo Scandoload0c1752018-08-09 15:47:16 -070052
53 # import all class names to globals
54 for (k, v) in model_accessor.all_model_classes.items():
55 globals()[k] = v
56
Scott Baker71d20472019-02-01 12:05:35 -080057 self.model_accessor = model_accessor
Matteo Scandoload0c1752018-08-09 15:47:16 -070058 self.log = log
59
Scott Baker71d20472019-02-01 12:05:35 -080060 self.event_step = SubscriberAuthEventStep(model_accessor=self.model_accessor, log=self.log)
Matteo Scandoload0c1752018-08-09 15:47:16 -070061
62 self.event = Mock()
63
64 self.volt = Mock()
65 self.volt.name = "vOLT"
66 self.volt.leaf_model = Mock()
67
68 self.hippie_si = AttWorkflowDriverServiceInstance()
69 self.hippie_si.serial_number = "BRCM1234"
70 self.hippie_si.save = Mock()
71
Matteo Scandoload0c1752018-08-09 15:47:16 -070072 def tearDown(self):
73 sys.path = self.sys_path_save
74
75 def test_authenticate_subscriber(self):
76
77 self.event.value = json.dumps({
78 'authenticationState': "APPROVED",
79 'deviceId': "of:0000000ce2314000",
80 'portNumber': "101",
81 })
82
83 with patch.object(VOLTService.objects, "get_items") as volt_service_mock, \
Scott Bakerc2a633d2019-04-01 19:27:41 -070084 patch.object(AttWorkflowDriverServiceInstance.objects, "get_items") as hippie_si_mock, \
85 patch.object(self.volt, "get_onu_sn_from_openflow") as get_onu_sn:
Matteo Scandoload0c1752018-08-09 15:47:16 -070086
87 volt_service_mock.return_value = [self.volt]
88 get_onu_sn.return_value = "BRCM1234"
89 hippie_si_mock.return_value = [self.hippie_si]
90
91 self.event_step.process_event(self.event)
92
Scott Bakerc2a633d2019-04-01 19:27:41 -070093 self.hippie_si.save.assert_called_with(
94 always_update_timestamp=True, update_fields=[
95 'authentication_state', 'serial_number', 'updated'])
Matteo Scandoload0c1752018-08-09 15:47:16 -070096 self.assertEqual(self.hippie_si.authentication_state, 'APPROVED')
Scott Baker71d20472019-02-01 12:05:35 -080097
Scott Bakerc2a633d2019-04-01 19:27:41 -070098
Scott Baker71d20472019-02-01 12:05:35 -080099if __name__ == '__main__':
Scott Bakerc2a633d2019-04-01 19:27:41 -0700100 sys.path.append("..") # for import of helpers.py
101 unittest.main()