internlm/SWE-Fixer-Train-110K huggingface dataset:
Repository: Kinto/kinto

Issue:
Crash in PostgreSQL when filtering with empty value (v6.0.5)
With querystring `?last_modified=`

Error 
```
(psycopg2.DataError) invalid input syntax for type numeric: ""
```
From this `get_all()` query
```
AND (data->>%(filters_field_0_0)s)::numeric = %(filters_value_0)s
```
that becomes:
```
...
AND (data->>'1492639357')::numeric = ''
...
```


Discussion:
Problem comes from here: https://github.com/Kinto/kinto/blob/6.0.5/kinto/core/storage/postgresql/__init__.py#L703

Where `all([is_numeric(v) for v in value])` is True for empty strings/lists

Resolving patch:
diff --git a/kinto/core/storage/postgresql/__init__.py b/kinto/core/storage/postgresql/__init__.py
--- a/kinto/core/storage/postgresql/__init__.py
+++ b/kinto/core/storage/postgresql/__init__.py
@@ -703,7 +703,7 @@ def _format_conditions(self, filters, id_field, modified_field,
                 sql_field = "coalesce({}, '')".format(column_name)
                 # Cast when comparing to number (eg. '4' < '12')
                 try:
-                    if is_numeric(value) or all([is_numeric(v) for v in value]):
+                    if value and (is_numeric(value) or all([is_numeric(v) for v in value])):
                         sql_field = "({})::numeric".format(column_name)
                 except TypeError:  # not iterable
                     pass


Test patch:
diff --git a/kinto/core/storage/testing.py b/kinto/core/storage/testing.py
--- a/kinto/core/storage/testing.py
+++ b/kinto/core/storage/testing.py
@@ -401,6 +401,14 @@ def test_get_all_can_filter_with_numeric_strings(self):
                                           **self.storage_kw)
         self.assertEqual(len(records), 1)
 
+    def test_get_all_can_filter_with_empty_numeric_strings(self):
+        for l in ["0566199093", "0781566199"]:
+            self.create_record({'phone': l})
+        filters = [Filter('phone', "", utils.COMPARISON.EQ)]
+        records, _ = self.storage.get_all(filters=filters,
+                                          **self.storage_kw)
+        self.assertEqual(len(records), 0)
+
     def test_get_all_can_filter_with_float_values(self):
         for l in [10, 11.5, 8.5, 6, 7.5]:
             self.create_record({'note': l})
Repository: Kinto/kinto

Issue:
Cache delete method should return deleted value or None
Ref https://github.com/Kinto/kinto/pull/1218#discussion_r116852457
Cache delete method should return deleted value or None
Ref https://github.com/Kinto/kinto/pull/1218#discussion_r116852457


Discussion:

Resolving patch:
diff --git a/kinto/core/cache/memory.py b/kinto/core/cache/memory.py
--- a/kinto/core/cache/memory.py
+++ b/kinto/core/cache/memory.py
@@ -82,6 +82,7 @@ def delete(self, key):
         self._created_at.pop(key, None)
         value = self._store.pop(key, None)
         self._quota -= size_of(key, value)
+        return value
 
 
 def load_from_config(config):
diff --git a/kinto/core/cache/postgresql/__init__.py b/kinto/core/cache/postgresql/__init__.py
--- a/kinto/core/cache/postgresql/__init__.py
+++ b/kinto/core/cache/postgresql/__init__.py
@@ -150,9 +150,13 @@ def get(self, key):
                 return json.loads(value)
 
     def delete(self, key):
-        query = "DELETE FROM cache WHERE key = :key"
+        query = "DELETE FROM cache WHERE key = :key RETURNING value;"
         with self.client.connect() as conn:
-            conn.execute(query, dict(key=self.prefix + key))
+            result = conn.execute(query, dict(key=self.prefix + key))
+            if result.rowcount > 0:
+                value = result.fetchone()['value']
+                return json.loads(value)
+        return None
 
 
 def load_from_config(config):


Test patch:
diff --git a/kinto/core/cache/testing.py b/kinto/core/cache/testing.py
--- a/kinto/core/cache/testing.py
+++ b/kinto/core/cache/testing.py
@@ -107,12 +107,14 @@ def test_bytes_cannot_be_stored_in_the_cache(self):
 
     def test_delete_removes_the_record(self):
         self.cache.set('foobar', 'toto', 42)
