def test_go_to_admin_login_page(self):
        # in method under test, stub the expected function calls with correct arguments
        expect(LibraryLoader.get_instance().sl, times=1).go_to(url=admin_login_page_url)

        expect(LibraryLoader.get_instance().sl, times=1).wait_until_element_is_enabled(
            locator=locator['admin_login_page']['login_button'])

        expect(LibraryLoader.get_instance().sl, times=1).element_text_should_be(
            locator=locator['admin_login_page']['title'],
            expected=expected['admin_login_page']['title_text'])

        expect(LibraryLoader.get_instance().sl, times=1).element_text_should_be(
            locator=locator['admin_login_page']['username_title'],
            expected=expected['admin_login_page']['username_text'])

        expect(LibraryLoader.get_instance().sl, times=1).element_text_should_be(
            locator=locator['admin_login_page']['password_title'],
            expected=expected['admin_login_page']['password_text'])

        expect(LibraryLoader.get_instance().sl, times=1).element_attribute_value_should_be(
            locator=locator['admin_login_page']['login_button'],
            attribute='value',
            expected=expected['admin_login_page']['login_button_text'])

        # CUT gets magically the mock instances (i.e. _loader & sl)
        admin_login_page = AdminLoginPage()

        # method under test gets called
        admin_login_page.go_to_admin_login_page()

        # Verifies that expectations set via expect are met
        # all registered objects will be checked.
        verifyNoUnwantedInteractions()
Beispiel #2
0
    def testNoUnwantedInteractionsWorks(self, verification):
        rex = Dog()
        expect(rex, **verification).bark('Miau').thenReturn('Wuff')
        rex.bark('Miau')
        rex.bark('Miau')

        verifyNoUnwantedInteractions(rex)
    def testNoUnwantedInteractionsWorks(self, verification):
        rex = Dog()
        expect(rex, **verification).bark('Miau').thenReturn('Wuff')
        rex.bark('Miau')
        rex.bark('Miau')

        verifyNoUnwantedInteractions(rex)
Beispiel #4
0
    def test_request_params(self, host, api, expected_host, expected_url,
                            insecure, expected_verify, stream):

        # Setup
        params = XnatServerParams(host=host, user='******', insecure=insecure)
        session = XnatSession(params)

        # Request
        cookie1 = 'first_session_id'
        with self._mock_auth_login(expected_host=expected_host,
                                   success=True,
                                   verify=expected_verify,
                                   cookie=cookie1):
            self._do_request(session=session,
                             cookie=cookie1,
                             api=api,
                             expected_url=expected_url,
                             expected_verify=expected_verify,
                             stream=stream)
            verifyNoUnwantedInteractions()

        # Deletion session to logout
        with self._mock_auth_logout(expected_host=expected_host,
                                    success=True,
                                    verify=expected_verify,
                                    cookie=cookie1):
            session.__del__()
            verifyNoUnwantedInteractions()
Beispiel #5
0
    def test_login_short_way(self):
        # stub the expected function calls in method under test
        expect(LibraryLoader.get_instance().bl, times=1).fill_secret(
            selector=locator['admin_login_page']['username_field'],
            secret=self.credentials['valid_admin']['username']).thenReturn(
                None)

        expect(LibraryLoader.get_instance().bl, times=1).fill_secret(
            selector=locator['admin_login_page']['password_field'],
            secret=self.credentials['valid_admin']['password']).thenReturn(
                None)

        expect(LibraryLoader.get_instance().bl,
               times=1).click(selector=locator['admin_login_page']
                              ['login_button']).thenReturn(None)

        # CUT gets magically the mock instances (i.e. _loader & bl)
        admin_login_page = AdminLoginPage()

        # method under test gets called
        admin_login_page.login(self.credentials['valid_admin']['username'],
                               self.credentials['valid_admin']['password'])

        # Verifies that expectations set via expect are met
        # all registered objects will be checked.
        verifyNoUnwantedInteractions()
