internlm/SWE-Fixer-Train-110K huggingface dataset:
Repository: getmoto/moto

Issue:
Running lambda invoke with govcloud results in a KeyError
moto version: 1.3.3
botocore version: 1.10.4

When using moto to invoke a lambda function on a govcloud region, you run into a key error with the lambda_backends.  This is because boto.awslambda.regions() does not include the govcloud region, despite it being available for use.


I've made a pull request that fixes the issue: #1613

Trace of the error:
```
Traceback (most recent call last):
  File "/Users/eric/nimbis/sites/tss/apps/session_aws/tasks/dns.py", line 84, in run
    Payload=lambda_payload)
  File "/Users/eric/.virtualenvs/tss/lib/python2.7/site-packages/botocore/client.py", line 314, in _api_call
    return self._make_api_call(operation_name, kwargs)
  File "/Users/eric/.virtualenvs/tss/lib/python2.7/site-packages/botocore/client.py", line 599, in _make_api_call
    operation_model, request_dict)
  File "/Users/eric/.virtualenvs/tss/lib/python2.7/site-packages/botocore/endpoint.py", line 148, in make_request
    return self._send_request(request_dict, operation_model)
  File "/Users/eric/.virtualenvs/tss/lib/python2.7/site-packages/botocore/endpoint.py", line 177, in _send_request
    success_response, exception):
  File "/Users/eric/.virtualenvs/tss/lib/python2.7/site-packages/botocore/endpoint.py", line 273, in _needs_retry
    caught_exception=caught_exception, request_dict=request_dict)
  File "/Users/eric/.virtualenvs/tss/lib/python2.7/site-packages/botocore/hooks.py", line 227, in emit
    return self._emit(event_name, kwargs)
  File "/Users/eric/.virtualenvs/tss/lib/python2.7/site-packages/botocore/hooks.py", line 210, in _emit
    response = handler(**kwargs)
  File "/Users/eric/.virtualenvs/tss/lib/python2.7/site-packages/botocore/retryhandler.py", line 183, in __call__
    if self._checker(attempts, response, caught_exception):
  File "/Users/eric/.virtualenvs/tss/lib/python2.7/site-packages/botocore/retryhandler.py", line 251, in __call__
    caught_exception)
  File "/Users/eric/.virtualenvs/tss/lib/python2.7/site-packages/botocore/retryhandler.py", line 269, in _should_retry
    return self._checker(attempt_number, response, caught_exception)
  File "/Users/eric/.virtualenvs/tss/lib/python2.7/site-packages/botocore/retryhandler.py", line 317, in __call__
    caught_exception)
  File "/Users/eric/.virtualenvs/tss/lib/python2.7/site-packages/botocore/retryhandler.py", line 223, in __call__
    attempt_number, caught_exception)
  File "/Users/eric/.virtualenvs/tss/lib/python2.7/site-packages/botocore/retryhandler.py", line 359, in _check_caught_exception
    raise caught_exception
KeyError: u'us-gov-west-1'
```


Discussion:

Resolving patch:
diff --git a/moto/awslambda/models.py b/moto/awslambda/models.py
--- a/moto/awslambda/models.py
+++ b/moto/awslambda/models.py
@@ -675,3 +675,4 @@ def do_validate_s3():
                    for _region in boto.awslambda.regions()}
 
 lambda_backends['ap-southeast-2'] = LambdaBackend('ap-southeast-2')
+lambda_backends['us-gov-west-1'] = LambdaBackend('us-gov-west-1')


Test patch:
Repository: getmoto/moto

Issue:
Moto CreateTopic for SNS uses incorrect regex
AWS allows me to create an SNS topic with leading underscores (`__app-ses-bounces`).

Moto (1.3.3) does not.

The cause is the regex incorrectly insists that the topic name may not begin or end with underscores, which makes no sense given that AWS documentation states:

> Topic names are limited to 256 characters. Alphanumeric characters plus hyphens (-) and underscores (_) are allowed. Topic names must be unique within an AWS account.

That's all the restrictions in AWS SNS provides. I have personally tested topics that starts and ends with `-` and `_`.


