forked from saleor/saleor
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Maciej Korycinski
committed
Mar 20, 2020
1 parent
a45c78d
commit b41b2ed
Showing
1 changed file
with
63 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
from unittest.mock import ANY, Mock | ||
|
||
import requests | ||
from django.core.management import call_command | ||
|
||
from saleor.account.models import ServiceAccount | ||
from saleor.core.permissions import get_permissions | ||
|
||
|
||
def test_createaccount_command_creates_service_account(): | ||
name = "SA name" | ||
permissions = ["account.manage_users", "order.manage_orders"] | ||
call_command("createserviceaccount", name, permission=permissions) | ||
|
||
sa_accounts = ServiceAccount.objects.filter(name=name) | ||
assert len(sa_accounts) == 1 | ||
|
||
sa_account = sa_accounts[0] | ||
tokens = sa_account.tokens.all() | ||
assert len(tokens) == 1 | ||
|
||
|
||
def test_createaccount_command_service_account_has_all_required_permissions(): | ||
name = "SA name" | ||
permission_list = ["account.manage_users", "order.manage_orders"] | ||
expected_permission = get_permissions(permission_list) | ||
call_command("createserviceaccount", name, permission=permission_list) | ||
|
||
sa_accounts = ServiceAccount.objects.filter(name=name) | ||
assert len(sa_accounts) == 1 | ||
sa_account = sa_accounts[0] | ||
assert set(sa_account.permissions.all()) == set(expected_permission) | ||
|
||
|
||
def test_createaccount_command_sends_data_to_target_url(monkeypatch): | ||
mocked_response = Mock() | ||
mocked_response.status_code = 200 | ||
mocked_post = Mock(return_value=mocked_response) | ||
|
||
monkeypatch.setattr(requests, "post", mocked_post) | ||
|
||
name = "SA name" | ||
target_url = "https://ss.shop.com/register" | ||
permissions = [ | ||
"account.manage_users", | ||
] | ||
|
||
call_command( | ||
"createserviceaccount", name, permission=permissions, target_url=target_url | ||
) | ||
|
||
service_account = ServiceAccount.objects.filter(name=name)[0] | ||
token = service_account.tokens.all()[0].auth_token | ||
mocked_post.assert_called_once_with( | ||
target_url, | ||
headers={"x-saleor-domain": "mirumee.com"}, | ||
json={ | ||
"auth_token": token, | ||
"name": "SA name", | ||
"permissions": ["account.manage_users"], | ||
}, | ||
timeout=ANY, | ||
) |