2019-12-26 13:23:53 +00:00
import boto3
2023-11-30 15:55:51 +00:00
import pytest
from botocore . exceptions import ClientError
2019-12-26 13:23:53 +00:00
2024-01-07 12:03:33 +00:00
from moto import mock_aws
2022-08-13 09:49:43 +00:00
from moto . core import DEFAULT_ACCOUNT_ID as ACCOUNT_ID
2019-12-26 13:23:53 +00:00
2024-01-07 12:03:33 +00:00
@mock_aws
2019-12-26 13:23:53 +00:00
def test_create_repository ( ) :
client = boto3 . client ( " codecommit " , region_name = " eu-central-1 " )
2023-07-08 10:25:06 +00:00
metadata = client . create_repository (
2019-12-26 14:02:24 +00:00
repositoryName = " repository_one " , repositoryDescription = " description repo one "
2023-07-08 10:25:06 +00:00
) [ " repositoryMetadata " ]
2019-12-26 13:23:53 +00:00
2023-07-08 10:25:06 +00:00
assert metadata [ " creationDate " ] is not None
assert metadata [ " lastModifiedDate " ] is not None
assert metadata [ " repositoryId " ] is not None
assert metadata [ " repositoryName " ] == " repository_one "
assert metadata [ " repositoryDescription " ] == " description repo one "
assert (
metadata [ " cloneUrlSsh " ]
== " ssh://git-codecommit.eu-central-1.amazonaws.com/v1/repos/repository_one "
2019-12-26 14:02:24 +00:00
)
2023-07-08 10:25:06 +00:00
assert (
metadata [ " cloneUrlHttp " ]
== " https://git-codecommit.eu-central-1.amazonaws.com/v1/repos/repository_one "
2019-12-26 14:02:24 +00:00
)
2023-07-08 10:25:06 +00:00
assert (
metadata [ " Arn " ]
== f " arn:aws:codecommit:eu-central-1: { ACCOUNT_ID } :repository_one "
2019-12-26 14:02:24 +00:00
)
2023-07-08 10:25:06 +00:00
assert metadata [ " accountId " ] == ACCOUNT_ID
2019-12-26 13:23:53 +00:00
2019-12-26 15:06:53 +00:00
2024-01-07 12:03:33 +00:00
@mock_aws
2019-12-26 15:06:53 +00:00
def test_create_repository_without_description ( ) :
client = boto3 . client ( " codecommit " , region_name = " eu-central-1 " )
2023-07-08 10:25:06 +00:00
metadata = client . create_repository ( repositoryName = " repository_two " ) [
" repositoryMetadata "
]
2019-12-26 13:23:53 +00:00
2023-07-08 10:25:06 +00:00
assert metadata . get ( " repositoryName " ) == " repository_two "
assert metadata . get ( " repositoryDescription " ) is None
assert metadata [ " creationDate " ] is not None
assert metadata [ " lastModifiedDate " ] is not None
assert metadata [ " repositoryId " ] is not None
assert (
metadata [ " cloneUrlSsh " ]
== " ssh://git-codecommit.eu-central-1.amazonaws.com/v1/repos/repository_two "
2019-12-26 15:06:53 +00:00
)
2023-07-08 10:25:06 +00:00
assert (
metadata [ " cloneUrlHttp " ]
== " https://git-codecommit.eu-central-1.amazonaws.com/v1/repos/repository_two "
2019-12-26 15:06:53 +00:00
)
2023-07-08 10:25:06 +00:00
assert (
metadata [ " Arn " ]
== f " arn:aws:codecommit:eu-central-1: { ACCOUNT_ID } :repository_two "
2019-12-26 15:06:53 +00:00
)
2023-07-08 10:25:06 +00:00
assert metadata [ " accountId " ] == ACCOUNT_ID
2019-12-26 15:06:53 +00:00
2024-01-07 12:03:33 +00:00
@mock_aws
2019-12-26 15:06:53 +00:00
def test_create_repository_repository_name_exists ( ) :
client = boto3 . client ( " codecommit " , region_name = " eu-central-1 " )
client . create_repository ( repositoryName = " repository_two " )
2019-12-26 13:23:53 +00:00
2020-10-06 05:54:49 +00:00
with pytest . raises ( ClientError ) as e :
2019-12-26 15:06:53 +00:00
client . create_repository (
repositoryName = " repository_two " ,
repositoryDescription = " description repo two " ,
)
2020-10-06 06:04:09 +00:00
ex = e . value
2023-07-08 10:25:06 +00:00
assert ex . operation_name == " CreateRepository "
assert ex . response [ " ResponseMetadata " ] [ " HTTPStatusCode " ] == 400
assert ex . response [ " Error " ] [ " Code " ] == " RepositoryNameExistsException "
assert (
ex . response [ " Error " ] [ " Message " ]
== " Repository named repository_two already exists "
2019-12-26 14:02:24 +00:00
)
2019-12-26 13:23:53 +00:00
2024-01-07 12:03:33 +00:00
@mock_aws
2019-12-26 15:06:53 +00:00
def test_create_repository_invalid_repository_name ( ) :
client = boto3 . client ( " codecommit " , region_name = " eu-central-1 " )
2020-10-06 05:54:49 +00:00
with pytest . raises ( ClientError ) as e :
2019-12-26 15:06:53 +00:00
client . create_repository ( repositoryName = " in_123_valid_@#$_characters " )
2020-10-06 06:04:09 +00:00
ex = e . value
2023-07-08 10:25:06 +00:00
assert ex . operation_name == " CreateRepository "
assert ex . response [ " ResponseMetadata " ] [ " HTTPStatusCode " ] == 400
assert ex . response [ " Error " ] [ " Code " ] == " InvalidRepositoryNameException "
assert (
ex . response [ " Error " ] [ " Message " ]
== " The repository name is not valid. Repository names can be any valid combination of letters, numbers, periods, underscores, and dashes between 1 and 100 characters in length. Names are case sensitive. For more information, see Limits in the AWS CodeCommit User Guide. "
2019-12-26 15:06:53 +00:00
)
2024-01-07 12:03:33 +00:00
@mock_aws
2019-12-26 13:23:53 +00:00
def test_get_repository ( ) :
client = boto3 . client ( " codecommit " , region_name = " eu-central-1 " )
2019-12-26 14:02:24 +00:00
repository_name = " repository_one "
2019-12-26 13:23:53 +00:00
client . create_repository (
2019-12-26 14:02:24 +00:00
repositoryName = repository_name , repositoryDescription = " description repo one "
2019-12-26 13:23:53 +00:00
)
2023-07-08 10:25:06 +00:00
metadata = client . get_repository ( repositoryName = repository_name ) [
" repositoryMetadata "
]
2019-12-26 13:23:53 +00:00
2023-07-08 10:25:06 +00:00
assert metadata [ " creationDate " ] is not None
assert metadata [ " lastModifiedDate " ] is not None
assert metadata [ " repositoryId " ] is not None
assert metadata [ " repositoryName " ] == repository_name
assert metadata [ " repositoryDescription " ] == " description repo one "
assert (
metadata [ " cloneUrlSsh " ]
== " ssh://git-codecommit.eu-central-1.amazonaws.com/v1/repos/repository_one "
2019-12-26 14:02:24 +00:00
)
2023-07-08 10:25:06 +00:00
assert (
metadata [ " cloneUrlHttp " ]
== " https://git-codecommit.eu-central-1.amazonaws.com/v1/repos/repository_one "
2019-12-26 14:02:24 +00:00
)
2023-07-08 10:25:06 +00:00
assert (
metadata [ " Arn " ]
== f " arn:aws:codecommit:eu-central-1: { ACCOUNT_ID } :repository_one "
2019-12-26 14:02:24 +00:00
)
2023-07-08 10:25:06 +00:00
assert metadata [ " accountId " ] == ACCOUNT_ID
2019-12-26 13:23:53 +00:00
client = boto3 . client ( " codecommit " , region_name = " us-east-1 " )
2020-10-06 05:54:49 +00:00
with pytest . raises ( ClientError ) as e :
2019-12-26 14:02:24 +00:00
client . get_repository ( repositoryName = repository_name )
2020-10-06 06:04:09 +00:00
ex = e . value
2023-07-08 10:25:06 +00:00
assert ex . operation_name == " GetRepository "
assert ex . response [ " ResponseMetadata " ] [ " HTTPStatusCode " ] == 400
assert ex . response [ " Error " ] [ " Code " ] == " RepositoryDoesNotExistException "
assert ex . response [ " Error " ] [ " Message " ] == f " { repository_name } does not exist "
2019-12-26 13:23:53 +00:00
2024-01-07 12:03:33 +00:00
@mock_aws
2019-12-26 15:06:53 +00:00
def test_get_repository_invalid_repository_name ( ) :
2019-12-26 13:23:53 +00:00
client = boto3 . client ( " codecommit " , region_name = " eu-central-1 " )
2020-10-06 05:54:49 +00:00
with pytest . raises ( ClientError ) as e :
2019-12-26 15:06:53 +00:00
client . get_repository ( repositoryName = " repository_one-@#@ " )
2020-10-06 06:04:09 +00:00
ex = e . value
2023-07-08 10:25:06 +00:00
assert ex . response [ " ResponseMetadata " ] [ " HTTPStatusCode " ] == 400
assert ex . response [ " Error " ] [ " Code " ] == " InvalidRepositoryNameException "
assert (
ex . response [ " Error " ] [ " Message " ]
== " The repository name is not valid. Repository names can be any valid combination of letters, numbers, periods, underscores, and dashes between 1 and 100 characters in length. Names are case sensitive. For more information, see Limits in the AWS CodeCommit User Guide. "
2019-12-26 14:02:24 +00:00
)
2019-12-26 13:23:53 +00:00
2024-01-07 12:03:33 +00:00
@mock_aws
2019-12-26 13:23:53 +00:00
def test_delete_repository ( ) :
client = boto3 . client ( " codecommit " , region_name = " us-east-1 " )
2019-12-26 14:02:24 +00:00
response = client . create_repository ( repositoryName = " repository_one " )
2019-12-26 13:23:53 +00:00
repository_id_create = response . get ( " repositoryMetadata " ) . get ( " repositoryId " )
2019-12-26 14:02:24 +00:00
response = client . delete_repository ( repositoryName = " repository_one " )
2019-12-26 13:23:53 +00:00
2023-07-08 10:25:06 +00:00
assert response . get ( " repositoryId " ) is not None
assert repository_id_create == response . get ( " repositoryId " )
2019-12-26 13:23:53 +00:00
2019-12-26 14:02:24 +00:00
response = client . delete_repository ( repositoryName = " unknown_repository " )
2019-12-26 13:23:53 +00:00
2023-07-08 10:25:06 +00:00
assert response . get ( " repositoryId " ) is None
2019-12-26 13:23:53 +00:00
2024-01-07 12:03:33 +00:00
@mock_aws
2019-12-26 13:23:53 +00:00
def test_delete_repository_invalid_repository_name ( ) :
client = boto3 . client ( " codecommit " , region_name = " us-east-1 " )
2020-10-06 05:54:49 +00:00
with pytest . raises ( ClientError ) as e :
2019-12-26 14:02:24 +00:00
client . delete_repository ( repositoryName = " _rep@ository_one " )
2020-10-06 06:04:09 +00:00
ex = e . value
2023-07-08 10:25:06 +00:00
assert ex . operation_name == " DeleteRepository "
assert ex . response [ " ResponseMetadata " ] [ " HTTPStatusCode " ] == 400
assert ex . response [ " Error " ] [ " Code " ] == " InvalidRepositoryNameException "
assert (
ex . response [ " Error " ] [ " Message " ]
== " The repository name is not valid. Repository names can be any valid combination of letters, numbers, periods, underscores, and dashes between 1 and 100 characters in length. Names are case sensitive. For more information, see Limits in the AWS CodeCommit User Guide. "
2019-12-26 14:02:24 +00:00
)