Commit Graph

136 Commits

Author SHA1 Message Date
Akihiro Motoki 7052b7f065 Address RemovedInDjango40Warning (7)
HttpRequest.is_ajax() was marked as deprecated since Django 3.1 and will be
removed in Django 4.0 [1].

While the current implementation of is_ajax() relies on a jQuery-specific way
of signifying AJAX as noted in the Django relnotes, horizon works with this.
Thus this commit copies the existing logic of HttpRequest.is_ajax() to the
horizon repo (as horizon.utils.http.is_ajax()) and consumes it.

https: //docs.djangoproject.com/en/4.0/releases/3.1/#features-deprecated-in-3-1
Change-Id: I3def53033524985818a891a1b9d4659fad4ba2ba
2022-02-04 16:27:32 +09:00
Akihiro Motoki e4444e6979 Address RemovedInDjango40Warning (4)
In Django 3.0, django.utils.http.urlquote(), urlquote_plus(),
urlunquote(), and urlunquote_plus() are deprecated in favor of
the functions that they’re aliases for: urllib.parse.quote(),
quote_plus(), unquote(), and unquote_plus().

https: //docs.djangoproject.com/en/4.0/releases/3.0/
Change-Id: I37fcd917cbf87b4d3141cfbdd2675aa38f33f2a4
2022-02-04 16:27:18 +09:00
Radomir Dopieralski 34a0159d1a Add system scope support to context switcher
Change-Id: Idd2ec7ae6e978a358b4b3639e86cadae06c90976
2021-11-26 14:09:46 +01:00
Akihiro Motoki 9526289b76 Support Django 3.2 support (2)
In Django 3.2 CookieStorage stores messages in the RFC 6265 compliant
format [1][2]. This means that horizon messages pass through cookies are
encoded in a different way that messages are encrypted.

Previously horizon UT interpretes messages in cookies literally in its own way,
but the change on CookieStorage in Django 3.2 broke it. Horizon should not
depend on its own way. The suggested way I believe is to use a method defined
in django.contrib.messages.storage.cookie.CookieStorage.

[1] https://docs.djangoproject.com/en/3.2/releases/3.2/#miscellaneous
[2] 2d6179c819

Change-Id: I3e5e12265d0bc7b753bbe1f57acdd663b9dd3587
2021-09-16 11:18:00 +00:00
Akihiro Motoki e77633c56a Support Django 3.2 support (1)
Django 3.2 defines the new header API for the response object [1].
This change dropped "_headers" attribute from the response object.
Instead of accesing a private attribute, we need to use the public APIs
for response headers [2].

[1] https://docs.djangoproject.com/en/3.2/releases/3.2/#requests-and-responses
[2] https://docs.djangoproject.com/en/3.2/ref/request-response/#httpresponse-objects

Change-Id: Ia13212fd231215e17f4bba2039e79874b676e634
2021-09-15 18:15:04 +00:00
Akihiro Motoki 44b7c03fba Support Django 3.0 and 3.1 support (4)
assertFormErrors() now compares actual and expected messages
after parsing them as HTML. Similar to the previous commit,
after Django 3.0 escaped characters are handled differently
(decimal representation, hexdecimal representation and
unescaped representation) so we need an uniform way to compare
texts. Instead of comparing texts literally, this commit changes
assertFormErrors to compare texts after parsing them as HTML.

Change-Id: I4ff89cdcb27a2671c7d79fb2caec30585696a30f
2021-09-14 19:36:27 +00:00
Akihiro Motoki 1dd12d8deb test: Ensure to stop mock when create_mocks decorator exits
During reviewing https://review.opendev.org/c/openstack/horizon/+/772603,
we noticed that a method decorated by create_mocks is called multiple time
in a single test. Previously create_mocks decorator does not stop mocking,
so this means that multiple active mock can exist for one method.
In general, it is not a good idea to mock a method multiple times at the
same time.

To cope with this situation, this commit ensures for create_mocks decorator
to stop active mocks when exiting the decorator.

This works for most cases, but it does not work only when mocking and
assertions are handled by separate methods and test logic is placed between
them. To cope with this, "stop_mock" optional argument is introduced.

