Takahiro Suzuki | 2b66b94 | 2020-12-17 11:58:14 +0900 | [diff] [blame] | 1 | # Copyright 2020-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 |
| 16 | from mock import patch, Mock |
| 17 | import json |
| 18 | |
| 19 | import os |
| 20 | import sys |
| 21 | |
| 22 | test_path = os.path.abspath(os.path.dirname(os.path.realpath(__file__))) |
| 23 | |
| 24 | |
| 25 | class TestSubscriberAuthEvent(unittest.TestCase): |
| 26 | |
| 27 | def setUp(self): |
| 28 | |
| 29 | self.sys_path_save = sys.path |
| 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 | |
| 40 | from xossynchronizer.mock_modelaccessor_build import mock_modelaccessor_config |
| 41 | mock_modelaccessor_config(test_path, [("ntt-workflow-driver", "ntt-workflow-driver.xproto"), |
| 42 | ("olt-service", "volt.xproto"), |
| 43 | ("rcord", "rcord.xproto")]) |
| 44 | |
| 45 | import xossynchronizer.modelaccessor |
| 46 | import mock_modelaccessor |
| 47 | reload(mock_modelaccessor) # in case nose2 loaded it in a previous test |
| 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 |
| 52 | |
| 53 | # import all class names to globals |
| 54 | for (k, v) in model_accessor.all_model_classes.items(): |
| 55 | globals()[k] = v |
| 56 | |
| 57 | self.model_accessor = model_accessor |
| 58 | self.log = log |
| 59 | |
| 60 | self.event_step = SubscriberAuthEventStep(model_accessor=self.model_accessor, log=self.log) |
| 61 | |
| 62 | self.event = Mock() |
| 63 | |
| 64 | self.ntt_si = NttWorkflowDriverServiceInstance() |
| 65 | self.ntt_si.serial_number = "BRCM1234" |
| 66 | self.ntt_si.save = Mock() |
| 67 | |
| 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", |
| 77 | 'serialNumber': "BRCM1234", |
| 78 | }) |
| 79 | |
| 80 | with patch.object(NttWorkflowDriverServiceInstance.objects, "get_items") as ntt_si_mock: |
| 81 | |
| 82 | ntt_si_mock.return_value = [self.ntt_si] |
| 83 | |
| 84 | self.event_step.process_event(self.event) |
| 85 | |
| 86 | self.ntt_si.save.assert_called() |
| 87 | self.ntt_si.save.assert_called_with( |
| 88 | always_update_timestamp=True, update_fields=[ |
| 89 | 'authentication_state', 'serial_number', 'updated']) |
| 90 | self.assertEqual(self.ntt_si.authentication_state, 'APPROVED') |
| 91 | |
| 92 | |
| 93 | if __name__ == '__main__': |
| 94 | sys.path.append(os.path.join(os.path.dirname(os.path.realpath(__file__)), "..")) # for import of helpers.py |
| 95 | unittest.main() |