Skip to content

Commit

Permalink
test improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
petrkalos committed Jun 20, 2024
1 parent d3f98da commit 844f134
Show file tree
Hide file tree
Showing 8 changed files with 86 additions and 30 deletions.
9 changes: 3 additions & 6 deletions tests_new/integration_tests/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,9 @@
import os
from munch import DefaultMunch

ENVNAME = os.getenv('ENVNAME', 'dev')

from integration_tests.errors import GqlError

class GqlError(RuntimeError):
def __init__(self, msg):
super().__init__(msg)
ENVNAME = os.getenv('ENVNAME', 'dev')


class Client:
Expand All @@ -30,7 +27,7 @@ def query(self, query: str):
def _get_jwt_token(self):
cognito_client = boto3.client('cognito-idp', region_name=os.getenv('AWS_REGION', 'eu-west-1'))
kwargs = {
'ClientId': os.getenv('COGNITO_CLIENT', False),
'ClientId': os.environ['COGNITO_CLIENT'],
'AuthFlow': 'USER_PASSWORD_AUTH',
'AuthParameters': {
'USERNAME': self.username,
Expand Down
6 changes: 6 additions & 0 deletions tests_new/integration_tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import datetime
import logging
import os
import sys
Expand Down Expand Up @@ -132,3 +133,8 @@ def client4(user4) -> Client:
@pytest.fixture(scope='session')
def clientTenant(userTenant) -> Client:
yield Client(userTenant.username, userTenant.password)


@pytest.fixture(scope='session')
def session_id() -> str:
return datetime.datetime.utcnow().isoformat()
35 changes: 33 additions & 2 deletions tests_new/integration_tests/core/environment/queries.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def create_environment(client, name, group, organizationUri, awsAccountId, regio
}
},
'query': """
mutation CreateEnvironment($input: NewEnvironmentInput) {
mutation CreateEnvironment($input: NewEnvironmentInput!) {
createEnvironment(input: $input) {
environmentUri
label
Expand All @@ -38,7 +38,7 @@ def get_environment(client, environmentUri):
'operationName': 'GetEnvironment',
'variables': {'environmentUri': environmentUri},
'query': """
query GetEnvironment($environmentUri: String) {
query GetEnvironment($environmentUri: String!) {
getEnvironment(environmentUri: $environmentUri) {
environmentUri
created
Expand Down Expand Up @@ -116,3 +116,34 @@ def delete_environment(client, environmentUri, deleteFromAWS=True):
}
response = client.query(query=query)
return response


def update_environment(client, environmentUri, input: dict):
query = {
'operationName': 'UpdateEnvironment',
'variables': {
'environmentUri': environmentUri,
'input': input,
},
'query': """
mutation UpdateEnvironment(
$environmentUri: String!
$input: ModifyEnvironmentInput!
) {
updateEnvironment(environmentUri: $environmentUri, input: $input) {
environmentUri
label
userRoleInEnvironment
SamlGroupName
AwsAccountId
created
parameters {
key
value
}
}
}
""",
}
response = client.query(query=query)
return response
22 changes: 20 additions & 2 deletions tests_new/integration_tests/core/environment/test_environment.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,27 @@
from assertpy import assert_that

from integration_tests.core.environment.queries import update_environment, get_environment
from integration_tests.errors import GqlError


def test_create_env(session_env1):
assert_that(session_env1.stack.status).is_equal_to('CREATE_COMPLETE')


def test_create_env2(session_env2):
assert_that(session_env2.stack.status).is_equal_to('CREATE_COMPLETE')
def test_modify_env(client1, session_env1):
test_description = 'a test description'
env_uri = session_env1.environmentUri
update_environment(client1, env_uri, {'description': test_description})
env = get_environment(client1, env_uri)
assert_that(env.description).is_equal_to(test_description)


def test_modify_env_unauthorized(client2, session_env1):
test_description = 'unauthorized'
env_uri = session_env1.environmentUri
assert_that(update_environment).raises(GqlError).when_called_with(
client2, env_uri, {'description': test_description}
).contains(
'UnauthorizedOperation',
env_uri,
)
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,21 @@


@pytest.fixture(scope='session')
def org1(client1, group1):
def org1(client1, group1, session_id):
"""
Session org owned by group1
"""
org = create_organization(client1, 'organization1', group1)
org = create_organization(client1, 'organization1', group1, tags=[session_id])
yield org
archive_organization(client1, org.organizationUri)


@pytest.fixture(scope='session')
def org2(client1, group1, group2):
def org2(client1, group1, group2, session_id):
"""
Session org owned by group1 and invite group2
"""
org = create_organization(client1, 'organization2', group1)
org = create_organization(client1, 'organization2', group1, tags=[session_id])
invite_team_to_organization(client=client1, organizationUri=org.organizationUri, group=group2)
yield org
archive_organization(client1, org.organizationUri)
Expand Down
4 changes: 2 additions & 2 deletions tests_new/integration_tests/core/organizations/queries.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
# TODO: This file will be replaced by using the SDK directly
def create_organization(client, name, group):
def create_organization(client, name, group, tags=[]):
query = {
'operationName': 'CreateOrg',
'variables': {
'input': {
'label': name,
'SamlGroupName': group,
'description': 'Created for integration testing',
'tags': [],
'tags': tags,
}
},
'query': """mutation CreateOrg($input: NewOrganizationInput) {
Expand Down
29 changes: 15 additions & 14 deletions tests_new/integration_tests/core/organizations/test_organization.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from assertpy import assert_that

from .queries import (
from integration_tests.core.organizations.queries import (
archive_organization,
create_organization,
get_organization,
Expand All @@ -9,6 +9,7 @@
remove_team_from_organization,
update_organization,
)
from integration_tests.errors import GqlError


def test_create_organization_with_team_with_permissions(org1):
Expand All @@ -23,7 +24,7 @@ def test_create_organization_with_team_with_permissions(org1):
def test_create_organization_with_unauthorized_team(client_noTenantPermissions, group4):
# Given a user with no tenant permissions to MANAGE ORGANIZATIONS
# When it creates an organization
assert_that(create_organization).raises(RuntimeError).when_called_with(
assert_that(create_organization).raises(GqlError).when_called_with(
client_noTenantPermissions,
'organization2',
group4,
Expand Down Expand Up @@ -55,7 +56,7 @@ def test_get_organization_organization_with_invited_team(client2, org2):


def test_get_organization_with_unauthorized_team(client3, org1):
assert_that(get_organization).raises(RuntimeError).when_called_with(
assert_that(get_organization).raises(GqlError).when_called_with(
client=client3,
organizationUri=org1.organizationUri,
).contains(
Expand All @@ -65,26 +66,26 @@ def test_get_organization_with_unauthorized_team(client3, org1):
)


def test_list_organizations_with_admin_team(client1, org1, org2):
def test_list_organizations_with_admin_team(client1, org1, org2, session_id):
# Given 2 organizations
# When the admin user of both of them
response = list_organizations(client1)
response = list_organizations(client1, term=session_id)
# Then
assert_that(response.count).is_equal_to(2)


def test_list_organizations_with_invited_team(client2, org1, org2):
def test_list_organizations_with_invited_team(client2, org1, org2, session_id):
# Given 2 organizations
# When an invited user to one organization only
response = list_organizations(client2)
response = list_organizations(client2, term=session_id)
# Then
assert_that(response.count).is_equal_to(1)


def test_list_organizations_with_unauthorized_team(client3, org1, org2):
def test_list_organizations_with_unauthorized_team(client3, org1, org2, session_id):
# Given 2 organizations
# When a non-invited user
response = list_organizations(client3)
response = list_organizations(client3, term=session_id)
# Then
assert_that(response.count).is_equal_to(0)

Expand All @@ -101,7 +102,7 @@ def test_update_organization_organization_with_admin_team(client1, org1):


def test_update_organization_organization_with_unauthorized_team(client3, org1):
assert_that(update_organization).raises(RuntimeError).when_called_with(
assert_that(update_organization).raises(GqlError).when_called_with(
client=client3,
organizationUri=org1.organizationUri,
).contains(
Expand All @@ -121,7 +122,7 @@ def test_invite_group_to_organization_with_admin_team(org2):


def test_invite_group_to_organization_with_unauthorized_team(client3, org1, group2):
assert_that(invite_team_to_organization).raises(RuntimeError).when_called_with(
assert_that(invite_team_to_organization).raises(GqlError).when_called_with(
client=client3,
organizationUri=org1.organizationUri,
group=group2,
Expand All @@ -142,7 +143,7 @@ def test_remove_group_from_organization_with_admin_team(client1, org2, group2):


def test_remove_group_from_organization_with_unauthorized_team(client3, org2, group2):
assert_that(remove_team_from_organization).raises(RuntimeError).when_called_with(
assert_that(remove_team_from_organization).raises(GqlError).when_called_with(
client=client3,
organizationUri=org2.organizationUri,
group=group2,
Expand All @@ -163,7 +164,7 @@ def test_archive_organization_organization_with_admin_team(client1, group1):


def test_archive_organization_organization_with_invited_team(client2, org2):
assert_that(archive_organization).raises(RuntimeError).when_called_with(
assert_that(archive_organization).raises(GqlError).when_called_with(
client=client2,
organizationUri=org2.organizationUri,
).contains(
Expand All @@ -174,7 +175,7 @@ def test_archive_organization_organization_with_invited_team(client2, org2):


def test_archive_organization_organization_with_unauthorized_team(client3, org1):
assert_that(archive_organization).raises(RuntimeError).when_called_with(
assert_that(archive_organization).raises(GqlError).when_called_with(
client=client3,
organizationUri=org1.organizationUri,
).contains(
Expand Down
3 changes: 3 additions & 0 deletions tests_new/integration_tests/errors.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class GqlError(RuntimeError):
def __init__(self, msg):
super().__init__(msg)

0 comments on commit 844f134

Please sign in to comment.