-
Notifications
You must be signed in to change notification settings - Fork 18
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
1 parent
7b68c36
commit 072c9b6
Showing
3 changed files
with
85 additions
and
11 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
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 |
---|---|---|
@@ -1,17 +1,39 @@ | ||
import time_machine | ||
from google_api.models import GoogleCloudOAuthCredential, UsedRequestQuota | ||
import pytest | ||
|
||
|
||
pytestmark = pytest.mark.django_db | ||
|
||
|
||
# def test_get_available_credentials(): | ||
# credential = GoogleCloudOAuthCredential.objects.create( | ||
# quota_limit_for_youtube=10_000, | ||
# ) | ||
def test_with_quota_left(): | ||
credential = GoogleCloudOAuthCredential.objects.create( | ||
quota_limit_for_youtube=10_000, | ||
) | ||
|
||
# found = GoogleCloudOAuthCredential.get_available_credentials( | ||
# service="youtube", | ||
# min_quota=1600, | ||
# ) | ||
with time_machine.travel("2023-10-10 00:00:00", tick=False): | ||
result = GoogleCloudOAuthCredential.objects.with_quota_left("youtube").get() | ||
assert result.youtube_quota_left == 10_000 | ||
|
||
# assert found.id == credential.id | ||
UsedRequestQuota.objects.create( | ||
credentials=credential, | ||
cost=1000, | ||
service="youtube", | ||
) | ||
|
||
result = GoogleCloudOAuthCredential.objects.with_quota_left("youtube").get() | ||
assert result.youtube_quota_left == 9_000 | ||
|
||
with time_machine.travel("2023-10-10 08:00:00", tick=False): | ||
result = GoogleCloudOAuthCredential.objects.with_quota_left("youtube").get() | ||
assert result.youtube_quota_left == 10_000 | ||
|
||
|
||
def test_get_by_client_id(): | ||
credential = GoogleCloudOAuthCredential.objects.create(client_id="test123") | ||
|
||
assert ( | ||
GoogleCloudOAuthCredential.objects.get_by_client_id("test123").id | ||
== credential.id | ||
) | ||
assert GoogleCloudOAuthCredential.objects.get_by_client_id("invalid") is None |
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,53 @@ | ||
import datetime | ||
import time_machine | ||
from google_api.models import GoogleCloudOAuthCredential, GoogleCloudToken | ||
from google_api.exceptions import NoGoogleCloudQuotaLeftError | ||
from google_api.sdk import count_quota, get_available_credentials | ||
import pytest | ||
|
||
pytestmark = pytest.mark.django_db | ||
|
||
|
||
def test_get_available_credentials(admin_user): | ||
stored_credential = GoogleCloudOAuthCredential.objects.create() | ||
GoogleCloudToken.objects.create( | ||
oauth_credential=stored_credential, token="token", admin_user=admin_user | ||
) | ||
|
||
available_credentials = get_available_credentials("youtube", 1000) | ||
|
||
assert available_credentials.token == "token" | ||
|
||
|
||
def test_get_available_credentials_fails_when_no_quota_is_left(admin_user): | ||
stored_credential = GoogleCloudOAuthCredential.objects.create( | ||
quota_limit_for_youtube=500 | ||
) | ||
GoogleCloudToken.objects.create( | ||
oauth_credential=stored_credential, token="token", admin_user=admin_user | ||
) | ||
|
||
with pytest.raises(NoGoogleCloudQuotaLeftError): | ||
get_available_credentials("youtube", 1000) | ||
|
||
|
||
def test_count_quota(admin_user): | ||
stored_credential = GoogleCloudOAuthCredential.objects.create() | ||
GoogleCloudToken.objects.create( | ||
oauth_credential=stored_credential, token="token", admin_user=admin_user | ||
) | ||
|
||
@count_quota("youtube", 1000) | ||
def test_function(*, credentials): | ||
return credentials | ||
|
||
with time_machine.travel("2023-10-10 12:00:00", tick=False): | ||
credentials = test_function() | ||
|
||
assert credentials.token == "token" | ||
assert stored_credential.usedrequestquota_set.count() == 1 | ||
|
||
used_quota = stored_credential.usedrequestquota_set.first() | ||
assert used_quota.cost == 1000 | ||
assert used_quota.service == "youtube" | ||
assert used_quota.used_at == datetime.datetime.now(tz=datetime.timezone.utc) |