def test_push_to_prometheus():
    url = "http://prometheus.com"

    with expect(prometheus_client,
                times=1).push_to_gateway(url, **kwargs).thenReturn(":ok"):
        assert aws_cost_exporter.push_to_prometheus(url, "stuff", 0)

        verifyNoUnwantedInteractions()
    def testUseWhenAndExpectTogetherVerifyNoUnwatedInteractions(self):
        rex = Dog()
        when(rex).waggle()
        expect(rex, times=1).bark('Miau')

        rex.waggle()
        rex.bark('Miau')

        verifyNoUnwantedInteractions()
Beispiel #8
0
def test_capture_simple(caplog):
    caplog.set_level(10, 'elib_run.process')
    context = _dummy_context()
    when(context.capture).readline(block=False).thenReturn(b'random string').thenReturn(None)
    _capture_output.capture_output_from_running_process(context)
    verifyNoUnwantedInteractions()
    verifyStubbedInvocationsAreUsed()
    assert ['random string'] == context.process_output_chunks
    assert 'random string' in caplog.text
Beispiel #9
0
 def test_get_log_weight_from_canform():
     """
     Test that the _update_covform function is called before returning the log-weight parameter.
     """
     gaussian_a = Gaussian(prec=2.0, h_vec=1.0, g_val=0.0, var_names=["a"])
     expect(Gaussian, times=1)._update_covform()
     gaussian_a.get_log_weight()
     verifyNoUnwantedInteractions()
     unstub()
Beispiel #10
0
    def testUseWhenAndExpectTogetherVerifyNoUnwatedInteractions(self):
        rex = Dog()
        when(rex).waggle()
        expect(rex, times=1).bark('Miau')

        rex.waggle()
        rex.bark('Miau')

        verifyNoUnwantedInteractions()
Beispiel #11
0
def test_process_finished(dummy_kwargs):
    command = mock()
    when(command).poll().thenReturn(None).thenReturn(0)
    context = _run_context.RunContext(**dummy_kwargs)
    setattr(context, '_command', command)
    assert not context.process_finished()
    assert context.process_finished()
    verifyNoUnwantedInteractions()
    verifyStubbedInvocationsAreUsed()
Beispiel #12
0
def test_capture_muted():
    context = _dummy_context()
    context.mute = True
    when(context.capture).readline(block=False).thenReturn(b'random string').thenReturn(None)
    when(context.process_logger).debug(...)
    _capture_output.capture_output_from_running_process(context)
    verify(context.process_logger, times=0).debug(...)
    verifyNoUnwantedInteractions()
    verifyStubbedInvocationsAreUsed()
    assert ['random string'] == context.process_output_chunks
Beispiel #13
0
    def testNoUnwantedInteractionsForAllRegisteredObjects(self):
        rex = Dog()
        mox = Dog()

        expect(rex, times=1).bark('Miau')
        expect(mox, times=1).bark('Miau')

        rex.bark('Miau')
        mox.bark('Miau')

        verifyNoUnwantedInteractions()
Beispiel #14
0
def test_start_process(dummy_kwargs):
    command = mock()
    when(command).run(async_=True)
    context = _run_context.RunContext(**dummy_kwargs)
    setattr(context, '_command', command)
    assert context.start_time == 0
    verifyZeroInteractions()
    context.start_process()
    assert context.start_time != 0
    verifyStubbedInvocationsAreUsed()
    verifyNoUnwantedInteractions()
    def testNoUnwantedInteractionsForAllRegisteredObjects(self):
        rex = Dog()
        mox = Dog()

        expect(rex, times=1).bark('Miau')
        expect(mox, times=1).bark('Miau')

        rex.bark('Miau')
        mox.bark('Miau')

        verifyNoUnwantedInteractions()
Beispiel #16
0
 def test_get_g_from_covform():
     """
     Test that the _update_canform function is called before returning the g parameter.
     """
     gaussian_a = Gaussian(cov=2.0,
                           mean=1.0,
                           log_weight=0.0,
                           var_names=["a"])
     expect(Gaussian, times=1)._update_canform()
     gaussian_a.get_g()
     verifyNoUnwantedInteractions()
     unstub()
