Esempio n. 1
0
 def test_missing_component_no_exception(self):
     """If a component no longer exists at the path to receive the notification, no error should occur."""
     v = ViewState(MagicMock())
     n = NotificationCentre(v)
     v.component_from_path = MagicMock(return_value=None)
     n.subscribe_to_notification('test_notification', 'page.test_component')
     n.post_notification('test_notification')
Esempio n. 2
0
    def test_viewstate_cannot_be_set(self):
        """ViewState is read only and can not be set once the NC is instantiated."""
        v = ViewState(MagicMock())
        n = NotificationCentre(v)
        new_v = ViewState(MagicMock())

        with self.assertRaises(AttributeError):
            n.view_state = new_v
Esempio n. 3
0
 def test_post_setup_root_attach(self):
     """The root's post_attach method should not be called until VS's post_setup is called so that the NC has
     been able to be set up and everything has a path."""
     root_controller = BaseViewController()
     root_controller.post_attach = MagicMock()
     vs = ViewState(root_controller)
     root_controller.post_attach.assert_not_called()
     vs.post_setup()
     root_controller.post_attach.assert_called_with()
Esempio n. 4
0
    def setUp(self):
        self.root_controller = BaseViewController()
        self.child_one = BaseViewController()
        self.child_two = BaseViewController()
        self.child_three = BaseViewController()

        self.vs = ViewState(self.root_controller)
        self.root_controller.set_named_child('one', self.child_one)
        self.child_one.set_named_child('two', self.child_two)
        self.child_two.set_named_child('three', self.child_three)
Esempio n. 5
0
    def test_notification_unsubscribe_all(self):
        """nc.unsubscribe_from_all_notifications should prevent any more calls going to the component."""
        v = ViewState(MagicMock())
        n = NotificationCentre(v)
        component = MagicMock()
        v.component_from_path = MagicMock(return_value=component)

        n.subscribe_to_notification('test_notification', 'page.test_component')
        n.subscribe_to_notification('test_notification2', 'page.test_component')
        n.unsubscribe_from_all_notifications('page.test_component')
        n.post_notification('test_notification')
        n.post_notification('test_notification2')
        self.assertEqual(component.process_notification.call_count, 0)
Esempio n. 6
0
    def test_global_notification_subscription_and_receive(self):
        """A component that subscribes to a notification with a blank source, should receive that notification no matter
        what source posts it."""
        v = ViewState(MagicMock())
        n = NotificationCentre(v)
        component = MagicMock()
        v.component_from_path = MagicMock(return_value=component)

        n.subscribe_to_notification('test_notification', 'page.test_component')
        n.post_notification('test_notification')
        component.process_notification.assert_called_with('test_notification', None)

        n.subscribe_to_notification('test_notification2', 'page.test_component')
        n.post_notification('test_notification2', 'page.another_component')
        component.process_notification.assert_called_with('test_notification2', None)
Esempio n. 7
0
    def test_specific_notification_subscription_and_receive(self):
        """A component can listen to events from only single sources, so shouldn't receive that notification if posted
        from elsewhere."""
        v = ViewState(MagicMock())
        n = NotificationCentre(v)
        component = MagicMock()
        v.component_from_path = MagicMock(return_value=component)

        n.subscribe_to_notification('test_notification', 'page.test_component', 'page.source_component')
        n.post_notification('test_notification')
        n.post_notification('test_notification', 'page.another_source')
        self.assertEqual(component.process_notification.call_count, 0)

        n.post_notification('test_notification', 'page.source_component')
        component.process_notification.assert_called_with('test_notification', None)
Esempio n. 8
0
    def test_notification_unsubscribe(self):
        """After a component unsubscribes, it should not receive that notification any more (but should still receive
        others that it is subscribed to)."""
        v = ViewState(MagicMock())
        n = NotificationCentre(v)
        component = MagicMock()
        v.component_from_path = MagicMock(return_value=component)

        n.subscribe_to_notification('test_notification', 'page.test_component')
        n.subscribe_to_notification('test_notification2', 'page.test_component')
        n.unsubscribe_from_notification('test_notification', 'page.test_component')
        n.post_notification('test_notification')
        self.assertEqual(component.process_notification.call_count, 0)
        n.post_notification('test_notification2')
        component.process_notification.assert_called_with('test_notification2', None)
Esempio n. 9
0
    def test_force_same_notification_multiple_queue(self):
        """The same notification will be queued twice if the force arg is True."""
        v = ViewState(MagicMock())
        n = NotificationCentre(v)

        n.queue_client_notification('test_name', 'page.test_component')
        n.queue_client_notification('test_name', 'page.test_component', force=True)
        queued_notifications = [notification for notification in n]
        self.assertEqual(len(queued_notifications), 2)
        self.assertEqual(queued_notifications[0], queued_notifications[1])
