From d90ed462e25dd17ea7bc8bbd1cd14b7b2eb70210 Mon Sep 17 00:00:00 2001
From: Aurélien Bompard <aurelien@bompard.org>
Date: Mar 21 2018 15:39:11 +0000
Subject: More flakiness fixes


---

diff --git a/tests/test_pagure_flask_internal.py b/tests/test_pagure_flask_internal.py
index 9de087d..b13b250 100644
--- a/tests/test_pagure_flask_internal.py
+++ b/tests/test_pagure_flask_internal.py
@@ -1767,8 +1767,8 @@ class PagureFlaskInternaltests(tests.Modeltests):
             [u'code', u'message']
         )
         self.assertEqual(js_data['code'], 'OK')
-        self.assertEqual(
-            js_data['message'].keys(),
+        self.assertListEqual(
+            sorted(js_data['message'].keys()),
             [u'branch_w_pr', u'new_branch'])
         self.assertEqual(js_data['message']['branch_w_pr'], {})
         self.assertEqual(js_data['message']['new_branch'].keys(), ['feature'])
@@ -1893,7 +1893,7 @@ class PagureFlaskInternaltests(tests.Modeltests):
         )
         self.assertEqual(js_data['code'], 'OK')
         self.assertEqual(
-            js_data['message'].keys(),
+            sorted(js_data['message'].keys()),
             [u'branch_w_pr', u'new_branch'])
         self.assertEqual(js_data['message']['branch_w_pr'], {})
         self.assertEqual(js_data['message']['new_branch'].keys(), ['feature'])
diff --git a/tests/test_pagure_flask_ui_fork.py b/tests/test_pagure_flask_ui_fork.py
index e588e8b..40665b3 100644
--- a/tests/test_pagure_flask_ui_fork.py
+++ b/tests/test_pagure_flask_ui_fork.py
@@ -592,8 +592,11 @@ class PagureFlaskForktests(tests.Modeltests):
         self.assertEqual(output.status_code, 404)
 
     @patch('pagure.lib.notify.send_email')
-    def test_request_pull_empty_repo(self, send_email):
+    @patch('pagure.lib.git.update_pull_ref')
+    def test_request_pull_empty_repo(self, send_email, update_pull_ref):
         """ Test the request_pull endpoint against an empty repo. """
+        # Mock update_pull_ref or the repo won't be empty anymore
+        # (the PR will have been pushed to refs/pull)
         send_email.return_value = True
 
         tests.create_projects(self.session)
@@ -672,6 +675,7 @@ class PagureFlaskForktests(tests.Modeltests):
             '  PR from the feature branch\n</h3>', output.data)
         self.assertTrue(
             output.data.count('<span class="commitdate" title='), 1)
+        update_pull_ref.assert_called()
 
         shutil.rmtree(newpath)
 
@@ -988,8 +992,11 @@ index 9f44358..2a552bb 100644
         self.assertEqual(patch, exp)
 
     @patch('pagure.lib.notify.send_email')
-    def test_request_pull_patch_empty_repo(self, send_email):
+    @patch('pagure.lib.git.update_pull_ref')
+    def test_request_pull_patch_empty_repo(self, send_email, update_pull_ref):
         """ Test the request_pull_patch endpoint against an empty repo. """
+        # Mock update_pull_ref or the repo won't be empty anymore
+        # (the PR will have been pushed to refs/pull)
         send_email.return_value = True
 
         tests.create_projects(self.session)
diff --git a/tests/test_pagure_flask_ui_login.py b/tests/test_pagure_flask_ui_login.py
index 1f60370..f43d01d 100644
--- a/tests/test_pagure_flask_ui_login.py
+++ b/tests/test_pagure_flask_ui_login.py
@@ -302,23 +302,11 @@ class PagureFlaskLogintests(tests.SimplePagureTest):
         self.assertEqual(item.user, 'foouser')
         self.assertTrue(item.password.startswith('$2$'))
 
-        # I'm not sure if the change was in flask or werkzeug, but in older
-        # version flask.request.remote_addr was returning None, while it
-        # now returns 127.0.0.1 making our logic pass where it used to
-        # partly fail
-        if hasattr(flask, '__version__'):
-            flask_v = tuple(int(el) for el in flask.__version__.split('.'))
-            if flask_v <= (0, 12, 0):
-                self.assertIn(
-                    '<a class="nav-link btn btn-primary" '
-                    'href="/login/?next=http://localhost/">', output.data)
-                self.assertIn(
-                    'Could not set the session in the db, please report '
-                    'this error to an admin', output.data)
-            else:
-                self.assertIn(
-                    '<a class="dropdown-item" '
-                    'href="/logout/?next=http://localhost/">', output.data)
+        # We have set the REMOTE_ADDR in the request, so this works with all
+        # versions of Flask.
+        self.assertIn(
+            '<a class="dropdown-item" '
+            'href="/logout/?next=http://localhost/">', output.data)
 
     @patch.dict('pagure.config.config', {'PAGURE_AUTH': 'local'})
     @patch('pagure.lib.notify.send_email', MagicMock(return_value=True))