Beispiel #17
0
    def do_test_select_checkbox_for_group(group_name):
        # configure the mock selenium library for select_checkbox_for_group()'s calls
        group_element_checkbox_locator = locator['groups_page']['generic_group_element_checkbox'] % group_name
        expect(LibraryLoader.get_instance().sl).click_element(locator=group_element_checkbox_locator).thenReturn(None)

        # CUT gets magically the mock instances (i.e. _loader & sl)
        groups_page = GroupsPage()

        # method under test gets called
        groups_page.select_checkbox_for_group(group_name)

        # Verifies that expectations set via expect are met
        # all registered objects will be checked.
        verifyNoUnwantedInteractions()
Beispiel #18
0
    def test_go_to_admin_main_page(self):
        # configure the mock selenium library for go_to_admin_main_page()'s calls
        expect(LibraryLoader.get_instance().sl).go_to(
            expected_admin_main_page_url).thenReturn(None)

        # CUT gets magically the mock instances (i.e. _loader & sl)
        admin_main_page = AdminMainPage()

        # method under test gets called
        admin_main_page.go_to_admin_main_page()

        # Verifies that expectations set via expect are met
        # all registered objects will be checked.
        verifyNoUnwantedInteractions()
    def test_press_go(self):
        # configure the mock browser library
        expect(LibraryLoader.get_instance().bl).click(
            selector=locator['groups_page']['go_button']).thenReturn(None)

        # CUT gets magically the mock instances (i.e. _loader & sl)
        groups_page = GroupsPage()

        # method under test gets called
        groups_page.press_go()

        # Verifies that expectations set via expect are met
        # all registered objects will be checked.
        verifyNoUnwantedInteractions()
Beispiel #20
0
    def test_click_on_save_button(self):
        # configure the mock browser library for click_on_save_button()'s calls
        expect(LibraryLoader.get_instance().bl).click(
            selector=locator['add_group_page']['save_button']).thenReturn(None)

        # CUT gets magically the mock instances (i.e. _loader & sl)
        add_group_page = AddGroupPage()

        # method under test gets called
        add_group_page.click_on_save_button()

        # Verifies that expectations set via expect are met
        # all registered objects will be checked.
        verifyNoUnwantedInteractions()
Beispiel #21
0
    def test_select_delete_selected_groups_dropdown(self):
        # configure the mock selenium library for select_delete_selected_groups_dropdown()'s calls
        expect(LibraryLoader.get_instance().sl).click_element(
            locator=locator['groups_page']['delete_selected_groups_option']
        ).thenReturn(None)
        # CUT gets magically the mock instances (i.e. _loader & sl)
        groups_page = GroupsPage()

        # method under test gets called
        groups_page.select_delete_selected_groups_dropdown()

        # Verifies that expectations set via expect are met
        # all registered objects will be checked.
        verifyNoUnwantedInteractions()
    def test_press_confirm_button(self):
        # configure the mock selenium library for press_confirm_button()'s calls
        expect(LibraryLoader.get_instance().sl).click_element(
            locator=locator['confirm_groups_deletions_page']
            ['confirm_deletion_button']).thenReturn(None)

        # CUT gets magically the mock instances (i.e. _loader & sl)
        confirm_group_deletions_page = ConfirmGroupsDeletionsPage()

        # method under test gets called
        confirm_group_deletions_page.press_confirm_button()

        # Verifies that expectations set via expect are met
        # all registered objects will be checked.
        verifyNoUnwantedInteractions()
Beispiel #23
0
    def test_enter_name_for_new_group(self):
        # configure the mock browser library for enter_name_for_new_group()'s calls
        group_name = 'blog_editors'
        expect(LibraryLoader.get_instance().bl).fill_text(
            selector=locator['add_group_page']['input_name_field'],
            txt=group_name).thenReturn(None)
        # CUT gets magically the mock instances (i.e. _loader & sl)
        add_group_page = AddGroupPage()

        # method under test gets called
        add_group_page.enter_name_for_new_group(group_name=group_name)

        # Verifies that expectations set via expect are met
        # all registered objects will be checked.
        verifyNoUnwantedInteractions()