Esempio n. 10
0
    def setUp(self):
        self.root_controller = BaseViewController()
        self.child_one = BaseViewController()
        self.child_two = BaseViewController()
        self.child_three = BaseViewController()

        self.vs = ViewState(self.root_controller)
        self.root_controller.set_named_child('one', self.child_one)
        self.child_one.set_named_child('two', self.child_two)
        self.child_two.set_named_child('three', self.child_three)
Esempio n. 11
0
    def test_client_notification_queue_and_retrieve(self):
        """Client notifications are queued and retrieved in FIFO order."""
        v = ViewState(MagicMock())
        n = NotificationCentre(v)

        n.queue_client_notification('test_name', 'page.test_component')
        n.queue_client_notification('test_name2', 'page.test_component2', 'somedata')

        # notifications are retrieved FIFO
        queued_notifications = [notification for notification in n]
        self.assertEqual(len(queued_notifications), 2)
        self.assertEqual({'name': 'test_name', 'path': 'page.test_component'}, queued_notifications[0])
        self.assertEqual({'name': 'test_name2', 'path': 'page.test_component2', 'data': 'somedata'},
                         queued_notifications[1])

        # queue should now be empty
        self.assertEqual(len([notification for notification in n]), 0)
Esempio n. 12
0
    def test_same_notification_multiple_queue(self):
        """The same notification will not be queued twice, however if any of name, path or data differs, it will."""
        v = ViewState(MagicMock())
        n = NotificationCentre(v)

        n.queue_client_notification('test_name', 'page.test_component')
        n.queue_client_notification('test_name', 'page.test_component')
        self.assertEqual(len([notification for notification in n]), 1)

        n.queue_client_notification('test_name', 'page.test_component')
        n.queue_client_notification('test_name', 'page.test_component', 'somedata')
        self.assertEqual(len([notification for notification in n]), 2)

        n.queue_client_notification('test_name', 'page.test_component2')
        n.queue_client_notification('test_name', 'page.test_component')
        self.assertEqual(len([notification for notification in n]), 2)

        n.queue_client_notification('test_name', 'page.test_component')
        n.queue_client_notification('test_name2', 'page.test_component')
        self.assertEqual(len([notification for notification in n]), 2)
        'ERROR: Input needed - Usage: viewstate-decoder.py [-h] [-vs ENCODED_VS] [-f VS_FILENAME] [-o OUTPUT_FILENAME]'
    )
else:
    # Check if the input is given in the command line
    if (args.encoded_vs):
        encoded_vs = args.encoded_vs
    else:  # If not, we have the input inside a file with name: args.vs_filename
        input_file = open(args.vs_filename, 'r')
        encoded_vs = input_file.read()
        input_file.close()

        # Convert the URL encoded input into Base64 only (URL decode)
        encoded_vs = urllib.parse.unquote(encoded_vs)

    #Create ViewState object
    vs = ViewState(encoded_vs)
    decoded_vs = vs.decode()
    hmac = vs.mac
    sign = vs.signature

    #Write to output file
    output_file = open(args.output_filename, 'w')
    output_file.write(
        'Decoded Viewstate: ' + str(decoded_vs)
    )  # We must set it as 'string' to write it because it's a tuple, not a string
    output_file.write('\n ViewState HMAC Signature Type: ' + hmac)
    output_file.write('\n ViewState HMAC Signature: ' + str(sign))
    output_file.close()

    print('Output file saved to: ' + args.output_filename)