-        self.cache.delete('foobar')
-        retrieved = self.cache.get('foobar')
-        self.assertIsNone(retrieved)
+        returned = self.cache.delete('foobar')
+        self.assertEqual(returned, 'toto')
+        missing = self.cache.get('foobar')
+        self.assertIsNone(missing)
 
     def test_delete_does_not_fail_if_record_is_unknown(self):
-        self.cache.delete('foobar')
+        returned = self.cache.delete('foobar')
+        self.assertIsNone(returned)
 
     def test_expire_expires_the_value(self):
         self.cache.set('foobar', 'toto', 42)
Repository: Kinto/kinto

Issue:
Remove userid from statsd metrics
```
Traceback (most recent
call last):#012  File "/opt/datadog-agent/agent/dogstatsd.py", line 419, in
start#012    aggregator_submit(message)#012  File
"/opt/datadog-agent/agent/aggregator.py", line 613, in submit_packets#012   
parsed_packets = self.parse_metric_packet(packet)#012  File
"/opt/datadog-agent/agent/aggregator.py", line 467, in parse_metric_packet#012 
  raise Exception(u'Unparseable metric packet: %s' % packet)#012Exception:
Unparseable metric packet: kinto.users:<auth>:<id>|s
```
Remove userid from statsd metrics
```
Traceback (most recent
call last):#012  File "/opt/datadog-agent/agent/dogstatsd.py", line 419, in
start#012    aggregator_submit(message)#012  File
"/opt/datadog-agent/agent/aggregator.py", line 613, in submit_packets#012   
parsed_packets = self.parse_metric_packet(packet)#012  File
"/opt/datadog-agent/agent/aggregator.py", line 467, in parse_metric_packet#012 
  raise Exception(u'Unparseable metric packet: %s' % packet)#012Exception:
Unparseable metric packet: kinto.users:<auth>:<id>|s
```


Discussion:
Caused by colon in unique key counter, here https://github.com/DataDog/dd-agent/blob/5.14.1/aggregator.py#L446
Caused by colon in unique key counter, here https://github.com/DataDog/dd-agent/blob/5.14.1/aggregator.py#L446

Resolving patch:
diff --git a/kinto/core/initialization.py b/kinto/core/initialization.py
--- a/kinto/core/initialization.py
+++ b/kinto/core/initialization.py
@@ -278,6 +278,8 @@ def on_new_response(event):
             # Count unique users.
             user_id = request.prefixed_userid
             if user_id:
+                # Get rid of colons in metric packet (see #1282).
+                user_id = user_id.replace(':', '.')
                 client.count('users', unique=user_id)
 
             # Count authentication verifications.


Test patch:
diff --git a/tests/core/test_initialization.py b/tests/core/test_initialization.py
--- a/tests/core/test_initialization.py
+++ b/tests/core/test_initialization.py
@@ -328,7 +328,7 @@ def test_statsd_counts_unique_users(self, digest_mocked):
         app = webtest.TestApp(self.config.make_wsgi_app())
         headers = {'Authorization': 'Basic bWF0Og=='}
         app.get('/v0/', headers=headers)
-        self.mocked().count.assert_any_call('users', unique='basicauth:mat')
+        self.mocked().count.assert_any_call('users', unique='basicauth.mat')
 
     def test_statsd_counts_authentication_types(self):
         kinto.core.initialize(self.config, '0.0.1', 'project_name')
Repository: Kinto/kinto

Issue:
`kinto create-user` doesn't override the password if the user already exists.



Discussion:
I did some more tests, and I suspect that "create-user" doesn't create the user at all. For example, I run,

```
$ sudo docker run --rm --net host \
    --env-file /etc/dockerflow/buildhub.txt \
    -v /etc/kinto.ini:/etc/kinto.ini \
    --log-driver=syslog hub.prod.mozaws.net/pipelines/buildhub:4.2.0 \
kinto create-user -u user1 -p password --ini /etc/kinto.ini

/usr/local/lib/python3.5/site-packages/kinto/core/storage/postgresql/client.py:91: UserWarning: Reuse existing PostgreSQL connection. Parameters permission_* will be ignored.
  warnings.warn(msg)
INFO   Running kinto 7.4.1.
Creating user 'user1'
```