Beispiel #24
0
    def test_clear_available_permissions_filter(self):
        # configure the mock browser library for clear_available_permissions_filter()'s calls
        expect(LibraryLoader.get_instance().bl).clear_text(
            selector=locator['add_group_page']
            ['input_permission_field']).thenReturn(None)

        # CUT gets magically the mock instances (i.e. _loader & sl)
        add_group_page = AddGroupPage()

        # method under test gets called
        add_group_page.clear_available_permissions_filter()

        # Verifies that expectations set via expect are met
        # all registered objects will be checked.
        verifyNoUnwantedInteractions()
Beispiel #25
0
    def test_choose_all_filtered_permissions(self):
        # configure mock calls in choose_all_filtered_permissions()
        filtered_permission_elements = AddGroupPageUT.get_filtered_permission_elements(
        )

        expect(LibraryLoader.get_instance().bl).wait_for_elements_state(
            selector=locator['add_group_page']['generic_filtered_permission'],
            state=ElementState.visible).thenReturn(None)

        expect(LibraryLoader.get_instance().bl).get_elements(
            selector=locator['add_group_page']['generic_filtered_permission']
        ).thenReturn(filtered_permission_elements)

        control_option = ControlOption()
        setattr(control_option, 'name', 'Control')
        for element in filtered_permission_elements:
            expect(
                LibraryLoader.get_instance().bl).get_text(element).thenReturn(
                    element.text)
            expect(LibraryLoader.get_instance().bl).click(
                element, MouseButton.left, 1, None, None, None, False, False,
                ...).thenReturn(None)

        expect(LibraryLoader.get_instance().bl).click(
            locator['add_group_page']['choose_all_permissions_option'],
            MouseButton.left).thenReturn(None)

        # configure mock calls in _verify_permissions_added()
        chosen_permission_elements = AddGroupPageUT.get_filtered_permission_elements(
        )
        expect(LibraryLoader.get_instance().bl).get_elements(
            selector=locator['add_group_page']['generic_chosen_permission']
        ).thenReturn(chosen_permission_elements)

        for element in chosen_permission_elements:
            expect(
                LibraryLoader.get_instance().bl).get_text(element).thenReturn(
                    element.text)

        # CUT gets magically the mock instances (i.e. _loader & sl)
        add_group_page = AddGroupPage()

        # method under test gets called
        add_group_page.choose_all_filtered_permissions()

        # Verifies that expectations set via expect are met
        # all registered objects will be checked.
        verifyNoUnwantedInteractions()
Beispiel #26
0
    def test_reauth_success(self):
        # Tests when a REST call fails with authentication error, so
        # a new session is created and the REST call retries and succeeds

        host = 'http://test.server'
        params = XnatServerParams(host=host, user='******', insecure=False)
        session = XnatSession(params)

        # First request
        cookie1 = 'first_session_id'
        with self._mock_auth_login(expected_host=host,
                                   success=True,
                                   verify=True,
                                   cookie=cookie1):
            api1 = 'my-api/call1'
            expected_url1 = host + '/' + api1
            self._do_request(session=session,
                             cookie=cookie1,
                             api=api1,
                             expected_url=expected_url1,
                             expected_verify=True)
            verifyNoUnwantedInteractions()

        # Trigger an authentication failure on new rest call but let reauth
        # succeed
        cookie2 = 'second_session_id'
        with self._mock_auth_login(expected_host=host,
                                   success=True,
                                   verify=True,
                                   cookie=cookie2):
            api2 = 'my-api/call2'
            expected_url2 = host + '/' + api2
            self._do_request(session=session,
                             cookie=cookie1,
                             api=api2,
                             retry_cookie=cookie2,
                             expected_url=expected_url2,
                             expected_verify=True,
                             success_first=False,
                             success_second=True)

        # New session is valid so logout
        with self._mock_auth_logout(expected_host=host,
                                    success=True,
                                    verify=True,
                                    cookie=cookie2):
            session.__del__()
            verifyNoUnwantedInteractions()
