Matteo Scandolo | ad0c175 | 2018-08-09 15:47:16 -0700 | [diff] [blame] | 1 | # 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 | |
| 15 | import unittest |
Scott Baker | c2a633d | 2019-04-01 19:27:41 -0700 | [diff] [blame] | 16 | from mock import patch, Mock |
Matteo Scandolo | ad0c175 | 2018-08-09 15:47:16 -0700 | [diff] [blame] | 17 | import json |
| 18 | |
Scott Baker | c2a633d | 2019-04-01 19:27:41 -0700 | [diff] [blame] | 19 | import os |
| 20 | import sys |
Matteo Scandolo | ad0c175 | 2018-08-09 15:47:16 -0700 | [diff] [blame] | 21 | |
Scott Baker | c2a633d | 2019-04-01 19:27:41 -0700 | [diff] [blame] | 22 | test_path = os.path.abspath(os.path.dirname(os.path.realpath(__file__))) |
| 23 | |
Matteo Scandolo | ad0c175 | 2018-08-09 15:47:16 -0700 | [diff] [blame] | 24 | |
| 25 | class TestSubscriberAuthEvent(unittest.TestCase): |
| 26 | |
| 27 | def setUp(self): |
| 28 | |
| 29 | self.sys_path_save = sys.path |
Matteo Scandolo | ad0c175 | 2018-08-09 15:47:16 -0700 | [diff] [blame] | 30 | |
| 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 Baker | 71d2047 | 2019-02-01 12:05:35 -0800 | [diff] [blame] | 40 | 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 Scandolo | ad6c894 | 2019-02-14 13:02:08 -0800 | [diff] [blame] | 43 | ("rcord", "rcord.xproto")]) |
Matteo Scandolo | ad0c175 | 2018-08-09 15:47:16 -0700 | [diff] [blame] | 44 | |
Scott Baker | 71d2047 | 2019-02-01 12:05:35 -0800 | [diff] [blame] | 45 | import xossynchronizer.modelaccessor |
| 46 | import mock_modelaccessor |
Scott Baker | c2a633d | 2019-04-01 19:27:41 -0700 | [diff] [blame] | 47 | reload(mock_modelaccessor) # in case nose2 loaded it in a previous test |
Scott Baker | 71d2047 | 2019-02-01 12:05:35 -0800 | [diff] [blame] | 48 | 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 Scandolo | ad0c175 | 2018-08-09 15:47:16 -0700 | [diff] [blame] | 52 | |
| 53 | # import all class names to globals |
| 54 | for (k, v) in model_accessor.all_model_classes.items(): |
| 55 | globals()[k] = v |
| 56 | |
Scott Baker | 71d2047 | 2019-02-01 12:05:35 -0800 | [diff] [blame] | 57 | self.model_accessor = model_accessor |
Matteo Scandolo | ad0c175 | 2018-08-09 15:47:16 -0700 | [diff] [blame] | 58 | self.log = log |
| 59 | |
Scott Baker | 71d2047 | 2019-02-01 12:05:35 -0800 | [diff] [blame] | 60 | self.event_step = SubscriberAuthEventStep(model_accessor=self.model_accessor, log=self.log) |
Matteo Scandolo | ad0c175 | 2018-08-09 15:47:16 -0700 | [diff] [blame] | 61 | |
| 62 | self.event = Mock() |
| 63 | |
Matteo Scandolo | 59e10fc | 2019-04-18 14:19:52 -0700 | [diff] [blame] | 64 | self.att_si = AttWorkflowDriverServiceInstance() |
| 65 | self.att_si.serial_number = "BRCM1234" |
| 66 | self.att_si.save = Mock() |
Matteo Scandolo | ad0c175 | 2018-08-09 15:47:16 -0700 | [diff] [blame] | 67 | |
Matteo Scandolo | ad0c175 | 2018-08-09 15:47:16 -0700 | [diff] [blame] | 68 | def tearDown(self): |
| 69 | sys.path = self.sys_path_save |
| 70 | |
| 71 | def test_authenticate_subscriber(self): |
| 72 | |
| 73 | self.event.value = json.dumps({ |
| 74 | 'authenticationState': "APPROVED", |
| 75 | 'deviceId': "of:0000000ce2314000", |
| 76 | 'portNumber': "101", |
Matteo Scandolo | 59e10fc | 2019-04-18 14:19:52 -0700 | [diff] [blame] | 77 | 'serialNumber': "BRCM1234", |
Matteo Scandolo | ad0c175 | 2018-08-09 15:47:16 -0700 | [diff] [blame] | 78 | }) |
| 79 | |
Matteo Scandolo | 59e10fc | 2019-04-18 14:19:52 -0700 | [diff] [blame] | 80 | with patch.object(AttWorkflowDriverServiceInstance.objects, "get_items") as att_si_mock: |
Matteo Scandolo | ad0c175 | 2018-08-09 15:47:16 -0700 | [diff] [blame] | 81 | |
Matteo Scandolo | 59e10fc | 2019-04-18 14:19:52 -0700 | [diff] [blame] | 82 | att_si_mock.return_value = [self.att_si] |
Matteo Scandolo | ad0c175 | 2018-08-09 15:47:16 -0700 | [diff] [blame] | 83 | |
| 84 | self.event_step.process_event(self.event) |
| 85 | |
Matteo Scandolo | 59e10fc | 2019-04-18 14:19:52 -0700 | [diff] [blame] | 86 | self.att_si.save.assert_called() |
| 87 | self.att_si.save.assert_called_with( |
Scott Baker | c2a633d | 2019-04-01 19:27:41 -0700 | [diff] [blame] | 88 | always_update_timestamp=True, update_fields=[ |
| 89 | 'authentication_state', 'serial_number', 'updated']) |
Matteo Scandolo | 59e10fc | 2019-04-18 14:19:52 -0700 | [diff] [blame] | 90 | self.assertEqual(self.att_si.authentication_state, 'APPROVED') |
Scott Baker | 71d2047 | 2019-02-01 12:05:35 -0800 | [diff] [blame] | 91 | |
Scott Baker | c2a633d | 2019-04-01 19:27:41 -0700 | [diff] [blame] | 92 | |
Scott Baker | 71d2047 | 2019-02-01 12:05:35 -0800 | [diff] [blame] | 93 | if __name__ == '__main__': |
Matteo Scandolo | 59e10fc | 2019-04-18 14:19:52 -0700 | [diff] [blame] | 94 | sys.path.append(os.path.join(os.path.dirname(os.path.realpath(__file__)), "..")) # for import of helpers.py |
Scott Baker | c2a633d | 2019-04-01 19:27:41 -0700 | [diff] [blame] | 95 | unittest.main() |