And afterwards I ran ```curl -s -u user1:password https://buildhub.stage.mozaws.net/v1/```, which doesn't return the user property.

```
$ curl -s -u user1:password https://buildhub.stage.mozaws.net/v1/ | jq .
{
  "http_api_version": "1.17",
  "project_name": "kinto",
  "project_version": "7.4.1",
  "settings": {
    "batch_max_requests": 25,
    "readonly": false
  },
  "project_docs": "https://kinto.readthedocs.io/",
  "url": "https://buildhub.stage.mozaws.net/v1/",
  "capabilities": {
    "elasticsearch": {
      "description": "Index and search records using ElasticSearch.",
      "version": "0.3.0",
      "url": "https://github.com/Kinto/kinto-elasticsearch"
    },
    "accounts": {
      "description": "Manage user accounts.",
      "url": "https://kinto.readthedocs.io/en/latest/api/1.x/accounts.html"
    }
  }
}
```

Further more the user_principals table doesn't contain the ```user1``` user either.

```
buildhub=> select * from user_principals where user_id like '%user1%';
 user_id | principal
---------+-----------
(0 rows)
```
One possible flaw is that the command does not run `current_transaction.commit()` (see https://github.com/Kinto/kinto/blob/master/kinto/core/scripts.py#L89) and maybe it was only tested on the memory backend ?
> Maybe it was only tested on the memory backend?

It was also tested with the redis backend but you could really well be right about the postgresql backend error.

Resolving patch:
diff --git a/kinto/plugins/accounts/scripts.py b/kinto/plugins/accounts/scripts.py
--- a/kinto/plugins/accounts/scripts.py
+++ b/kinto/plugins/accounts/scripts.py
@@ -1,5 +1,7 @@
 import logging
 import getpass
+
+import transaction as current_transaction
 from pyramid.settings import asbool
 
 from .utils import hash_password
@@ -56,4 +58,6 @@ def create_user(env, username=None, password=None):
                                              'write',
                                              'account:{}'.format(username))
 
+    current_transaction.commit()
+
     return 0


Test patch:
Repository: Kinto/kinto

Issue:
`kinto create-user` command should fallback to KINTO_INI env variable for the config file;



Discussion:
Sounds good.

When this gets implemented, do we must put the user password in the env file?
I guess we can already do: `kinto create-user --username "$USERNAME" --password "$PASSWORD" --ini $KINTO_INI`

What variable name would you like to default to for username and password?

Resolving patch:
diff --git a/kinto/__main__.py b/kinto/__main__.py
--- a/kinto/__main__.py
+++ b/kinto/__main__.py
@@ -11,7 +11,7 @@
 from kinto import __version__
 from kinto.config import init
 
-DEFAULT_CONFIG_FILE = 'config/kinto.ini'
+DEFAULT_CONFIG_FILE = os.getenv('KINTO_INI', 'config/kinto.ini')
 DEFAULT_PORT = 8888
 DEFAULT_LOG_LEVEL = logging.INFO
 DEFAULT_LOG_FORMAT = '%(levelname)-5.5s  %(message)s'


Test patch:
Repository: Kinto/kinto

Issue:
Add Memcached to dockerfile and docker compose setup.
Following the addition of Memcached as a cache backend.


Discussion:
Hi Natim, 

I'm new to kinto project and require clarification on this issue.

Do you mean to add some pip install command to [dockerfile](https://github.com/Kinto/kinto/blob/master/Dockerfile) and update docker compose[ yml file](https://github.com/Kinto/kinto/blob/master/docker-compose.yml)? Are there any other relevant files work on?
Thanks for your interest!

Actually, adding `memcache` as an extra dependency here: https://github.com/Kinto/kinto/blob/master/Dockerfile#L21

And add the necessary changes in the yml file to add a link to a memcache container and add the configuration values so that it would be the configured cache backend
Hello, I am intermediate user of python, and would like to contribute myself on this project. Can I work on it? This is my first project. Thanks
Hi @mayurvadhavana !
Thanks for your interest in the project!
I don't know if @emamurho is still working on this :) 
Please note also that this precise issue is more about Docker than Python. 
You could look into #1334 otherwise :)
Enjoy!
Hi @leplatrem thanks for asking am still on it :) sorry @mayurvadhavana  :)