Beispiel #27
0
    def do_test_verify_group_added(group_name):
        # configure the mock selenium library for verify_group_added()'s calls
        locator_formatter = locator['groups_page']['generic_group_element'] % group_name
        expect(LibraryLoader.get_instance().sl).element_text_should_be(
            locator=locator_formatter,
            expected=group_name
        ).thenReturn(None)

        # CUT gets magically the mock instances (i.e. _loader & sl)
        groups_page = GroupsPage()

        # method under test gets called
        groups_page.verify_group_added(group_name)

        # Verifies that expectations set via expect are met
        # all registered objects will be checked.
        verifyNoUnwantedInteractions()
Beispiel #28
0
    def test_reauth_retry_failure(self):
        # This captures the case where user credentials succeed but the
        # subsequent REST call still returns an unauthorised error. In this
        # case a response is returned to the rest client rather than
        # raising an exception as it suggests some other access issue rather
        # than user credentials.

        host = 'http://test.server'
        params = XnatServerParams(host=host, user='******', insecure=False)
        session = XnatSession(params)

        # First request
        cookie1 = 'first_session_id'
        with self._mock_auth_login(expected_host=host,
                                   success=True,
                                   verify=True,
                                   cookie=cookie1):
            api1 = 'my-api/call1'
            expected_url1 = host + '/' + api1
            self._do_request(session=session,
                             cookie=cookie1,
                             api=api1,
                             expected_url=expected_url1,
                             expected_verify=True)
            verifyNoUnwantedInteractions()

        # Trigger an authentication failure on rest call, let the reauth
        # succeed but make the request retry fail
        cookie2 = 'second_session_id'
        with self._mock_auth_login(expected_host=host,
                                   success=True,
                                   verify=True,
                                   cookie=cookie2):
            api2 = 'my-api/call2'
            expected_url2 = host + '/' + api2

            with pytest.raises(MockHttpError) as e_info:
                self._do_request(session=session,
                                 cookie=cookie1,
                                 api=api2,
                                 retry_cookie=cookie2,
                                 expected_url=expected_url2,
                                 expected_verify=True,
                                 success_first=False,
                                 success_second=False)
    def do_test_verify_group_added(group_name):
        # configure the mock browser library for verify_group_added()'s calls
        added_group_locator = locator['groups_page'][
            'generic_group_element'] % group_name
        expect(LibraryLoader.get_instance().bl).get_text(
            selector=added_group_locator,
            assertion_operator=AssertionOperator.equal,
            assertion_expected=group_name).thenReturn(None)

        # CUT gets magically the mock instances (i.e. _loader & sl)
        groups_page = GroupsPage()

        # method under test gets called
        groups_page.verify_group_added(group_name)

        # Verifies that expectations set via expect are met
        # all registered objects will be checked.
        verifyNoUnwantedInteractions()
Beispiel #30
0
    def test_reauth_failure(self):
        # Tests when a REST call fails with authentication error, try to create
        # a new session but this fails

        host = 'http://test.server'
        params = XnatServerParams(host=host, user='******', insecure=False)
        session = XnatSession(params)

        # First request
        cookie1 = 'first_session_id'
        with self._mock_auth_login(expected_host=host,
                                   success=True,
                                   verify=True,
                                   cookie=cookie1):
            api1 = 'my-api/call1'
            expected_url1 = host + '/' + api1
            self._do_request(session=session,
                             cookie=cookie1,
                             api=api1,
                             expected_url=expected_url1,
                             expected_verify=True)
            verifyNoUnwantedInteractions()

        # Trigger an authentication failure on rest call, let the reauth fail
        cookie2 = 'fourth_session_id'
        with self._mock_auth_login(expected_host=host,
                                   success=False,
                                   verify=True,
                                   cookie=cookie2):
            api2 = 'my-api/call2'
            expected_url2 = host + '/' + api2
            with pytest.raises(UserError) as e_info:
                self._do_request(session=session,
                                 cookie=cookie1,
                                 api=api2,
                                 retry_cookie=None,
                                 expected_url=expected_url2,
                                 expected_verify=True,
                                 success_first=False,
                                 success_second=False)

        # Should be no sessions remaining due to failure
        session.__del__()
        verifyNoUnwantedInteractions()