FYI: Details on why "stop_mock" is needed.
I explored various ways but could not find a good way so far.
create_mocks needs to be a decorator as it needs to refer an object
reference (self). On the other hand, if we would like to merge the logic of
mocking and assertions (for example, _stub_api_calls() and _check_api_calls()
in openstack_dashboard/dashboards/project/overview/tests.py), a context
manager would be good as we would like to call it inside a class.
However, we cannot mix a decorator and a context manger as a decorator is
executed when a context manager is iniitialized and stopping mock in
the create_mocks decorator is done during the initialization of the
context manager and methods are not mocked when test code is run.

Change-Id: I9e37dc1eaa08adf36d11975fed6f5a0a90cdde52
2021-02-26 18:44:11 +09:00
Akihiro Motoki c45cc6b1c5 Drop the usage of nova extensions in python code
Part of the removal of OPENSTACK_NOVA_EXTENSIONS_BLACKLIST (1/3)

All references of nova extensions in the python code are cleaned up.
Note that the API layer is not touched yet as it is used by the
JavaScript side.

Change-Id: I66cd0a9629253a6462aace9902ef8200b94b2a21
2020-10-27 20:00:45 +09:00
Akihiro Motoki e5d09edc20 Use python3-style super()
In python3, super() does not always require a class and self reference.
In other words, super() is enough for most cases.
This is much simpler and it is time to switch it to the newer style.

pylint provides a check for this.
Let's enable 'super-with-arguments' check.

NOTE: _prepare_mappings() method of FormRegion in
openstack_dashboard/test/integration_tests/regions/forms.py is refactored.
super() (without explicit class and self referece) does not work when
a subclass method calls a same method in a parent class multiple times.
It looks better to prepare a separate method to provide a common logic.

Change-Id: Id9512a14be9f20dbd5ebd63d446570c7b7c825ff
2020-10-15 14:37:20 +09:00
Akihiro Motoki 0c96dcf293 Handle case without pytest for plugin tests
Switching the test runner to pytest in horizon assumes pytest is always
installed, but horizon test helpers are used in horizon plugin tests and
pytest is not used in horizon plugin tests. As a result, all horizon plugin
tests are now broken.

This commit considers a case where pytest does not exist.
A wrapper decorator for pytest.mark.xxxx is introduced and
it acts as a null decorator if pytest is not installed.

Change-Id: I80736b108f5ae9a36c0e756bf386468879be3293
Closes-Bug: #1873532
2020-04-18 08:44:45 +09:00
Oleksii Petrenko d6fe0170ee Change horizon test runner to pytest
Changes test invocation from `manage.py test` to `pytest`. Adds addtitional
test requirements like pytest, pytest-django, pytest-html. Adds
`pytest.mark` alongside django's test `tag`. Adds posibility to export test
results into xml and html formats.

Depends-On: https://review.opendev.org/#/c/712315/
Related-Bug: #1866666
Co-Authored-By: Ivan Kolodyazhny <e0ne@e0ne.info>
Change-Id: Idb6e63cd23ca2ba8ca56f36eb8b63069bd211944
2020-04-03 13:47:18 +03:00
Ivan Kolodyazhny 14e779bbac Use unittest.mock instead of third party mock
Now that we no longer support py27, we can use the standard library
unittest.mock module instead of the third party mock lib.

Change-Id: I2de669d8e89b8daeb7ee5405ffab35af6307c40b
2020-03-26 19:45:37 +02:00
Ivan Kolodyazhny d870b40583 Remove six usage from openstack_dashboard package
We don't support Python 2 anymore so we don't need this
compatibility library.

six.reraise usages are left as is until it'll be moved to some
base lib like oslo.utils to not re-implenent this method in
Horizon.

This patch also removes Python2-specific base test case methods
assertItemsEqual and assertNotRegexpMatches in flavor of new
Python 3 analogues.

Change-Id: I26a59176be9e9f213128e4945a58b9459334b626
2020-01-15 12:47:48 +02:00
Akihiro Motoki 2f1e1899d3 Define default settings explicitly (openstack_dashboard 5/5)
This commit mainly covers settings in the remaining files
under openstack_dashboard.

Note that HORIZON_CONFIG, horizon and openstack_auth are not covered.
They will be covered by follow-up patches.

