Repository: rotki/rotki

Issue:
Asset update API returns 500 when called outside of a session
## Problem Definition

We originally intended for the asset update endpoints to not be tied to sessions. This changed due to limitations but the API still allows for calls without a session that result in a 500 error.

## Logs

```python
[16/07/2021 12:16:12 CEST] ERROR rotkehlchen.api.server: Exception on /api/1/assets/updates [POST]
Traceback (most recent call last):
  File "/home/kelsos/.virtualenvs/rotkehlchen/lib/python3.7/site-packages/flask/app.py", line 1499, in full_dispatch_request
    rv = self.dispatch_request()
  File "/home/kelsos/.virtualenvs/rotkehlchen/lib/python3.7/site-packages/flask/app.py", line 1485, in dispatch_request
    return self.ensure_sync(self.view_functions[rule.endpoint])(**req.view_args)
  File "/home/kelsos/.virtualenvs/rotkehlchen/lib/python3.7/site-packages/flask_restful/__init__.py", line 467, in wrapper
    resp = resource(*args, **kwargs)
  File "/home/kelsos/.virtualenvs/rotkehlchen/lib/python3.7/site-packages/flask/views.py", line 83, in view
    return self.dispatch_request(*args, **kwargs)
  File "/home/kelsos/.virtualenvs/rotkehlchen/lib/python3.7/site-packages/flask_restful/__init__.py", line 582, in dispatch_request
    resp = meth(*args, **kwargs)
  File "/home/kelsos/.virtualenvs/rotkehlchen/lib/python3.7/site-packages/webargs/core.py", line 450, in wrapper
    return func(*args, **kwargs)
  File "/home/kelsos/development/github/rotkehlchenio/rotkehlchen/rotkehlchen/api/v1/resources.py", line 491, in post
    return self.rest_api.perform_assets_updates(async_query, up_to_version, conflicts)
  File "/home/kelsos/development/github/rotkehlchenio/rotkehlchen/rotkehlchen/api/rest.py", line 3075, in perform_assets_updates
    response = self._perform_assets_updates(up_to_version, conflicts)
  File "/home/kelsos/development/github/rotkehlchenio/rotkehlchen/rotkehlchen/api/rest.py", line 3050, in _perform_assets_updates
    cursor = self.rotkehlchen.data.db.conn.cursor()
AttributeError: 'DataHandler' object has no attribute 'db'
```


Discussion:

Resolving patch:
diff --git a/rotkehlchen/api/rest.py b/rotkehlchen/api/rest.py
--- a/rotkehlchen/api/rest.py
+++ b/rotkehlchen/api/rest.py
@@ -3059,6 +3059,7 @@ def _perform_assets_updates(
             'status_code': HTTPStatus.CONFLICT,
         }
 
+    @require_loggedin_user()
     def perform_assets_updates(
             self,
             async_query: bool,


Test patch:
diff --git a/rotkehlchen/tests/api/test_assets_updates.py b/rotkehlchen/tests/api/test_assets_updates.py
--- a/rotkehlchen/tests/api/test_assets_updates.py
+++ b/rotkehlchen/tests/api/test_assets_updates.py
@@ -16,6 +16,7 @@
 from rotkehlchen.globaldb.updates import ASSETS_VERSION_KEY
 from rotkehlchen.tests.utils.api import (
     api_url_for,
+    assert_error_response,
     assert_ok_async_response,
     assert_proper_response_with_result,
     wait_for_async_task,
@@ -764,3 +765,20 @@ def test_update_from_early_clean_db(rotkehlchen_api_server, globaldb):
         assert len(errors) == 0, f'Found errors: {errors}'
         assert len(warnings) == 1
         assert f'Failed to resolve conflict for {gnt.identifier} in the DB during the v1 assets update. Skipping entry' in warnings[0]  # noqa: E501
+
+
+@pytest.mark.parametrize('use_clean_caching_directory', [True])
+@pytest.mark.parametrize('start_with_logged_in_user', [False])
+@pytest.mark.parametrize('number_of_eth_accounts', [0])
+def test_update_no_user_loggedin(rotkehlchen_api_server):
+    response = requests.post(
+        api_url_for(
+            rotkehlchen_api_server,
+            'assetupdatesresource',
+        ),
+    )
+    assert_error_response(
+        response=response,
+        contained_in_msg='No user is currently logged in',
+        status_code=HTTPStatus.CONFLICT,
+    )