Beispiel #31
0
    def test_auto_logout(self, host, expected_host):
        server_params = XnatServerParams(host=host, user='******')
        session = XnatSession(server_params)

        # Login
        cookie = 'first_session_id'
        with self._mock_auth_login(expected_host=expected_host,
                                   success=True,
                                   verify=True,
                                   cookie=cookie):
            session.login()
            verifyNoUnwantedInteractions()

        # Check auto logout on session deletion
        with self._mock_auth_logout(expected_host=expected_host,
                                    success=True,
                                    verify=True,
                                    cookie=cookie):
            session.__del__()
            verifyNoUnwantedInteractions()
Beispiel #32
0
    def test_go_to_admin_login_page(self):
        # in method under test, stub the expected function calls with correct arguments
        expect(LibraryLoader.get_instance().bl,
               times=1).go_to(url=admin_login_page_url)

        expect(LibraryLoader.get_instance().bl,
               times=1).wait_for_elements_state(
                   selector=locator['admin_login_page']['login_button'],
                   state=ElementState.visible)

        expect(LibraryLoader.get_instance().bl, times=1).get_text(
            selector=locator['admin_login_page']['title'],
            assertion_operator=AssertionOperator.equal,
            assertion_expected=expected['admin_login_page']['title_text'])

        expect(LibraryLoader.get_instance().bl, times=1).get_text(
            selector=locator['admin_login_page']['username_title'],
            assertion_operator=AssertionOperator.equal,
            assertion_expected=expected['admin_login_page']['username_text'])

        expect(LibraryLoader.get_instance().bl, times=1).get_text(
            selector=locator['admin_login_page']['password_title'],
            assertion_operator=AssertionOperator.equal,
            assertion_expected=expected['admin_login_page']['password_text'])

        expect(LibraryLoader.get_instance().bl, times=1).get_attribute(
            selector=locator['admin_login_page']['login_button'],
            attribute='value',
            assertion_operator=AssertionOperator.equal,
            assertion_expected=expected['admin_login_page']
            ['login_button_text'])

        # CUT gets magically the mock instances (i.e. _loader & bl)
        admin_login_page = AdminLoginPage()

        # method under test gets called
        admin_login_page.go_to_admin_login_page()

        # Verifies that expectations set via expect are met
        # all registered objects will be checked.
        verifyNoUnwantedInteractions()
    def test_choose_all_filtered_permissions(self):
        # configure mock calls in choose_all_filtered_permissions()
        filtered_permission_elements = AddGroupPageUT.get_filtered_permission_elements(
        )

        expect(LibraryLoader.get_instance().sl).get_webelements(
            locator=locator['add_group_page']['generic_filtered_permission']
        ).thenReturn(filtered_permission_elements)

        for element in filtered_permission_elements:
            expect(
                LibraryLoader.get_instance().sl).get_text(element).thenReturn(
                    element.text)
            expect(LibraryLoader.get_instance().sl).press_keys(
                element, 'CTRL').thenReturn(None)

        expect(LibraryLoader.get_instance().sl).click_link(
            locator=locator['add_group_page']
            ['choose_all_permissions_option']).thenReturn(None)

        # configure mock calls in _verify_permissions_added()
        chosen_permission_elements = AddGroupPageUT.get_filtered_permission_elements(
        )
        expect(LibraryLoader.get_instance().sl).get_webelements(
            locator=locator['add_group_page']['generic_chosen_permission']
        ).thenReturn(chosen_permission_elements)

        for element in chosen_permission_elements:
            expect(
                LibraryLoader.get_instance().sl).get_text(element).thenReturn(
                    element.text)

        # CUT gets magically the mock instances (i.e. _loader & sl)
        add_group_page = AddGroupPage()

        # method under test gets called
        add_group_page.choose_all_filtered_permissions()

        # Verifies that expectations set via expect are met
        # all registered objects will be checked.
        verifyNoUnwantedInteractions()
    def testNoUnwantedInteractionsBarksIfUnsatisfied(self, verification):
        rex = Dog()
        expect(rex, **verification).bark('Miau').thenReturn('Wuff')

        with pytest.raises(VerificationError):
            verifyNoUnwantedInteractions(rex)