Part of blueprint ini-based-configuration
Change-Id: Ibd70e030445a073d9a62da9867850f4893135a89
2019-07-08 18:20:32 +09:00
ZhongShengping a569f0faf3 Replace git.openstack.org URLs with opendev.org URLs
Thorough replacement of git.openstack.org URLs with their opendev.org
counterparts.

Change-Id: I7f5506455ed52e4670b8e428548e931db13b13db
2019-04-24 12:25:40 +08:00
Akihiro Motoki a69ba853a7 UT: Run test_plugins as separate process
This commit changes to run test_plugins in a separate process.

test_plugins touches urlpatterns and Site registry during setUp(),
but the cleanup process in tearDown does not recover urlpatterns correctly.
After tests under test_plugins run, the urlpatterns continues to have
entries of test panels and this leads to NoReverseMatch error when
rendering other panels after that.

I investigated details in setUp/tearDown of PluginTestCase but failed
to find a good solution to recover urlpatterns and Horizon Site registry.
As a workaround, runnig test_plugins in a separate process can avoid
the issue.

Closes-Bug: #1809983
Change-Id: I848f6b341b3f93ed055955b2b12d2497811edc5a
2019-01-31 21:29:29 +09:00
Ivan Kolodyazhny de69edd040 Add Fix django.contrib.auth.middleware monkey patching for APITestCase
This patch rollbacks changes in APITestCase introduced in the
0d16361326 commit to unblock Horizon
plugins gates.

Change-Id: I6b2be31568e4aa4273990f9cfa76ca139620c84b
Closes-bug: #1806053
Closes-bug: #1805240
2018-11-30 17:33:21 +02:00
pengyuesheng 6216715e7d add the assertWorkflowErrors
some of the action pages use workflow,
but the check is using assertFormErrors,
which is unreasonable,
assertFormErrors only checks one step, not all

Change-Id: I764be4a243fb9c4790c195610514663d71e18373
2018-11-16 09:58:00 +08:00
pengyuesheng a8006b4441 Ignore error msg for gateway IP when gateway is disabled
Although the gateway is disabled,
it still checks the format of the gateway ip and
throws an error if there is a problem with the format.

Change-Id: I8d13819175701885412ab4813e19aba0ac63a3c3
Closes-Bug: #1799896
2018-11-13 16:18:45 +08:00
Mathieu Gagné 0d16361326 Fix django.contrib.auth.middleware monkey patching
The "request" attribute is not available in
openstack_auth.backend.KeystoneBackend.get_user when session data is restored
and it's the first request to happen after a server restart.

As stated by the function document, the "request" attribute needs to be
monkey-patched by openstack_auth.utils.patch_middleware_get_user
for this function to work properly.

This should happen in openstack_auth.urls at import time. But there is nowhere
in Horizon where this module is imported at startup. It's only introspected
by openstack_dashboard.urls due to AUTHENTICATION_URLS setting.

Without this monkey-patching, the whole authentication mechanism falls back
to "AnonymousUser" and you will get redirected to the login page due
to horizon.exceptions.NotAuthenticated being raised by
horizon.decorators.require_auth as request.user.is_authenticated will be False.

But if a user requests a page under auth/, it will have the side-effect of
monkey-patching django.contrib.auth.middleware as expected. This means that
once this request is completed, all following requests to pages other than
the ones under auth/ will have there sessions properly restored and
you will be properly authenticated.

Therefore this change introduces a dummy middleware which sole purpose is
to perform this monkey-patching as early as possible.

There is also some cleanup to get rid of the previous attempts at
monkeypatching.

Closes-bug: #1764622
Change-Id: Ib9912090a87b716e7f5710f6f360b0df168ec2e3
2018-11-06 21:36:45 +00:00
Akihiro Motoki aa1ca195f9 Drop use_mox from horizon test helpers
We announced in the Rocky cycle that use_mox will be dropped
in the beginning of Stein cycle. This commit drops use_mox and
cleans up mox related stuffs in horizon and openstack_dashboard codes.

Note that openstack_auth is under the mock migration,
so we cannot mox3 from test-requirements.txt.

blueprint mock-framework-in-unit-tests

Change-Id: I189daf2d678bef0fa8219acaab5cf45bbe54178f
2018-08-18 17:11:15 +09:00
Akihiro Motoki 4704b32a5c Drop mox usage in Selenium tests
Selenium tests now runs in our gate,
so this is the time to migrate them to mock.