Esempio n. 14
0
class TestViewStateClass(unittest.TestCase):
    def setUp(self):
        self.root_controller = BaseViewController()
        self.child_one = BaseViewController()
        self.child_two = BaseViewController()
        self.child_three = BaseViewController()

        self.vs = ViewState(self.root_controller)
        self.root_controller.set_named_child('one', self.child_one)
        self.child_one.set_named_child('two', self.child_two)
        self.child_two.set_named_child('three', self.child_three)

    def test_view_state_init(self):
        """A ViewState can only be inited with a root controller arg."""
        with self.assertRaises(TypeError):
            ViewState()

    def test_root_view_set(self):
        """Initing the ViewState sets the root controller to the root_controller attribute."""
        root_controller = MagicMock()
        vs = ViewState(root_controller)
        self.assertEqual(vs.root_controller, root_controller)

    def test_component_path_retrieval(self):
        """A component can be retrieved from the tree by its path."""
        self.assertEqual(self.vs.controller_from_path('page'), self.root_controller)
        self.assertEqual(self.vs.controller_from_path('page.one'), self.child_one)
        self.assertEqual(self.vs.controller_from_path('page.one.two'), self.child_two)
        self.assertEqual(self.vs.controller_from_path('page.one.two.three'), self.child_three)

    def test_root_component_insert_error(self):
        """Inserting a component at path 'page' is invalid and raises a ValueError."""
        new_component = BaseViewController
        self.assertRaises(ValueError, self.vs.insert_controller, 'page', new_component)

    def test_component_path_insert(self):
        """A component can be inserted directly using a path, replacing the existing stack there."""
        new_component = BaseViewController()
        self.child_one.set_named_child = MagicMock()
        self.vs.insert_controller('page.one.four', new_component)
        self.child_one.set_named_child.assert_called_with('four', new_component)

    def test_new_component_path_insert(self):
        """A new controller can be instantiated (based on component name) and then can be inserted directly using a view
        path, replacing the existing stack there."""
        new_component = MagicMock()
        self.vs.insert_controller = MagicMock()
        with patch('viewstate.viewstate.init_controller', return_value=new_component) as patched_init:
            self.vs.insert_new_controller('page.one.four', 'new.component.path', 'arg1', 'arg2', kwarg1='kwarg1',
                                         kwarg2='kwarg2')
            patched_init.assert_called_with('new.component.path', 'arg1', 'arg2', kwarg1='kwarg1', kwarg2='kwarg2')
            self.vs.insert_controller.assert_called_with('page.one.four', new_component)

    def test_component_path_pop(self):
        """A component can be popped from the stack by giving its path to the ViewState."""
        # tree looks like this -> page.one.two.three
        self.child_two.pop_named_child = MagicMock(return_value=self.child_three)
        self.child_one.pop_named_child = MagicMock(return_value=self.child_two)

        child_three = self.vs.pop_controller('page.one.two.three')
        child_two = self.vs.pop_controller('page.one.two')

        self.assertEqual(self.child_three, child_three)
        self.assertEqual(self.child_two, child_two)
        self.child_two.pop_named_child.assert_called_with('three')
        self.child_one.pop_named_child.assert_called_with('two')

    def test_page_pop_fails(self):
        """ValueError is raised when trying to pop the root (page)."""
        self.assertRaises(ValueError, self.vs.pop_controller, 'page')
Esempio n. 15
0
class TestViewStateClass(unittest.TestCase):
    def setUp(self):
        self.root_controller = BaseViewController()
        self.child_one = BaseViewController()
        self.child_two = BaseViewController()
        self.child_three = BaseViewController()

        self.vs = ViewState(self.root_controller)
        self.root_controller.set_named_child('one', self.child_one)
        self.child_one.set_named_child('two', self.child_two)
        self.child_two.set_named_child('three', self.child_three)

    def test_view_state_init(self):
        """A ViewState can only be inited with a root controller arg."""
        with self.assertRaises(TypeError):
            ViewState()

    def test_root_view_set(self):
        """Initing the ViewState sets the root controller to the root_controller attribute."""
        root_controller = MagicMock()
        vs = ViewState(root_controller)
        self.assertEqual(vs.root_controller, root_controller)

    def test_component_path_retrieval(self):
        """A component can be retrieved from the tree by its path."""
        self.assertEqual(self.vs.controller_from_path('page'),
                         self.root_controller)
        self.assertEqual(self.vs.controller_from_path('page.one'),
                         self.child_one)
        self.assertEqual(self.vs.controller_from_path('page.one.two'),
                         self.child_two)
        self.assertEqual(self.vs.controller_from_path('page.one.two.three'),
                         self.child_three)

    def test_root_component_insert_error(self):
        """Inserting a component at path 'page' is invalid and raises a ValueError."""
        new_component = BaseViewController
        self.assertRaises(ValueError, self.vs.insert_controller, 'page',
                          new_component)

    def test_component_path_insert(self):
        """A component can be inserted directly using a path, replacing the existing stack there."""
        new_component = BaseViewController()
        self.child_one.set_named_child = MagicMock()
        self.vs.insert_controller('page.one.four', new_component)
        self.child_one.set_named_child.assert_called_with(
            'four', new_component)

    def test_new_component_path_insert(self):
        """A new controller can be instantiated (based on component name) and then can be inserted directly using a view
        path, replacing the existing stack there."""
        new_component = MagicMock()
        self.vs.insert_controller = MagicMock()
        with patch('viewstate.viewstate.init_controller',
                   return_value=new_component) as patched_init:
            self.vs.insert_new_controller('page.one.four',
                                          'new.component.path',
                                          'arg1',
                                          'arg2',
                                          kwarg1='kwarg1',
                                          kwarg2='kwarg2')
            patched_init.assert_called_with('new.component.path',
                                            'arg1',
                                            'arg2',
                                            kwarg1='kwarg1',
                                            kwarg2='kwarg2')
            self.vs.insert_controller.assert_called_with(
                'page.one.four', new_component)

    def test_component_path_pop(self):
        """A component can be popped from the stack by giving its path to the ViewState."""
        # tree looks like this -> page.one.two.three
        self.child_two.pop_named_child = MagicMock(
            return_value=self.child_three)
        self.child_one.pop_named_child = MagicMock(return_value=self.child_two)

        child_three = self.vs.pop_controller('page.one.two.three')
        child_two = self.vs.pop_controller('page.one.two')

        self.assertEqual(self.child_three, child_three)
        self.assertEqual(self.child_two, child_two)
        self.child_two.pop_named_child.assert_called_with('three')
        self.child_one.pop_named_child.assert_called_with('two')

    def test_page_pop_fails(self):
        """ValueError is raised when trying to pop the root (page)."""
        self.assertRaises(ValueError, self.vs.pop_controller, 'page')