Resolving patch:
diff --git a/kinto/core/__init__.py b/kinto/core/__init__.py
--- a/kinto/core/__init__.py
+++ b/kinto/core/__init__.py
@@ -28,6 +28,7 @@
     'backoff': None,
     'batch_max_requests': 25,
     'cache_backend': '',
+    'cache_hosts': '',
     'cache_url': '',
     'cache_pool_size': 25,
     'cache_prefix': '',
diff --git a/kinto/core/storage/postgresql/client.py b/kinto/core/storage/postgresql/client.py
--- a/kinto/core/storage/postgresql/client.py
+++ b/kinto/core/storage/postgresql/client.py
@@ -81,6 +81,7 @@ def create_from_config(config, prefix='', with_transaction=True):
     settings.pop(prefix + 'max_size_bytes', None)
     settings.pop(prefix + 'prefix', None)
     settings.pop(prefix + 'strict_json', None)
+    settings.pop(prefix + 'hosts', None)
     transaction_per_request = with_transaction and settings.pop('transaction_per_request', False)
 
     url = settings[prefix + 'url']


Test patch:
Repository: Kinto/kinto

Issue:
Do not enforce instantiation of resource model during init

Do not enforce instantiation of resource model during init



Discussion:

Resolving patch:
diff --git a/kinto/core/resource/__init__.py b/kinto/core/resource/__init__.py
--- a/kinto/core/resource/__init__.py
+++ b/kinto/core/resource/__init__.py
@@ -167,12 +167,13 @@ def __init__(self, request, context=None):
         # Authentication to storage is transmitted as is (cf. cloud_storage).
         auth = request.headers.get('Authorization')
 
-        self.model = self.default_model(
-            storage=request.registry.storage,
-            id_generator=self.id_generator,
-            collection_id=classname(self),
-            parent_id=parent_id,
-            auth=auth)
+        if not hasattr(self, 'model'):
+            self.model = self.default_model(
+                storage=request.registry.storage,
+                id_generator=self.id_generator,
+                collection_id=classname(self),
+                parent_id=parent_id,
+                auth=auth)
 
         # Initialize timestamp as soon as possible.
         self.timestamp


Test patch:
diff --git a/tests/core/resource/test_base.py b/tests/core/resource/test_base.py
--- a/tests/core/resource/test_base.py
+++ b/tests/core/resource/test_base.py
@@ -1,4 +1,5 @@
 import mock
+import unittest
 from pyramid import httpexceptions
 
 from kinto.core.resource import UserResource, ShareableResource
@@ -66,3 +67,16 @@ def test_get_parent_can_be_overridded(self):
         parent_id = self.resource.get_parent_id(request)
         self.assertEquals(parent_id, 'overrided')
         self.assertEquals(self.resource.model.parent_id, 'overrided')
+
+
+class CustomModelResource(UserResource):
+    def __init__(self, *args, **kwargs):
+        self.model = mock.MagicMock()
+        self.model.name = mock.sentinel.model
+        super().__init__(*args, **kwargs)
+
+
+class CustomModelResourceTets(unittest.TestCase):
+    def test_custom_model_is_not_overriden(self):
+        c = CustomModelResource(request=mock.MagicMock())
+        self.assertEquals(c.model.name, mock.sentinel.model)
Repository: Kinto/kinto

Issue:
Enabling default bucket plugins removes user principals from hello endpoint

Enabling default bucket plugins removes user principals from hello endpoint



Discussion:

Resolving patch:
diff --git a/kinto/core/__init__.py b/kinto/core/__init__.py
--- a/kinto/core/__init__.py
+++ b/kinto/core/__init__.py
@@ -133,6 +133,14 @@ def __init__(self, fmt=None, datefmt=None, style='%'):
         self.logger_name = logger_name
 
 