The arguments of the first calls of image_list_detailed
in test_modal_create_image_from_url and
test_modal_create_image_from_file need to be updated.
It seems mox did not check arguments strictly.

blueprint mock-framework-in-unit-tests

Change-Id: If60734d6dbd667fe37ffe64858b93f503e6b8386
2018-08-18 17:08:23 +09:00
Ivan Kolodyazhny 1f80d94459 Use default Django test runner instead of nose
Nose has been in maintenance mode for the past several years. It has
issue with exit code [1] which leads to false positive results for our
seleniun-headless job.

This patch changes test runner for Horizon tests and does the following
things:

* Django test runner  executes test in a different order than Nose does.
  That's why we've got an issue with side-effect in
  horizon.tests.unit.tables.test_tables.MyToggleAction class. This patch
  adds workaround to it.
* Rename filename of test files to names starting with 'test_'
  so that the django test runner can find tests expectedly.
* '--with-html-output' option is temporary dropped and will be added in
  a following patch.
* Integraion tests is marked via django.test.tag mechanism which is
  introduced in Django 1.10
* 'selenium-headless' is broken now because we don't have geckodriver on
  gates, this patch makes it non-voting.
* 'tox -e cover' is fixed
* Remove @memorized decorator from
  dashboards.project.images.images.tables.filter_tenant_ids function.

[1] https://github.com/nose-devs/nose/issues/984

Depends-On: https://review.openstack.org/572095
Depends-On: https://review.openstack.org/572124
Depends-On: https://review.openstack.org/572390
Depends-On: https://review.openstack.org/572391

Related blueprint: improve-horizon-testing
Change-Id: I7fb2fd7dd40f301ea822154b9809a9a07610c507
2018-06-08 15:21:12 +03:00
Akihiro Motoki 58019a7411 Disable mox usage by default in test helper
- Add use_mox = True to tests which still depend on mox
- Remove use_mox = False from all tests converted into mock
- Update the warning messages
- Replace 'S'-release with 'Stein'

Part of blueprint mock-framework-in-unit-tests
Change-Id: I208185e97b2de346304a09643a9cb3f1425a7683
Signed-off-by: Akihiro Motoki <amotoki@gmail.com>
2018-04-20 03:34:51 +00:00
Akihiro Motoki 7b7ace0aeb Simplify APIMockTestCase
There is no need to use stub_*client() because we can simply use
mock.patch(.object). This commit removed stub_*client().

Part of blueprint mock-framework-in-unit-tests
Change-Id: I1563720e3fc636f7b042d030baf59e0b1a0e9bf0
Signed-off-by: Akihiro Motoki <amotoki@gmail.com>
2018-04-08 07:58:50 +09:00
Akihiro Motoki c5cd0b2048 Switch Neutron/Network API tests to mock
This patch changes mox to mock for the following test modules:
* openstack_dashboard/test/unit/api/test_network.py
* openstack_dashboard/test/unit/api/test_neutron.py
* openstack_dashboard/test/unit/api/rest/test_network.py
* openstack_dashboard/test/unit/api/rest/test_neutron.py

Partially-Implements: blueprint mock-framework-in-unit-tests

Change-Id: Ib97fb1c06a883fb9fd5ce39b8fe2362a88135692
2018-03-28 18:39:14 +00:00
Zuul fcf108818f Merge "Update docstring of test.helpers.create_mocks" 2018-03-20 21:53:39 +00:00
Zuul cb65015ade Merge "Switch horizon UT from mox to mock" 2018-03-16 12:56:51 +00:00
Zuul 1e0b76a7fd Merge "Switch Keystone API tests to mock" 2018-03-15 10:51:00 +00:00
Akihiro Motoki 2e3e68ad66 Switch horizon UT from mox to mock
This completes mock migration in horizon UT.

IsA and IsHttpRequest are moved to horizon.test.helpers
as they are genric enough and IsA is needed in a horizon UT.

