-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add update self hosted settings mutation (#559)
- Loading branch information
1 parent
e1af712
commit ad629f2
Showing
13 changed files
with
252 additions
and
1 deletion.
There are no files selected for viewing
61 changes: 61 additions & 0 deletions
61
codecov_auth/commands/owner/interactors/tests/test_update_self_hosted_settings.py
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,61 @@ | ||
from unittest.mock import patch | ||
|
||
import pytest | ||
from asgiref.sync import async_to_sync | ||
from django.contrib.auth.models import AnonymousUser | ||
from django.test import TransactionTestCase, override_settings | ||
|
||
from codecov.commands.exceptions import Unauthenticated, ValidationError | ||
from codecov_auth.commands.owner.interactors.update_self_hosted_settings import ( | ||
UpdateSelfHostedSettingsInteractor, | ||
) | ||
from codecov_auth.tests.factories import OwnerFactory | ||
|
||
|
||
class UpdateSelfHostedSettingsInteractorTest(TransactionTestCase): | ||
@async_to_sync | ||
def execute( | ||
self, | ||
current_user, | ||
input={ | ||
"shouldAutoActivate": None, | ||
}, | ||
): | ||
return UpdateSelfHostedSettingsInteractor(None, "github", current_user).execute( | ||
input=input, | ||
) | ||
|
||
@override_settings(IS_ENTERPRISE=True) | ||
def test_update_self_hosted_settings_when_auto_activate_is_true(self): | ||
owner = OwnerFactory(plan_auto_activate=False) | ||
self.execute(current_user=owner, input={"shouldAutoActivate": True}) | ||
owner.refresh_from_db() | ||
assert owner.plan_auto_activate == True | ||
|
||
@override_settings(IS_ENTERPRISE=True) | ||
def test_update_self_hosted_settings_when_auto_activate_is_false(self): | ||
owner = OwnerFactory(plan_auto_activate=True) | ||
self.execute(current_user=owner, input={"shouldAutoActivate": False}) | ||
owner.refresh_from_db() | ||
assert owner.plan_auto_activate == False | ||
|
||
@override_settings(IS_ENTERPRISE=False) | ||
def test_validation_error_when_not_self_hosted_instance(self): | ||
owner = OwnerFactory(plan_auto_activate=True) | ||
with pytest.raises(ValidationError): | ||
self.execute( | ||
current_user=owner, | ||
input={ | ||
"shouldAutoActivate": False, | ||
}, | ||
) | ||
|
||
@override_settings(IS_ENTERPRISE=True) | ||
def test_user_is_not_authenticated(self): | ||
with pytest.raises(Unauthenticated) as e: | ||
self.execute( | ||
current_user=AnonymousUser(), | ||
input={ | ||
"shouldAutoActivate": False, | ||
}, | ||
) |
38 changes: 38 additions & 0 deletions
38
codecov_auth/commands/owner/interactors/update_self_hosted_settings.py
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,38 @@ | ||
from dataclasses import dataclass | ||
|
||
from django.conf import settings | ||
|
||
import services.self_hosted as self_hosted | ||
from codecov.commands.base import BaseInteractor | ||
from codecov.commands.exceptions import Unauthenticated, ValidationError | ||
from codecov.db import sync_to_async | ||
from services.refresh import RefreshService | ||
|
||
|
||
@dataclass | ||
class UpdateSelfHostedSettingsInput: | ||
auto_activate_members: bool = False | ||
|
||
|
||
class UpdateSelfHostedSettingsInteractor(BaseInteractor): | ||
def validate(self) -> None: | ||
if not self.current_user.is_authenticated: | ||
raise Unauthenticated() | ||
|
||
if not settings.IS_ENTERPRISE: | ||
raise ValidationError( | ||
"enable_autoactivation and disable_autoactivation are only available in self-hosted environments" | ||
) | ||
|
||
@sync_to_async | ||
def execute(self, input: UpdateSelfHostedSettingsInput) -> None: | ||
self.validate() | ||
typed_input = UpdateSelfHostedSettingsInput( | ||
auto_activate_members=input.get("shouldAutoActivate"), | ||
) | ||
|
||
should_auto_activate = typed_input.auto_activate_members | ||
if should_auto_activate: | ||
self_hosted.enable_autoactivation() | ||
else: | ||
self_hosted.disable_autoactivation() |
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
70 changes: 70 additions & 0 deletions
70
graphql_api/tests/mutation/test_update_self_hosted_settings.py
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,70 @@ | ||
import pytest | ||
from django.test import TransactionTestCase, override_settings | ||
|
||
from codecov.commands.exceptions import ValidationError | ||
from codecov_auth.tests.factories import OwnerFactory | ||
from graphql_api.tests.helper import GraphQLTestHelper | ||
|
||
query = """ | ||
mutation($input: UpdateSelfHostedSettingsInput!) { | ||
updateSelfHostedSettings(input: $input) { | ||
error { | ||
__typename | ||
... on ResolverError { | ||
message | ||
} | ||
} | ||
} | ||
} | ||
""" | ||
|
||
|
||
class UpdateSelfHostedSettingsTest(GraphQLTestHelper, TransactionTestCase): | ||
def _request(self, owner=None): | ||
return self.gql_request( | ||
query, | ||
variables={"input": {"shouldAutoActivate": True}}, | ||
owner=owner, | ||
) | ||
|
||
def _request_deactivate(self, owner=None): | ||
return self.gql_request( | ||
query, | ||
variables={"input": {"shouldAutoActivate": False}}, | ||
owner=owner, | ||
) | ||
|
||
@override_settings(IS_ENTERPRISE=True) | ||
def test_unauthenticated(self): | ||
assert self._request() == { | ||
"updateSelfHostedSettings": { | ||
"error": { | ||
"__typename": "UnauthenticatedError", | ||
"message": "You are not authenticated", | ||
} | ||
} | ||
} | ||
|
||
@override_settings(IS_ENTERPRISE=True) | ||
def test_authenticated_enable_autoactivation(self): | ||
owner = OwnerFactory() | ||
assert self._request(owner=owner) == {"updateSelfHostedSettings": None} | ||
|
||
@override_settings(IS_ENTERPRISE=True) | ||
def test_authenticate_disable_autoactivation(self): | ||
owner = OwnerFactory() | ||
assert self._request_deactivate(owner=owner) == { | ||
"updateSelfHostedSettings": None | ||
} | ||
|
||
@override_settings(IS_ENTERPRISE=False) | ||
def test_invalid_settings(self): | ||
owner = OwnerFactory() | ||
assert self._request(owner=owner) == { | ||
"updateSelfHostedSettings": { | ||
"error": { | ||
"__typename": "ValidationError", | ||
"message": "enable_autoactivation and disable_autoactivation are only available in self-hosted environments", | ||
} | ||
} | ||
} |
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
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
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
10 changes: 10 additions & 0 deletions
10
graphql_api/types/mutation/update_self_hosted_settings/__init__.py
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,10 @@ | ||
from graphql_api.helpers.ariadne import ariadne_load_local_graphql | ||
|
||
from .update_self_hosted_settings import ( | ||
error_update_self_hosted_settings, | ||
resolve_update_self_hosted_settings, | ||
) | ||
|
||
gql_update_self_hosted_settings = ariadne_load_local_graphql( | ||
__file__, "update_self_hosted_settings.graphql" | ||
) |
9 changes: 9 additions & 0 deletions
9
graphql_api/types/mutation/update_self_hosted_settings/update_self_hosted_settings.graphql
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,9 @@ | ||
union UpdateSelfHostedSettingsError = UnauthenticatedError | ValidationError | ||
|
||
type UpdateSelfHostedSettingsPayload { | ||
error: UpdateSelfHostedSettingsError | ||
} | ||
|
||
input UpdateSelfHostedSettingsInput { | ||
shouldAutoActivate: Boolean! | ||
} |
19 changes: 19 additions & 0 deletions
19
graphql_api/types/mutation/update_self_hosted_settings/update_self_hosted_settings.py
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,19 @@ | ||
from ariadne import UnionType | ||
|
||
from codecov_auth.commands.owner import OwnerCommands | ||
from graphql_api.helpers.mutation import ( | ||
require_authenticated, | ||
resolve_union_error_type, | ||
wrap_error_handling_mutation, | ||
) | ||
|
||
|
||
@wrap_error_handling_mutation | ||
@require_authenticated | ||
async def resolve_update_self_hosted_settings(_, info, input): | ||
command: OwnerCommands = info.context["executor"].get_command("owner") | ||
return await command.update_self_hosted_settings(input) | ||
|
||
|
||
error_update_self_hosted_settings = UnionType("UpdateSelfHostedSettingsError") | ||
error_update_self_hosted_settings.type_resolver(resolve_union_error_type) |