Repository: rotki/rotki

Issue:
Upgrade to Premium popup still displayed when created user with Premium api key 
Created docker image from commit https://github.com/rotki/rotki/commit/a7ef8be31f8059eeff34509667688a87a63edfc6 and created user for a first time importing api keys i was present with `Upgrade to Premium` popup, expected no to see this as far as Premium keys already were imported during user creation. 

<img width="797" alt="Screen Shot 2021-05-05 at 6 40 03 PM" src="https://user-images.githubusercontent.com/15807643/117230156-fb5d4880-ae0b-11eb-9bc1-0d1d11cb5c6c.png">
<img width="1888" alt="Screen Shot 2021-05-05 at 6 43 32 PM" src="https://user-images.githubusercontent.com/15807643/117230372-79215400-ae0c-11eb-8f24-dbe79a38bab2.png">

Imported api keys second time to activate Premium
<img width="1814" alt="Screen Shot 2021-05-05 at 6 44 45 PM" src="https://user-images.githubusercontent.com/15807643/117230452-a66e0200-ae0c-11eb-9c3a-d1ab8be0be9a.png">





Discussion:
Hey @CruiseMan thanks a lot.

Let me see if I understand. Where did you add the premium keys? During account creation? And that did not work?

But then you followed this and it worked? https://rotki.readthedocs.io/en/latest/usage_guide.html#set-up-rotki-premium
> Hey @CruiseMan thanks a lot.
> 
> Let me see if I understand. Where did you add the premium keys? During account creation? And that did not work?
> 
> But then you followed this and it worked? https://rotki.readthedocs.io/en/latest/usage_guide.html#set-up-rotki-premium

Correct. Importing apt key during user creation. I hade have to import after user was created and logged in
Just to confirm.

1. Creating the account with adding the premium keys at start seems to work for me in electron (did not test docker). For that first session.
2. But if I logout of that account and login again the keys are gone and need to be inserted again. 

I think that narrows it down as to what may be happening so I will check the backend code to figure out the culprit in our little problem.

Resolving patch:
diff --git a/rotkehlchen/premium/sync.py b/rotkehlchen/premium/sync.py
--- a/rotkehlchen/premium/sync.py
+++ b/rotkehlchen/premium/sync.py
@@ -284,25 +284,18 @@ def try_premium_at_start(
 
             if sync_approval == 'yes':
                 log.info('User approved data sync from server')
-                if self._sync_data_from_server_and_replace_local()[0]:
-                    if create_new:
-                        # if we successfully synced data from the server and this is
-                        # a new account, make sure the api keys are properly stored
-                        # in the DB
-                        self.data.db.set_rotkehlchen_premium(self.premium.credentials)
+                self._sync_data_from_server_and_replace_local()  # this may raise due to password
 
             else:
                 log.debug('Could sync data from server but user refused')
         elif result.can_sync == CanSync.YES:
             log.info('User approved data sync from server')
-            if self._sync_data_from_server_and_replace_local()[0]:
-                if create_new:
-                    # if we successfully synced data from the server and this is
-                    # a new account, make sure the api keys are properly stored
-                    # in the DB
-                    self.data.db.set_rotkehlchen_premium(self.premium.credentials)
-
-        # else result.can_sync was no, so we do nothing
+            self._sync_data_from_server_and_replace_local()  # this may raise due to password
+
+        if create_new:
+            # if this is a new account, make sure the api keys are properly stored
+            # in the DB
+            self.data.db.set_rotkehlchen_premium(self.premium.credentials)
 
         # Success, return premium
         return self.premium


Test patch:
diff --git a/rotkehlchen/tests/integration/test_premium.py b/rotkehlchen/tests/integration/test_premium.py
--- a/rotkehlchen/tests/integration/test_premium.py
+++ b/rotkehlchen/tests/integration/test_premium.py
@@ -391,7 +391,10 @@ def test_try_premium_at_start_first_time_no_previous_db(
         db_password,
         rotki_premium_credentials,
 ):
-    """Regression test for https://github.com/rotki/rotki/issues/1571"""
+    """Regression test for:
+    - https://github.com/rotki/rotki/issues/1571
+    - https://github.com/rotki/rotki/issues/2846
+    """
     setup_starting_environment(
         rotkehlchen_instance=rotkehlchen_instance,
         username=username,
@@ -405,6 +408,12 @@ def test_try_premium_at_start_first_time_no_previous_db(
     )
     # DB should not have changed and no exception raised
     assert rotkehlchen_instance.data.db.get_main_currency() == DEFAULT_TESTS_MAIN_CURRENCY
+    # DB should have the given rotki premium credentials saved in it since premium
+    # was succesfully initialized
+    credentials = rotkehlchen_instance.data.db.get_rotkehlchen_premium()
+    assert credentials is not None
+    assert credentials.api_key == rotki_premium_credentials.api_key
+    assert credentials.api_secret == rotki_premium_credentials.api_secret
 
 
 def test_premium_credentials():