Part of blueprint mock-framework-in-unit-tests
Change-Id: I702551fe900224c9ebbbf3ef815206be035b6d14
2018-03-13 16:14:15 +09:00
Akihiro Motoki a987c039cf TabGroup: Make tabs pluggable via horizon plugin config
This commit enhances django tab implementation to allow horizon plugins
to add tabs to a tab group in other repository like the main horizon repo.
New setting "EXTRA_TABS" is introduced to the horizon plugin 'enabled' file.
To this aim, the tab group class looks up HORIZON_CONFIG['extra_tabs']
with its class full name and loads them as extra tabs if any.
HORIZON_CONFIG['extra_tabs'] are populated via horizon plugin settings.

This commit moves update_settings in openstack_dashboard.test.helpers
to horizon as I would like to use it in a new horizon unit test.

blueprint horizon-plugin-tab-for-info-and-quotas
Closes-Bug: #1746754
Change-Id: Ice2469a90553754929826d14d20b4719bd1f62d3
2018-03-12 21:04:22 +09:00
Zuul cd06fe813e Merge "Drop Django <= 1.10 support" 2018-03-09 17:40:17 +00:00
Akihiro Motoki fb9ef26a7b Allow mox-free horizon plugins to consume horizon test helper
horizon test helpers now depend on mox3.
This prevents horizon plugins from consuming the test helpers
even if a plugin itself is mox free.
This commit makes mox import optional.

Change-Id: I631518d8f4cd9641920f68cd1e405298ddb7965a
Closes-Bug: #1753555
2018-03-06 03:48:17 +09:00
Akihiro Motoki a72963bdbd Drop Django <= 1.10 support
After Django 2.0 support, we no longer supports Django 1.10 or older
(Actually Django 1.10 seems to work though).
The current django.VERSION branches are all related to Django 1.10
or older, so we can drop all conditions.

py35dj20 job is now voting.

blueprint django2-support
Change-Id: Iefc0ab1c62c82f2842ec7761a9b981da9351cbd2
2018-03-03 07:33:23 +09:00
Zuul b82b7d4167 Merge "django2: is_authenticated/is_anonymous is now property only" 2018-03-01 14:46:49 +00:00
Zuul 452bfbc32b Merge "django2: Replace django.core.urlresolves with django.urls" 2018-03-01 14:34:28 +00:00
Akihiro Motoki 61c7473bff Update docstring of test.helpers.create_mocks
This commit improves examples in the docstring of create_mocks.

Note that we agreed to use a list over a tuple to define methods
to be mocked for consistency in general.

Part of blueprint mock-framework-in-unit-tests
Change-Id: Ie2e31a7a4f4d205b19190be5960770f83e1ce858
2018-02-28 21:10:53 +09:00
Akihiro Motoki 6a56497180 Add assert_mock_multiple_calls_with_same_arguments test helper
During the migration from mox to mock, it turns out there is case where
some method is called multiple times with same parameters and
it is common in openstack dashboard tests.

This commit adds assert_mock_multiple_calls_with_same_arguments assertion
method to ensure to check both call_count and arguments of all calls.

Note that assert_has_calls just checks a subset of called arguments
so it is better to check arguments of all calls.

Part of blueprint mock-framework-in-unit-tests
Change-Id: Id939e61718b0405a02a22a5ec8e55eda9554c545
2018-02-20 08:59:02 +09:00
Akihiro Motoki 61ea0b6a76 Make create_mocks compatible with create_stubs
create_mocks was introduced to mock multiple methods easily,
but its arguments are not compatible with create_stubs for mox.
This requires us to make many many small changes when converting
mox tests into mock. It would be nice if create_mocks is compatible
with create_stubs.

Part of blueprint mock-framework-in-unit-tests
Change-Id: I3d98448b1b9c517c194d692d6b3b53dc00e46223
2018-02-20 08:58:29 +09:00
Akihiro Motoki 5a82ff9328 Switch Keystone API tests to mock
This patch changes mox to mock for the following test modules:
* openstack_dashboard/test/unit/api/test_keystone.py
* openstack_dashboard/test/unit/api/rest/test_keystone.py

Partially-Implements: blueprint mock-framework-in-unit-tests

Change-Id: I5b57bd46359466ee28bff1531c8a9bfd93842ebe
2018-02-17 04:30:41 +09:00
Akihiro Motoki 1a252cb5e8 django2: is_authenticated/is_anonymous is now property only
https://docs.djangoproject.com/en/2.0/releases/1.10/#user-is-auth-anon-deprecation