According to http://boto3.readthedocs.io/en/latest/reference/services/sns.html#SNS.Client.create_topic one needs at least one character.



Discussion:

Resolving patch:
diff --git a/moto/sns/models.py b/moto/sns/models.py
--- a/moto/sns/models.py
+++ b/moto/sns/models.py
@@ -240,7 +240,7 @@ def update_sms_attributes(self, attrs):
         self.sms_attributes.update(attrs)
 
     def create_topic(self, name):
-        fails_constraints = not re.match(r'^[a-zA-Z0-9](?:[A-Za-z0-9_-]{0,253}[a-zA-Z0-9])?$', name)
+        fails_constraints = not re.match(r'^[a-zA-Z0-9_-]{1,256}$', name)
         if fails_constraints:
             raise InvalidParameterValue("Topic names must be made up of only uppercase and lowercase ASCII letters, numbers, underscores, and hyphens, and must be between 1 and 256 characters long.")
         candidate_topic = Topic(name, self)


Test patch:
diff --git a/tests/test_sns/test_topics_boto3.py b/tests/test_sns/test_topics_boto3.py
--- a/tests/test_sns/test_topics_boto3.py
+++ b/tests/test_sns/test_topics_boto3.py
@@ -13,23 +13,24 @@
 @mock_sns
 def test_create_and_delete_topic():
     conn = boto3.client("sns", region_name="us-east-1")
-    conn.create_topic(Name="some-topic")
+    for topic_name in ('some-topic', '-some-topic-', '_some-topic_', 'a' * 256):
+        conn.create_topic(Name=topic_name)
 
-    topics_json = conn.list_topics()
-    topics = topics_json["Topics"]
-    topics.should.have.length_of(1)
-    topics[0]['TopicArn'].should.equal(
-        "arn:aws:sns:{0}:123456789012:some-topic"
-        .format(conn._client_config.region_name)
-    )
+        topics_json = conn.list_topics()
+        topics = topics_json["Topics"]
+        topics.should.have.length_of(1)
+        topics[0]['TopicArn'].should.equal(
+            "arn:aws:sns:{0}:123456789012:{1}"
+            .format(conn._client_config.region_name, topic_name)
+        )
 
-    # Delete the topic
-    conn.delete_topic(TopicArn=topics[0]['TopicArn'])
+        # Delete the topic
+        conn.delete_topic(TopicArn=topics[0]['TopicArn'])
 
-    # And there should now be 0 topics
-    topics_json = conn.list_topics()
-    topics = topics_json["Topics"]
-    topics.should.have.length_of(0)
+        # And there should now be 0 topics
+        topics_json = conn.list_topics()
+        topics = topics_json["Topics"]
+        topics.should.have.length_of(0)
 
 @mock_sns
 def test_create_topic_should_be_indempodent():
Repository: getmoto/moto

Issue:
Wrong IAM get_user_policy() response
The result given back from get_user_policy() is wrong. 'PolicyDocument' contains the internal dict that is used by moto instead of the clean policy document.
```
import json
from moto import mock_iam
import boto3

iam = boto3.client('iam')
iam.create_user(UserName='testuser')
iam.put_user_policy(
    UserName='testuser',
    PolicyName='testpolicy',
    PolicyDocument=json.dumps({'key': 'value'}))
result = iam.get_user_policy(
    UserName='testuser', PolicyName='testpolicy')
print result
```

Results in:
```
{u'UserName': 'testuser', u'PolicyName': 'testpolicy', u'PolicyDocument': '\n      {u\'policy_document\': u\'{"key": "value"}\', u\'user_name\': u\'testuser\', u\'policy_name\': u\'testpolicy\'}\n      ', 'ResponseMetadata': {'RetryAttempts': 0, 'HTTPStatusCode': 200, 'RequestId': '7a62c49f-347e-4fc4-9331-6e8eEXAMPLE', 'HTTPHeaders': {u'Content-Type': u'text/plain', u'server': u'amazon.com'}}}
```