+def get_user_info(request):
+    # Default user info (shown in hello view for example).
+    return {
+        'id': request.prefixed_userid,
+        'principals': request.prefixed_principals
+    }
+
+
 def includeme(config):
     settings = config.get_settings()
 
@@ -186,10 +194,7 @@ def add_api_capability(config, identifier, description='', url='', **kw):
     config.add_request_method(follow_subrequest)
     config.add_request_method(prefixed_userid, property=True)
     config.add_request_method(prefixed_principals, reify=True)
-    config.add_request_method(lambda r: {
-        'id': r.prefixed_userid,
-        'principals': r.prefixed_principals},
-        name='get_user_info')
+    config.add_request_method(get_user_info, name='get_user_info')
     config.add_request_method(current_resource_name, reify=True)
     config.add_request_method(current_service, reify=True)
     config.commit()
diff --git a/kinto/plugins/default_bucket/__init__.py b/kinto/plugins/default_bucket/__init__.py
--- a/kinto/plugins/default_bucket/__init__.py
+++ b/kinto/plugins/default_bucket/__init__.py
@@ -4,6 +4,7 @@
 from pyramid.settings import asbool
 from pyramid.security import NO_PERMISSION_REQUIRED, Authenticated
 
+from kinto.core import get_user_info as core_get_user_info
 from kinto.core.errors import raise_invalid
 from kinto.core.events import ACTIONS
 from kinto.core.storage.exceptions import UnicityError
@@ -183,8 +184,8 @@ def default_bucket_id(request):
 
 def get_user_info(request):
     user_info = {
-        'id': request.prefixed_userid,
-        'bucket': request.default_bucket_id
+        **core_get_user_info(request),
+        'bucket': request.default_bucket_id,
     }
     return user_info
 


Test patch:
diff --git a/tests/plugins/test_default_bucket.py b/tests/plugins/test_default_bucket.py
--- a/tests/plugins/test_default_bucket.py
+++ b/tests/plugins/test_default_bucket.py
@@ -259,8 +259,11 @@ class HelloViewTest(DefaultBucketWebTest):
 
     def test_returns_bucket_id_and_url_if_authenticated(self):
         response = self.app.get('/', headers=self.headers)
-        self.assertEqual(response.json['user']['bucket'],
-                         'ddaf8694-fa9e-2949-ed0e-77198a7907fb')
+        userinfo = response.json['user']
+        self.assertIn('id', userinfo)
+        self.assertIn('principals', userinfo)
+        self.assertIn('bucket', userinfo)
+        self.assertEqual(userinfo['bucket'], 'ddaf8694-fa9e-2949-ed0e-77198a7907fb')
 
     def test_flush_capability_if_enabled(self):
         resp = self.app.get('/')
Repository: Kinto/kinto

Issue:
OpenID state length is too long for the PostgreSQL cache backend
Those two lines are not compatible together:

- https://github.com/Kinto/kinto/blob/c6cc7bba094aed6897d0157dc78b1731ac12c8db/kinto/core/cache/postgresql/schema.sql#L7
- https://github.com/Kinto/kinto/blob/c6cc7bba094aed6897d0157dc78b1731ac12c8db/kinto/plugins/openid/views.py#L97
OpenID state length is too long for the PostgreSQL cache backend
Those two lines are not compatible together:

- https://github.com/Kinto/kinto/blob/c6cc7bba094aed6897d0157dc78b1731ac12c8db/kinto/core/cache/postgresql/schema.sql#L7
- https://github.com/Kinto/kinto/blob/c6cc7bba094aed6897d0157dc78b1731ac12c8db/kinto/plugins/openid/views.py#L97


Discussion:

Resolving patch:
diff --git a/kinto/plugins/openid/views.py b/kinto/plugins/openid/views.py
--- a/kinto/plugins/openid/views.py
+++ b/kinto/plugins/openid/views.py
@@ -15,6 +15,7 @@
 
 
 DEFAULT_STATE_TTL_SECONDS = 3600
+DEFAULT_STATE_LENGTH = 32
 
 
 class RedirectHeadersSchema(colander.MappingSchema):