Esempio n. 16
0
 def test_view_state_init(self):
     """A ViewState can only be inited with a root controller arg."""
     with self.assertRaises(TypeError):
         ViewState()
Esempio n. 17
0
 def test_no_listeners_no_exception(self):
     """If there is no listener for a notification, no error should occur."""
     v = ViewState(MagicMock())
     n = NotificationCentre(v)
     n.post_notification('test_notification')
Esempio n. 18
0
 def test_root_view_set(self):
     """Initing the ViewState sets the root controller to the root_controller attribute."""
     root_controller = MagicMock()
     vs = ViewState(root_controller)
     self.assertEqual(vs.root_controller, root_controller)
Esempio n. 19
0
 def test_symbiotic_relationship(self):
     """v.notification_centre -> nc, and nc.view_state -> vs."""
     v = ViewState(MagicMock())
     n = NotificationCentre(v)
     self.assertEqual(n.view_state, v)
     self.assertEqual(v.notification_centre, n)
Esempio n. 20
0
 def test_viewstate_read(self):
     """ViewState can be read from the NC."""
     v = ViewState(MagicMock())
     n = NotificationCentre(v)
     self.assertEqual(v, n.view_state)
Esempio n. 21
0
from viewstate import ViewState
import urllib.request

vs1 = "/wEPDwUKLTY4MjI1MDA4NA9kFgICAw9kFgICAQ9kFgJmDxYCHgtfIUl0ZW1Db3VudAICFgRmD2QWAgIBDw8WBB4ISW1hZ2VVcmwFIH4vaW1hZ2VzL0xhbmd1YWdlSWNvbnMvc3Ytc2UuZ2lmHg9Db21tYW5kQXJndW1lbnQFBXN2LVNFZGQCAQ9kFgICAQ8PFgQfAQUgfi9pbWFnZXMvTGFuZ3VhZ2VJY29ucy9lbi1nYi5naWYfAgUFZW4tR0JkZBgBBR5fX0NvbnRyb2xzUmVxdWlyZVBvc3RCYWNrS2V5X18WAwUwY3RsMDIkbGFuZ3VhZ2VSZXBlYXRlciRjdGwwMCRpbWFnZUJ1dHRvbkxhbmd1YWdlBTBjdGwwMiRsYW5ndWFnZVJlcGVhdGVyJGN0bDAxJGltYWdlQnV0dG9uTGFuZ3VhZ2UFHExvZ2luUG9ydGFsJExvZ2luSW1hZ2VCdXR0b27mxoduUjO73E1fxorgw8lFyTq/QQ=="
vs2 = "/wEPDwUKLTY4MjI1MDA4NA9kFgICAw9kFgICAQ9kFgJmDxYCHgtfIUl0ZW1Db3VudAICFgRmD2QWAgIBDw8WBB4ISW1hZ2VVcmwFIH4vaW1hZ2VzL0xhbmd1YWdlSWNvbnMvc3Ytc2UuZ2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QxeNtE0tIgHnnO30AYpUx"

print(ViewState(vs1).decode())
print(ViewState(vs2).decode())
print(vs1 == vs3)

cookieProcessor = urllib.request.HTTPCookieProcessor()
opener = urllib.request.build_opener(cookieProcessor)

request = urllib.request.Request('http://aptusportal.sssb.se/')
response = opener.open(request,timeout=100)

data = {
	"__LASTFOCUS": "",
	"__EVENTTARGET": "",
	"__EVENTARGUMENT": "",
	"__VIEWSTATE": vs3,
	"__EVENTVALIDATION": ev3,
	"LoginPortal$UserName": "******",
	"LoginPortal$Password": "******",
	"LoginPortal$LoginButton": "Logga in"
}

request = urllib.request.Request(r'http://aptusportal.sssb.se/login.aspx?ReturnUrl=%2findex.aspx', headers = data)