Discussion:
Hi, I would like to work on this issue. 
Just want to confirm - the expected output is:
```json5
{'UserName': 'testuser', 
'PolicyName': 'testpolicy', 
'PolicyDocument': {'key': 'value'},
'ResponseMetadata': {'RequestId': '7a62c49f-347e-4fc4-9331-6e8eEXAMPLE',
'HTTPStatusCode': 200, 
'HTTPHeaders': {'Content-Type': 'text/plain', 'server': 'amazon.com'}, 
'RetryAttempts': 0}}
```

Thanks!
That looks good
Thanks for opening; going to mark this as a bug.

Resolving patch:
diff --git a/moto/iam/responses.py b/moto/iam/responses.py
--- a/moto/iam/responses.py
+++ b/moto/iam/responses.py
@@ -407,7 +407,7 @@ def get_user_policy(self):
         return template.render(
             user_name=user_name,
             policy_name=policy_name,
-            policy_document=policy_document
+            policy_document=policy_document.get('policy_document')
         )
 
     def list_user_policies(self):


Test patch:
Repository: getmoto/moto

Issue:
IoT service list_things should include thing ARNs
### Actual
`list_things` method of the AWS IoT service returns _thingName_, _attributes_, _thingTypeName_ and _version_

### Expected
The `list_things` method of the AWS IoT service should include the thing ARN (_thingArn_) as described [here](https://boto3.readthedocs.io/en/latest/reference/services/iot.html#IoT.Client.list_things)


Discussion:

Resolving patch:
diff --git a/moto/iot/models.py b/moto/iot/models.py
--- a/moto/iot/models.py
+++ b/moto/iot/models.py
@@ -32,6 +32,7 @@ def __init__(self, thing_name, thing_type, attributes, region_name):
     def to_dict(self, include_default_client_id=False):
         obj = {
             'thingName': self.thing_name,
+            'thingArn': self.arn,
             'attributes': self.attributes,
             'version': self.version
         }


Test patch:
diff --git a/tests/test_iot/test_iot.py b/tests/test_iot/test_iot.py
--- a/tests/test_iot/test_iot.py
+++ b/tests/test_iot/test_iot.py
@@ -35,12 +35,14 @@ def test_things():
     res.should.have.key('things').which.should.have.length_of(1)
     for thing in res['things']:
         thing.should.have.key('thingName').which.should_not.be.none
+        thing.should.have.key('thingArn').which.should_not.be.none
 
     thing = client.update_thing(thingName=name, attributePayload={'attributes': {'k1': 'v1'}})
     res = client.list_things()
     res.should.have.key('things').which.should.have.length_of(1)
     for thing in res['things']:
         thing.should.have.key('thingName').which.should_not.be.none
+        thing.should.have.key('thingArn').which.should_not.be.none
     res['things'][0]['attributes'].should.have.key('k1').which.should.equal('v1')
 
     thing = client.describe_thing(thingName=name)
Repository: getmoto/moto

Issue:
[SES] Does not properly verify mailbox with display name
https://tools.ietf.org/html/rfc2822.html#section-3.4 defines two forms of valid mailbox:

* `foo@example.com`
* `"Foo Bar" <foo@example.com>`

SES supports both of these forms. Per https://github.com/spulec/moto/blob/master/moto/ses/models.py#L55, only the first form is supported by moto.


Discussion:

Resolving patch:
diff --git a/moto/ses/models.py b/moto/ses/models.py
--- a/moto/ses/models.py
+++ b/moto/ses/models.py
@@ -49,7 +49,8 @@ def __init__(self):
         self.sent_messages = []
         self.sent_message_count = 0
 
-    def _is_verified_address(self, address):
+    def _is_verified_address(self, source):
+        _, address = parseaddr(source)
         if address in self.addresses:
             return True
         user, host = address.split('@', 1)


Test patch:
Repository: getmoto/moto

Issue:
Decorating a class breaks staticmethods
If a test class is decorated `@moto.mock_s3`:

> moto/core/models.py:71: in wrapper
```python
    result = func(*args, **kwargs)
```

causes the instance to be passed to `func` even if it's `@staticmethod` decorated.

I have not tested with the other mocks, but that seems to be a generic wrapper, so I assume all are affected.


Discussion:

Resolving patch:
diff --git a/moto/core/models.py b/moto/core/models.py
--- a/moto/core/models.py
+++ b/moto/core/models.py
@@ -89,6 +89,17 @@ def decorate_class(self, klass):
             if inspect.ismethod(attr_value) and attr_value.__self__ is klass:
                 continue
 
+            # Check if this is a staticmethod. If so, skip patching
+            for cls in inspect.getmro(klass):
+                if attr_value.__name__ not in cls.__dict__:
+                    continue
+                bound_attr_value = cls.__dict__[attr_value.__name__]
+                if not isinstance(bound_attr_value, staticmethod):
+                    break
+            else:
+                # It is a staticmethod, skip patching
+                continue
+
             try:
                 setattr(klass, attr, self(attr_value, reset=False))
             except TypeError:


Test patch:
diff --git a/tests/test_core/test_decorator_calls.py b/tests/test_core/test_decorator_calls.py
--- a/tests/test_core/test_decorator_calls.py
+++ b/tests/test_core/test_decorator_calls.py
@@ -85,3 +85,14 @@ def setUp(self):
     def test_still_the_same(self):
         bucket = self.conn.get_bucket('mybucket')
         bucket.name.should.equal("mybucket")
+
+
+@mock_s3_deprecated
+class TesterWithStaticmethod(object):
+
+    @staticmethod
+    def static(*args):
+        assert not args or not isinstance(args[0], TesterWithStaticmethod)
+
+    def test_no_instance_sent_to_staticmethod(self):
+        self.static()
Repository: getmoto/moto

Issue:
CloudFormation default resources does not include AWS::Partition
Parsing CloudFormation templates generated by [Serverless](www.serverless.com) result in a KeyError: 'AWS::Partition' being thrown. This is caused by Serverless using the AWS::Partition resource which is not defined.

The following gist is an example of a serverless cloudformation template: https://gist.github.com/justinm/91a315ec604fc7c6c33ff89f7ce7fff8


Discussion:

Resolving patch:
diff --git a/moto/cloudformation/parsing.py b/moto/cloudformation/parsing.py
--- a/moto/cloudformation/parsing.py
+++ b/moto/cloudformation/parsing.py
@@ -387,6 +387,7 @@ def __init__(self, stack_id, stack_name, parameters, tags, region_name, template
             "AWS::StackName": stack_name,
             "AWS::URLSuffix": "amazonaws.com",
             "AWS::NoValue": None,
+            "AWS::Partition": "aws",
         }
 
     def __getitem__(self, key):


Test patch:
Repository: getmoto/moto

Issue:
Cryptography Package has a Security Vulnerability
Discovered using pipenv's security check feature that there's a vulnerability in the cryptography package versions<2.3.
    
> Checking installed package safety...
36351: cryptography >=1.9.0,<2.3 resolved (2.2.2 installed)!
python-cryptography versions >=1.9.0 and <2.3 did not enforce a minimum tag length for finalize_with_tag API. If a user did not validate the input length prior to passing it to finalize_with_tag an attacker could craft an invalid payload with a shortened tag (e.g. 1 byte) such that they would have a 1 in 256 chance of passing the MAC check. GCM tag forgeries can cause key leakage.
    
More details here: http://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2018-10903

setup.py should be updated to require cryptography>=2.3.0.



Discussion:

Resolving patch:
diff --git a/setup.py b/setup.py
--- a/setup.py
+++ b/setup.py
@@ -10,7 +10,7 @@
     "boto>=2.36.0",
     "boto3>=1.6.16,<1.8",
     "botocore>=1.9.16,<1.11",
-    "cryptography>=2.0.0",
+    "cryptography>=2.3.0",
     "requests>=2.5",
     "xmltodict",
     "six>1.9",


Test patch:
Repository: getmoto/moto

Issue:
EC2 describe-images filter is case sensitive and doesn't match AWS API
When dealing with filtering and booleans AWS expects the values to be lower-case (ie. "false" and "true"), but Moto expects the values to be upper-case (ie. "False" and "True").

https://docs.aws.amazon.com/cli/latest/reference/ec2/describe-images.html

> Public -> (boolean)
> 
> Indicates whether the image has public launch permissions. The value is true if this image has public launch permissions or false if it has only implicit and explicit launch permissions.

I've run into this issue specifically with ec2.describe-images and attempting to filter for private images with "is-public". As a temporary workaround AWS does allow you to submit multiple values for the filter and ignores the upper-case value for now so this will work with both AWS and Moto:

```
ec2_images = ec2_client.describe_images(
    Filters=[
        {
            'Name': 'is-public',
            'Values': ['False', 'false']
        },
    ],
    Owners['self']
)
```


Discussion:
Looks like the Ami model in Moto already has a method to return is-public as lower-case but it's not being used for filtering:

https://github.com/spulec/moto/blob/master/moto/ec2/models.py#L1105

```
    @property
    def is_public(self):
        return 'all' in self.launch_permission_groups

    @property
    def is_public_string(self):
        return str(self.is_public).lower()
```

Here's the original commit for reference:
https://github.com/spulec/moto/commit/8954242086de733e496331e56ff7fc9c456398f7

Resolving patch:
diff --git a/moto/ec2/models.py b/moto/ec2/models.py
--- a/moto/ec2/models.py
+++ b/moto/ec2/models.py
@@ -1115,7 +1115,7 @@ def get_filter_value(self, filter_name):
         elif filter_name == 'image-id':
             return self.id
         elif filter_name == 'is-public':
-            return str(self.is_public)
+            return self.is_public_string
         elif filter_name == 'state':
             return self.state
         elif filter_name == 'name':


Test patch:
diff --git a/tests/test_ec2/test_amis.py b/tests/test_ec2/test_amis.py
--- a/tests/test_ec2/test_amis.py
+++ b/tests/test_ec2/test_amis.py
@@ -258,11 +258,11 @@ def test_ami_filters():
     amis_by_name = conn.get_all_images(filters={'name': imageA.name})
     set([ami.id for ami in amis_by_name]).should.equal(set([imageA.id]))
 
-    amis_by_public = conn.get_all_images(filters={'is-public': True})
+    amis_by_public = conn.get_all_images(filters={'is-public': 'true'})
     set([ami.id for ami in amis_by_public]).should.contain(imageB.id)
     len(amis_by_public).should.equal(35)
 
-    amis_by_nonpublic = conn.get_all_images(filters={'is-public': False})
+    amis_by_nonpublic = conn.get_all_images(filters={'is-public': 'false'})
     set([ami.id for ami in amis_by_nonpublic]).should.contain(imageA.id)
     len(amis_by_nonpublic).should.equal(1)
Repository: getmoto/moto

Issue:
Replace pyaml dependency with PyYAML
There is a dependency on pyaml in setup.py:

https://github.com/spulec/moto/blob/master/setup.py#L18

I think that this is intended to be PyYAML (which pyaml depends on), and I do not see any usages of pyaml itself in this codebase.

pyaml uses WTFPL (https://github.com/mk-fg/pretty-yaml/blob/master/COPYING) which is not approved by the OSI (https://opensource.org/minutes20090304)


Discussion:

Resolving patch:
diff --git a/setup.py b/setup.py
--- a/setup.py
+++ b/setup.py
@@ -15,7 +15,7 @@
     "xmltodict",
     "six>1.9",
     "werkzeug",
-    "pyaml",
+    "PyYAML",
     "pytz",
     "python-dateutil<3.0.0,>=2.1",
     "python-jose<3.0.0",


Test patch:
Repository: getmoto/moto

Issue:
Add support for IAM update_user
The IAM update_user call isn't currently implemented. I've created a fork with an implementation (and test), so creating a quick issue here to tie the PR to.


Discussion:

Resolving patch:
diff --git a/moto/iam/models.py b/moto/iam/models.py
--- a/moto/iam/models.py
+++ b/moto/iam/models.py
@@ -892,6 +892,18 @@ def list_users(self, path_prefix, marker, max_items):
 
         return users
 
+    def update_user(self, user_name, new_path=None, new_user_name=None):
+        try:
+            user = self.users[user_name]
+        except KeyError:
+            raise IAMNotFoundException("User {0} not found".format(user_name))
+
+        if new_path:
+            user.path = new_path
+        if new_user_name:
+            user.name = new_user_name
+            self.users[new_user_name] = self.users.pop(user_name)
+
     def list_roles(self, path_prefix, marker, max_items):
         roles = None
         try:
diff --git a/moto/iam/responses.py b/moto/iam/responses.py
--- a/moto/iam/responses.py
+++ b/moto/iam/responses.py
@@ -440,6 +440,18 @@ def list_users(self):
         template = self.response_template(LIST_USERS_TEMPLATE)
         return template.render(action='List', users=users)
 
+    def update_user(self):
+        user_name = self._get_param('UserName')
+        new_path = self._get_param('NewPath')
+        new_user_name = self._get_param('NewUserName')
+        iam_backend.update_user(user_name, new_path, new_user_name)
+        if new_user_name:
+            user = iam_backend.get_user(new_user_name)
+        else:
+            user = iam_backend.get_user(user_name)
+        template = self.response_template(USER_TEMPLATE)
+        return template.render(action='Update', user=user)
+
     def create_login_profile(self):
         user_name = self._get_param('UserName')
         password = self._get_param('Password')


Test patch:
diff --git a/tests/test_iam/test_iam.py b/tests/test_iam/test_iam.py
--- a/tests/test_iam/test_iam.py
+++ b/tests/test_iam/test_iam.py
@@ -401,6 +401,19 @@ def test_get_user():
     conn.get_user('my-user')
 
 
+@mock_iam()
+def test_update_user():
+    conn = boto3.client('iam', region_name='us-east-1')
+    with assert_raises(conn.exceptions.NoSuchEntityException):
+        conn.update_user(UserName='my-user')
+    conn.create_user(UserName='my-user')
+    conn.update_user(UserName='my-user', NewPath='/new-path/', NewUserName='new-user')
+    response = conn.get_user(UserName='new-user')
+    response['User'].get('Path').should.equal('/new-path/')
+    with assert_raises(conn.exceptions.NoSuchEntityException):
+        conn.get_user(UserName='my-user')
+
+
 @mock_iam_deprecated()
 def test_get_current_user():
     """If no user is specific, IAM returns the current user"""
Repository: getmoto/moto

Issue:
Lambda publish_version returns wrong status code
In boto3,when lambda  publish_version is success,boto3 returns Http status code 201.
But, moto returns Http status code 200


moto and boto version
```
boto3               1.9.71
botocore          1.12.71
moto                 1.3.7
```



Discussion:

Resolving patch:
diff --git a/moto/awslambda/responses.py b/moto/awslambda/responses.py
--- a/moto/awslambda/responses.py
+++ b/moto/awslambda/responses.py
@@ -183,7 +183,7 @@ def _publish_function(self, request, full_url, headers):
         fn = self.lambda_backend.publish_function(function_name)
         if fn:
             config = fn.get_configuration()
-            return 200, {}, json.dumps(config)
+            return 201, {}, json.dumps(config)
         else:
             return 404, {}, "{}"
 


Test patch:
diff --git a/tests/test_awslambda/test_lambda.py b/tests/test_awslambda/test_lambda.py
--- a/tests/test_awslambda/test_lambda.py
+++ b/tests/test_awslambda/test_lambda.py
@@ -471,7 +471,8 @@ def test_publish():
     function_list['Functions'].should.have.length_of(1)
     latest_arn = function_list['Functions'][0]['FunctionArn']
 
-    conn.publish_version(FunctionName='testFunction')
+    res = conn.publish_version(FunctionName='testFunction')
+    assert res['ResponseMetadata']['HTTPStatusCode'] == 201
 
     function_list = conn.list_functions()
     function_list['Functions'].should.have.length_of(2)
@@ -853,8 +854,8 @@ def test_list_versions_by_function():
         Publish=True,
     )
 
-    conn.publish_version(FunctionName='testFunction')
-
+    res = conn.publish_version(FunctionName='testFunction')
+    assert res['ResponseMetadata']['HTTPStatusCode'] == 201
     versions = conn.list_versions_by_function(FunctionName='testFunction')
 
     assert versions['Versions'][0]['FunctionArn'] == 'arn:aws:lambda:us-west-2:123456789012:function:testFunction:$LATEST'