@@ -76,6 +77,8 @@ def get_login(request):
     userid_field = request.registry.settings.get(settings_prefix + 'userid_field')
     state_ttl = int(request.registry.settings.get(settings_prefix + 'state_ttl_seconds',
                                                   DEFAULT_STATE_TTL_SECONDS))
+    state_length = int(request.registry.settings.get(settings_prefix + 'state_length',
+                                                     DEFAULT_STATE_LENGTH))
 
     # Read OpenID configuration (cached by issuer)
     oid_config = fetch_openid_config(issuer)
@@ -94,7 +97,7 @@ def get_login(request):
 
     # Generate a random string as state.
     # And save it until code is traded.
-    state = random_bytes_hex(256)
+    state = random_bytes_hex(state_length)
     request.registry.cache.set('openid:state:' + state, callback, ttl=state_ttl)
 
     # Redirect the client to the Identity Provider that will eventually redirect


Test patch:
Repository: Kinto/kinto

Issue:
OpenID payload cache uses the wrong cache key
The `cache_key` becomes the same for every access token due to this bug: https://github.com/Kinto/kinto/blob/e1e0d6be0024418fd100210901f9d2ca06344fe1/kinto/plugins/openid/__init__.py#L51
No matter what the `hmac_tokens` variable is the `cache_key` always becomes `'openid:verify:%s'`.


OpenID payload cache uses the wrong cache key
The `cache_key` becomes the same for every access token due to this bug: https://github.com/Kinto/kinto/blob/e1e0d6be0024418fd100210901f9d2ca06344fe1/kinto/plugins/openid/__init__.py#L51
No matter what the `hmac_tokens` variable is the `cache_key` always becomes `'openid:verify:%s'`.




Discussion:

Resolving patch:
diff --git a/kinto/plugins/openid/__init__.py b/kinto/plugins/openid/__init__.py
--- a/kinto/plugins/openid/__init__.py
+++ b/kinto/plugins/openid/__init__.py
@@ -48,7 +48,7 @@ def unauthenticated_userid(self, request):
 
         # Check cache if these tokens were already verified.
         hmac_tokens = core_utils.hmac_digest(hmac_secret, access_token)
-        cache_key = 'openid:verify:%s'.format(hmac_tokens)
+        cache_key = 'openid:verify:{}'.format(hmac_tokens)
         payload = request.registry.cache.get(cache_key)
         if payload is None:
             # This can take some time.


Test patch:
diff --git a/tests/plugins/test_openid.py b/tests/plugins/test_openid.py
--- a/tests/plugins/test_openid.py
+++ b/tests/plugins/test_openid.py
@@ -111,13 +111,35 @@ def test_returns_none_if_no_cache_and_invalid_access_token(self):
     def test_payload_is_read_from_cache(self):
         self.request.headers['Authorization'] = 'Bearer xyz'
         self.request.registry.cache.get.return_value = {'sub': 'me'}
-        self.policy.unauthenticated_userid(self.request) == 'me'
+        assert self.policy.unauthenticated_userid(self.request) == 'me'
 
     def test_payload_is_stored_in_cache(self):
         self.request.headers['Authorization'] = 'Bearer xyz'
         assert self.policy.unauthenticated_userid(self.request) == 'userid'
         assert self.request.registry.cache.set.called
 
+    def test_payload_is_read_from_cache_but_differently_by_access_token(self):
+        # State to keep track of cache keys queried.
+        cache_keys_used = []
+
+        def mocked_cache_get(cache_key):
+            # This makes sure the same cache key is not used twice
+            assert cache_key not in cache_keys_used
+            cache_keys_used.append(cache_key)
+            if len(cache_keys_used) == 1:
+                return {'sub': 'me'}
+            elif len(cache_keys_used) == 2:
+                return {'sub': 'you'}
+
+        self.request.registry.cache.get.side_effect = mocked_cache_get
+
+        self.request.headers['Authorization'] = 'Bearer xyz'
+        assert self.policy.unauthenticated_userid(self.request) == 'me'
+
+        # Change the Authorization header the second time
+        self.request.headers['Authorization'] = 'Bearer abc'
+        assert self.policy.unauthenticated_userid(self.request) == 'you'
+
 
 class VerifyTokenTest(unittest.TestCase):