blueprint django2-support
Change-Id: I57a39417f0595eae8d1c06d7e61d0a67078bb231
2018-02-17 01:36:54 +09:00
Akihiro Motoki e477eafa45 django2: Replace django.core.urlresolves with django.urls
(In Django 2.0) The django.core.urlresolvers module is removed
in favor of its new location, django.urls.
It was depreacted in Django 1.10:
https://docs.djangoproject.com/en/2.0/releases/1.10/#id3

blueprint django2-support
Change-Id: I46ab5c325491274b8eaffbf848e5d80f83c2fd26
2018-02-17 01:36:48 +09:00
Akihiro Motoki 5c5159bc09 Switch swift API tests to mock
This patch changes mox to mock for the following test modules:
* openstack_dashboard/test/unit/api/test_swift.py
* openstack_dashboard/test/unit/api/rest/test_swift.py

Partially-Implements: blueprint mock-framework-in-unit-tests

Change-Id: I3c920564b781c9c1c97bd6ee22b834504c94cb60
2018-02-16 13:25:45 +00:00
Akihiro Motoki 79ae866d57 Switch Nova API tests to mock
This patch changes mox to mock for the following test modules:
* openstack_dashboard/test/unit/api/test_nova.py
* openstack_dashboard/test/unit/api/rest/test_nova.py

Partially-Implements: blueprint mock-framework-in-unit-tests

Change-Id: I894ba4f1ab2123ce36f451013d8f7ebd70d9bd0f
2018-02-15 08:37:26 +00:00
Akihiro Motoki 1a2c957fbb Provide a way to disable mox completely
This commit introduces 'use_mox' attribute to TestCase class.
If this is set to False, TestCase does not setup mox.

In addition, a common method to emit the deprecation warning
was introduced in APITestCase.

Part of blueprint mock-framework-in-unit-tests
Change-Id: I1e3df7ea2a867b8a7aa69523bed2beaab9663dc0
2018-02-15 08:36:57 +00:00
Ivan Kolodyazhny dc732488b3 Implement create_mocks decorator to make mocks easier
Co-Authored-By: Akihiro Motoki <amotoki@gmail.com>
Part of blueprint mock-framework-in-unit-tests
Change-Id: I4b9cf6e8ca7f3eb6988bc83f740b45337946e058
2018-01-09 02:44:47 +09:00
Ivan Kolodyazhny 0d1d22c85e Switch Glance-related tests (dashboards.project.images) to mock
Some duplicated test scenarios were deleted. This patch also doesn't
change Selenium tests because it's out of scope of unit tests.

This patch changes mox to mock for the following test module:
* openstack_dashboard/dashboards/project/images/tests.py
* openstack_dashboard/dashboards/project/images/images/tests.py
* openstack_dashboard/dashboards/project/images/tests.py

Change-Id: I951c2e5ee1c133faa2106e1e1fdba5c72d6f63b7
Partially-Implements: blueprint mock-framework-in-unit-tests
2018-01-04 16:37:52 +02:00
Ivan Kolodyazhny ec0b29f996 Switch Glance API tests to mock
This patch changes mox to mock for the following test modules:
* openstack_dashboard/test/api_tests/glance_tests.py

Change-Id: I282bc6a8ceca851b9755c9637790da055e6b4780
Partially-Implements: blueprint mock-framework-in-unit-tests
2017-12-27 16:59:02 +02:00
Akihiro Motoki eac3e7032a Drop Heat related code from horizon
Orchestration tab in the admin info panel needs a discussion.
It seems not to be covered by heat-dashboard yet.

blueprint heat-dashboard-split-out

Change-Id: I56e6edb1f2ac72e2f42d0e9f3291308e67f24cad
2017-12-05 07:38:55 +00:00
Ivan Kolodyazhny 14a350d013 Switch Cinder-related tests to mock
This patch changes mox to mock for the following test module:
* openstack_dashboard/dashboards/admin/volumes/tests.py

Change-Id: Id487feb397207b3544c9398a446077e3f6a39b1a
Partially-Implements: blueprint mock-framework-in-unit-tests
2017-10-23 15:43:41 +00:00