From 478a05a8baf5554d74c6cf640eda750b1e6d2ee6 Mon Sep 17 00:00:00 2001
From: Vivek Anand <vivekanand1101@gmail.com>
Date: Mar 14 2017 14:55:57 +0000
Subject: [PATCH 1/9] api/issues.py: Reword logic of 'since'


Fixes: #2007

---

diff --git a/pagure/api/issue.py b/pagure/api/issue.py
index 601869e..36de336 100644
--- a/pagure/api/issue.py
+++ b/pagure/api/issue.py
@@ -295,45 +295,43 @@ def api_view_issues(repo, username=None, namespace=None):
     if is_repo_committer(repo):
         private = None
 
+    params = {
+        'session': SESSION,
+        'repo': repo,
+        'tags': tags,
+        'assignee': assignee,
+        'author': author,
+        'private': private,
+    }
+
     if status is not None:
-        params = {
-            'session': SESSION,
-            'repo': repo,
-            'tags': tags,
-            'assignee': assignee,
-            'author': author,
-            'private': private
-        }
-        if status.lower() == 'closed':
+        if status.lower() == 'all':
+            params.update({'status': None})
+        elif status.lower() == 'closed':
             params.update({'closed': True})
-        elif status.lower() != 'all':
+        else:
             params.update({'status': status})
-        issues = pagure.lib.search_issues(**params)
-    else:
-        updated_after = None
-        if since:
-            # Validate and convert the time
-            if since.isdigit():
-                # We assume its a timestamp, so convert it to datetime
-                try:
-                    updated_after = \
-                        datetime.datetime.fromtimestamp(int(since))
-                except ValueError:
-                    raise pagure.exceptions.APIError(
-                        400, error_code=APIERROR.ETIMESTAMP)
-            else:
-                # We assume datetime format, so validate it
-                try:
-                    updated_after= datetime.datetime.strptime(
-                        since, '%Y-%m-%d')
-                except ValueError:
-                    raise pagure.exceptions.APIError(
-                        400, error_code=APIERROR.EDATETIME)
-
-        issues = pagure.lib.search_issues(
-            SESSION, repo, status='Open', tags=tags, assignee=assignee,
-            author=author, private=private, updated_after=updated_after)
 
+    updated_after = None
+    if since:
+        # Validate and convert the time
+        if since.isdigit():
+            # We assume its a timestamp, so convert it to datetime
+            try:
+                updated_after = datetime.datetime.fromtimestamp(int(since))
+            except ValueError:
+                raise pagure.exceptions.APIError(
+                    400, error_code=APIERROR.ETIMESTAMP)
+        else:
+            # We assume datetime format, so validate it
+            try:
+                updated_after = datetime.datetime.strptime(since, '%Y-%m-%d')
+            except ValueError:
+                raise pagure.exceptions.APIError(
+                    400, error_code=APIERROR.EDATETIME)
+
+    params.update({'updated_after': updated_after})
+    issues = pagure.lib.search_issues(**params)
     jsonout = flask.jsonify({
         'total_issues': len(issues),
         'issues': [issue.to_json(public=True) for issue in issues],

From e83d2d9369d1f8dc8cf327c789b0e8640a4756d2 Mon Sep 17 00:00:00 2001
From: Vivek Anand <vivekanand1101@gmail.com>
Date: Mar 14 2017 14:55:57 +0000
Subject: [PATCH 2/9] Unit test: view_issues via api when status=all and since


Signed-off-by: Vivek Anand <vivekanand1101@gmail.com>

---

diff --git a/tests/test_pagure_flask_api_issue.py b/tests/test_pagure_flask_api_issue.py
index 94eabd6..a6d3bd8 100644
--- a/tests/test_pagure_flask_api_issue.py
+++ b/tests/test_pagure_flask_api_issue.py
@@ -1013,6 +1013,29 @@ class PagureFlaskApiIssuetests(tests.Modeltests):
             }
         )
 
+        # Test since when status is 'all'
+        output = self.app.get(
+            '/api/0/test/issues?status=all&since=1431414700', headers=headers)
+        self.assertEqual(output.status_code, 200)
+        data = json.loads(output.data)
+        for idx in range(len(data['issues'])):
+            data['issues'][idx]['last_updated'] = '1431414800'
+            data['issues'][idx]['date_created'] = '1431414800'
+        self.assertDictEqual(
+            data,
+            {
+              "args": {
+                "assignee": None,
+                "author": None,
+                "since": '1431414700',
+                "status": "all",
+                "tags": []
+              },
+              "issues": FULL_ISSUE_LIST,
+              "total_issues": 9
+            }
+        )
+
     def test_api_view_issue(self):
         """ Test the api_view_issue method of the flask api. """
         self.test_api_new_issue()

From fd97589658c28fb470f6170b9142e9b18a70371e Mon Sep 17 00:00:00 2001
From: Vivek Anand <vivekanand1101@gmail.com>
Date: Mar 14 2017 14:55:57 +0000
Subject: [PATCH 3/9] Unit tests: api_view_issues when status is Open and since


Signed-off-by: Vivek Anand <vivekanand1101@gmail.com>

---

diff --git a/tests/test_pagure_flask_api_issue.py b/tests/test_pagure_flask_api_issue.py
index a6d3bd8..49733cf 100644
--- a/tests/test_pagure_flask_api_issue.py
+++ b/tests/test_pagure_flask_api_issue.py
@@ -1036,6 +1036,29 @@ class PagureFlaskApiIssuetests(tests.Modeltests):
             }
         )
 
+        # Test since when status is 'Open'
+        output = self.app.get(
+            '/api/0/test/issues?status=Open&since=1431414700', headers=headers)
+        self.assertEqual(output.status_code, 200)
+        data = json.loads(output.data)
+        for idx in range(len(data['issues'])):
+            data['issues'][idx]['last_updated'] = '1431414800'
+            data['issues'][idx]['date_created'] = '1431414800'
+        self.assertDictEqual(
+            data,
+            {
+              "args": {
+                "assignee": None,
+                "author": None,
+                "since": '1431414700',
+                "status": "Open",
+                "tags": []
+              },
+              "issues": FULL_ISSUE_LIST,
+              "total_issues": 9
+            }
+        )
+
     def test_api_view_issue(self):
         """ Test the api_view_issue method of the flask api. """
         self.test_api_new_issue()

From 268ccafc60b95f6aa24750da4378cc1b20f1047a Mon Sep 17 00:00:00 2001
From: Vivek Anand <vivekanand1101@gmail.com>
Date: Mar 14 2017 14:55:57 +0000
Subject: [PATCH 4/9] Unit tests: api_view_issues when status is Closed and since


Signed-off-by: Vivek Anand <vivekanand1101@gmail.com>

---

diff --git a/tests/test_pagure_flask_api_issue.py b/tests/test_pagure_flask_api_issue.py
index 49733cf..9c8aaf5 100644
--- a/tests/test_pagure_flask_api_issue.py
+++ b/tests/test_pagure_flask_api_issue.py
@@ -1059,6 +1059,29 @@ class PagureFlaskApiIssuetests(tests.Modeltests):
             }
         )
 
+        # Test since when status is 'Closed'
+        output = self.app.get(
+            '/api/0/test/issues?status=Closed&since=1431414700', headers=headers)
+        self.assertEqual(output.status_code, 200)
+        data = json.loads(output.data)
+        for idx in range(len(data['issues'])):
+            data['issues'][idx]['last_updated'] = '1431414800'
+            data['issues'][idx]['date_created'] = '1431414800'
+        self.assertDictEqual(
+            data,
+            {
+              "args": {
+                "assignee": None,
+                "author": None,
+                "since": '1431414700',
+                "status": "Closed",
+                "tags": []
+              },
+              "issues": [],
+              "total_issues": 0
+            }
+        )
+
     def test_api_view_issue(self):
         """ Test the api_view_issue method of the flask api. """
         self.test_api_new_issue()

From e73fe3a14cc02c4dd1812b63f2fbcf27cce3def8 Mon Sep 17 00:00:00 2001
From: Vivek Anand <vivekanand1101@gmail.com>
Date: Mar 14 2017 14:55:57 +0000
Subject: [PATCH 5/9] Unit test: api_view_issues when status is an absurd input


Signed-off-by: Vivek Anand <vivekanand1101@gmail.com>

---

diff --git a/tests/test_pagure_flask_api_issue.py b/tests/test_pagure_flask_api_issue.py
index 9c8aaf5..f4d77b2 100644
--- a/tests/test_pagure_flask_api_issue.py
+++ b/tests/test_pagure_flask_api_issue.py
@@ -1082,6 +1082,30 @@ class PagureFlaskApiIssuetests(tests.Modeltests):
             }
         )
 
+        # Test since when status is an absurd input
+        output = self.app.get(
+            '/api/0/test/issues?status=hello&since=1431414700', headers=headers)
+        self.assertEqual(output.status_code, 200)
+        data = json.loads(output.data)
+        for idx in range(len(data['issues'])):
+            data['issues'][idx]['last_updated'] = '1431414800'
+            data['issues'][idx]['date_created'] = '1431414800'
+        self.assertDictEqual(
+            data,
+            {
+              "args": {
+                "assignee": None,
+                "author": None,
+                "since": '1431414700',
+                "status": "hello",
+                "tags": []
+              },
+              "issues": [],
+              "total_issues": 0
+            }
+        )
+
+
     def test_api_view_issue(self):
         """ Test the api_view_issue method of the flask api. """
         self.test_api_new_issue()

From bb317c902fe32a3fcddcacd79a32e1b9faedd492 Mon Sep 17 00:00:00 2001
From: Vivek Anand <vivekanand1101@gmail.com>
Date: Mar 14 2017 14:55:57 +0000
Subject: [PATCH 6/9] Unit test: api_view_issues tests for since


Signed-off-by: Vivek Anand <vivekanand1101@gmail.com>

---

diff --git a/tests/test_pagure_flask_api_issue.py b/tests/test_pagure_flask_api_issue.py
index f4d77b2..ecb556c 100644
--- a/tests/test_pagure_flask_api_issue.py
+++ b/tests/test_pagure_flask_api_issue.py
@@ -1013,6 +1013,52 @@ class PagureFlaskApiIssuetests(tests.Modeltests):
             }
         )
 
+        # Test since
+        output = self.app.get(
+            '/api/0/test/issues?since=1431414700', headers=headers)
+        self.assertEqual(output.status_code, 200)
+        data = json.loads(output.data)
+        for idx in range(len(data['issues'])):
+            data['issues'][idx]['last_updated'] = '1431414800'
+            data['issues'][idx]['date_created'] = '1431414800'
+        self.assertDictEqual(
+            data,
+            {
+              "args": {
+                "assignee": None,
+                "author": None,
+                "since": '1431414700',
+                "status": None,
+                "tags": []
+              },
+              "issues": FULL_ISSUE_LIST,
+              "total_issues": 9
+            }
+        )
+
+        # Test since
+        output = self.app.get(
+            '/api/0/test/issues?since=1531414800', headers=headers)
+        self.assertEqual(output.status_code, 200)
+        data = json.loads(output.data)
+        for idx in range(len(data['issues'])):
+            data['issues'][idx]['last_updated'] = '1431414800'
+            data['issues'][idx]['date_created'] = '1431414800'
+        self.assertDictEqual(
+            data,
+            {
+              "args": {
+                "assignee": None,
+                "author": None,
+                "since": '1531414800',
+                "status": None,
+                "tags": []
+              },
+              "issues": [],
+              "total_issues": 0
+            }
+        )
+
         # Test since when status is 'all'
         output = self.app.get(
             '/api/0/test/issues?status=all&since=1431414700', headers=headers)

From f0e3cc0e46cd445af7684a380231ef09ff9ee6ad Mon Sep 17 00:00:00 2001
From: Vivek Anand <vivekanand1101@gmail.com>
Date: Mar 14 2017 14:55:57 +0000
Subject: [PATCH 7/9] Unit test: Move all tests for api_view_issues with since var to another function


Signed-off-by: Vivek Anand <vivekanand1101@gmail.com>

---

diff --git a/tests/test_pagure_flask_api_issue.py b/tests/test_pagure_flask_api_issue.py
index ecb556c..fc5263b 100644
--- a/tests/test_pagure_flask_api_issue.py
+++ b/tests/test_pagure_flask_api_issue.py
@@ -1013,7 +1013,146 @@ class PagureFlaskApiIssuetests(tests.Modeltests):
             }
         )
 
-        # Test since
+    def test_api_view_issues_since(self):
+        """ Test the api_view_issues method of the flask api for since option """
+        self.test_api_new_issue()
+
+        # Invalid repo
+        output = self.app.get('/api/0/foo/issues')
+        self.assertEqual(output.status_code, 404)
+        data = json.loads(output.data)
+        self.assertDictEqual(
+            data,
+            {
+              "error": "Project not found",
+              "error_code": "ENOPROJECT",
+            }
+        )
+
+        # List all opened issues
+        output = self.app.get('/api/0/test/issues')
+        self.assertEqual(output.status_code, 200)
+        data = json.loads(output.data)
+        for idx in range(len(data['issues'])):
+            data['issues'][idx]['date_created'] = '1431414800'
+            data['issues'][idx]['last_updated'] = '1431414800'
+        self.assertDictEqual(
+            data,
+            {
+              "args": {
+                "assignee": None,
+                "author": None,
+                "since": None,
+                "status": None,
+                "tags": []
+              },
+              "issues": FULL_ISSUE_LIST[3:],
+              "total_issues": 6
+            }
+        )
+
+        # Create private issue
+        repo = pagure.lib.get_project(self.session, 'test')
+        msg = pagure.lib.new_issue(
+            session=self.session,
+            repo=repo,
+            title='Test issue',
+            content='We should work on this',
+            user='pingou',
+            ticketfolder=None,
+            private=True,
+            milestone=""
+        )
+        self.session.commit()
+        self.assertEqual(msg.title, 'Test issue')
+
+        # Access issues un-authenticated
+        output = self.app.get('/api/0/test/issues')
+        self.assertEqual(output.status_code, 200)
+        data = json.loads(output.data)
+        for idx in range(len(data['issues'])):
+            data['issues'][idx]['date_created'] = '1431414800'
+            data['issues'][idx]['last_updated'] = '1431414800'
+        self.assertDictEqual(
+            data,
+            {
+              "args": {
+                "assignee": None,
+                "author": None,
+                "since": None,
+                "status": None,
+                "tags": []
+              },
+              "issues": FULL_ISSUE_LIST[3:],
+              "total_issues": 6
+            }
+        )
+        headers = {'Authorization': 'token aaabbbccc'}
+
+        # Access issues authenticated but non-existing token
+        output = self.app.get('/api/0/test/issues', headers=headers)
+        self.assertEqual(output.status_code, 401)
+
+        # Create a new token for another user
+        item = pagure.lib.model.Token(
+            id='bar_token',
+            user_id=2,
+            project_id=1,
+            expiration=datetime.datetime.utcnow() + datetime.timedelta(
+                days=30)
+        )
+        self.session.add(item)
+
+        headers = {'Authorization': 'token bar_token'}
+
+        # Access issues authenticated but wrong token
+        output = self.app.get('/api/0/test/issues', headers=headers)
+        self.assertEqual(output.status_code, 200)
+        data = json.loads(output.data)
+        for idx in range(len(data['issues'])):
+            data['issues'][idx]['date_created'] = '1431414800'
+            data['issues'][idx]['last_updated'] = '1431414800'
+        self.assertDictEqual(
+            data,
+            {
+              "args": {
+                "assignee": None,
+                "author": None,
+                "since": None,
+                "status": None,
+                "tags": []
+              },
+              "issues": FULL_ISSUE_LIST[3:],
+              "total_issues": 6
+            }
+        )
+
+        headers = {'Authorization': 'token aaabbbcccddd'}
+
+        # Access issues authenticated correctly
+        output = self.app.get('/api/0/test/issues', headers=headers)
+        self.assertEqual(output.status_code, 200)
+        data = json.loads(output.data)
+        for idx in range(len(data['issues'])):
+            data['issues'][idx]['date_created'] = '1431414800'
+            data['issues'][idx]['last_updated'] = '1431414800'
+
+        self.assertDictEqual(
+            data,
+            {
+              "args": {
+                "assignee": None,
+                "author": None,
+                "since": None,
+                "status": None,
+                "tags": []
+              },
+              "issues": FULL_ISSUE_LIST,
+              "total_issues": 9
+            }
+        )
+        headers = {'Authorization': 'token aaabbbcccddd'}
+        # Test since for a value before creation of issues
         output = self.app.get(
             '/api/0/test/issues?since=1431414700', headers=headers)
         self.assertEqual(output.status_code, 200)
@@ -1036,7 +1175,7 @@ class PagureFlaskApiIssuetests(tests.Modeltests):
             }
         )
 
-        # Test since
+        # Test since for a value after creation of all the issues
         output = self.app.get(
             '/api/0/test/issues?since=1531414800', headers=headers)
         self.assertEqual(output.status_code, 200)
@@ -1061,7 +1200,7 @@ class PagureFlaskApiIssuetests(tests.Modeltests):
 
         # Test since when status is 'all'
         output = self.app.get(
-            '/api/0/test/issues?status=all&since=1431414700', headers=headers)
+            '/api/0/test/issues?status=all&since=1231414800', headers=headers)
         self.assertEqual(output.status_code, 200)
         data = json.loads(output.data)
         for idx in range(len(data['issues'])):
@@ -1073,11 +1212,11 @@ class PagureFlaskApiIssuetests(tests.Modeltests):
               "args": {
                 "assignee": None,
                 "author": None,
-                "since": '1431414700',
+                "since": '1231414800',
                 "status": "all",
                 "tags": []
               },
-              "issues": FULL_ISSUE_LIST,
+                "issues": FULL_ISSUE_LIST,
               "total_issues": 9
             }
         )
@@ -1100,7 +1239,7 @@ class PagureFlaskApiIssuetests(tests.Modeltests):
                 "status": "Open",
                 "tags": []
               },
-              "issues": FULL_ISSUE_LIST,
+                "issues": FULL_ISSUE_LIST,
               "total_issues": 9
             }
         )
@@ -1151,7 +1290,6 @@ class PagureFlaskApiIssuetests(tests.Modeltests):
             }
         )
 
-
     def test_api_view_issue(self):
         """ Test the api_view_issue method of the flask api. """
         self.test_api_new_issue()

From efe4a885e00f1069e0a97b82555c6c3f87bd840c Mon Sep 17 00:00:00 2001
From: Pierre-Yves Chibon <pingou@pingoured.fr>
Date: Mar 14 2017 14:55:57 +0000
Subject: [PATCH 8/9] Adjust the unit-tests to not hard-code the date


---

diff --git a/tests/test_pagure_flask_api_issue.py b/tests/test_pagure_flask_api_issue.py
index fc5263b..ce69758 100644
--- a/tests/test_pagure_flask_api_issue.py
+++ b/tests/test_pagure_flask_api_issue.py
@@ -15,6 +15,7 @@ import datetime
 import unittest
 import shutil
 import sys
+import time
 import os
 
 import json
@@ -1015,6 +1016,7 @@ class PagureFlaskApiIssuetests(tests.Modeltests):
 
     def test_api_view_issues_since(self):
         """ Test the api_view_issues method of the flask api for since option """
+        start = datetime.datetime.utcnow().strftime('%s')
         self.test_api_new_issue()
 
         # Invalid repo
@@ -1066,95 +1068,13 @@ class PagureFlaskApiIssuetests(tests.Modeltests):
         self.session.commit()
         self.assertEqual(msg.title, 'Test issue')
 
-        # Access issues un-authenticated
-        output = self.app.get('/api/0/test/issues')
-        self.assertEqual(output.status_code, 200)
-        data = json.loads(output.data)
-        for idx in range(len(data['issues'])):
-            data['issues'][idx]['date_created'] = '1431414800'
-            data['issues'][idx]['last_updated'] = '1431414800'
-        self.assertDictEqual(
-            data,
-            {
-              "args": {
-                "assignee": None,
-                "author": None,
-                "since": None,
-                "status": None,
-                "tags": []
-              },
-              "issues": FULL_ISSUE_LIST[3:],
-              "total_issues": 6
-            }
-        )
-        headers = {'Authorization': 'token aaabbbccc'}
-
-        # Access issues authenticated but non-existing token
-        output = self.app.get('/api/0/test/issues', headers=headers)
-        self.assertEqual(output.status_code, 401)
-
-        # Create a new token for another user
-        item = pagure.lib.model.Token(
-            id='bar_token',
-            user_id=2,
-            project_id=1,
-            expiration=datetime.datetime.utcnow() + datetime.timedelta(
-                days=30)
-        )
-        self.session.add(item)
-
-        headers = {'Authorization': 'token bar_token'}
-
-        # Access issues authenticated but wrong token
-        output = self.app.get('/api/0/test/issues', headers=headers)
-        self.assertEqual(output.status_code, 200)
-        data = json.loads(output.data)
-        for idx in range(len(data['issues'])):
-            data['issues'][idx]['date_created'] = '1431414800'
-            data['issues'][idx]['last_updated'] = '1431414800'
-        self.assertDictEqual(
-            data,
-            {
-              "args": {
-                "assignee": None,
-                "author": None,
-                "since": None,
-                "status": None,
-                "tags": []
-              },
-              "issues": FULL_ISSUE_LIST[3:],
-              "total_issues": 6
-            }
-        )
+        time.sleep(1)
 
         headers = {'Authorization': 'token aaabbbcccddd'}
 
-        # Access issues authenticated correctly
-        output = self.app.get('/api/0/test/issues', headers=headers)
-        self.assertEqual(output.status_code, 200)
-        data = json.loads(output.data)
-        for idx in range(len(data['issues'])):
-            data['issues'][idx]['date_created'] = '1431414800'
-            data['issues'][idx]['last_updated'] = '1431414800'
-
-        self.assertDictEqual(
-            data,
-            {
-              "args": {
-                "assignee": None,
-                "author": None,
-                "since": None,
-                "status": None,
-                "tags": []
-              },
-              "issues": FULL_ISSUE_LIST,
-              "total_issues": 9
-            }
-        )
-        headers = {'Authorization': 'token aaabbbcccddd'}
         # Test since for a value before creation of issues
         output = self.app.get(
-            '/api/0/test/issues?since=1431414700', headers=headers)
+            '/api/0/test/issues?since=%s' % start, headers=headers)
         self.assertEqual(output.status_code, 200)
         data = json.loads(output.data)
         for idx in range(len(data['issues'])):
@@ -1166,7 +1086,7 @@ class PagureFlaskApiIssuetests(tests.Modeltests):
               "args": {
                 "assignee": None,
                 "author": None,
-                "since": '1431414700',
+                "since": start,
                 "status": None,
                 "tags": []
               },
@@ -1175,9 +1095,10 @@ class PagureFlaskApiIssuetests(tests.Modeltests):
             }
         )
 
+        late = datetime.datetime.utcnow().strftime('%s')
         # Test since for a value after creation of all the issues
         output = self.app.get(
-            '/api/0/test/issues?since=1531414800', headers=headers)
+            '/api/0/test/issues?since=%s' % late, headers=headers)
         self.assertEqual(output.status_code, 200)
         data = json.loads(output.data)
         for idx in range(len(data['issues'])):
@@ -1189,7 +1110,7 @@ class PagureFlaskApiIssuetests(tests.Modeltests):
               "args": {
                 "assignee": None,
                 "author": None,
-                "since": '1531414800',
+                "since": late,
                 "status": None,
                 "tags": []
               },
@@ -1200,7 +1121,7 @@ class PagureFlaskApiIssuetests(tests.Modeltests):
 
         # Test since when status is 'all'
         output = self.app.get(
-            '/api/0/test/issues?status=all&since=1231414800', headers=headers)
+            '/api/0/test/issues?status=all&since=%s' % start, headers=headers)
         self.assertEqual(output.status_code, 200)
         data = json.loads(output.data)
         for idx in range(len(data['issues'])):
@@ -1212,7 +1133,7 @@ class PagureFlaskApiIssuetests(tests.Modeltests):
               "args": {
                 "assignee": None,
                 "author": None,
-                "since": '1231414800',
+                "since": start,
                 "status": "all",
                 "tags": []
               },
@@ -1223,7 +1144,7 @@ class PagureFlaskApiIssuetests(tests.Modeltests):
 
         # Test since when status is 'Open'
         output = self.app.get(
-            '/api/0/test/issues?status=Open&since=1431414700', headers=headers)
+            '/api/0/test/issues?status=Open&since=%s' % start, headers=headers)
         self.assertEqual(output.status_code, 200)
         data = json.loads(output.data)
         for idx in range(len(data['issues'])):
@@ -1235,7 +1156,7 @@ class PagureFlaskApiIssuetests(tests.Modeltests):
               "args": {
                 "assignee": None,
                 "author": None,
-                "since": '1431414700',
+                "since": start,
                 "status": "Open",
                 "tags": []
               },
@@ -1246,7 +1167,7 @@ class PagureFlaskApiIssuetests(tests.Modeltests):
 
         # Test since when status is 'Closed'
         output = self.app.get(
-            '/api/0/test/issues?status=Closed&since=1431414700', headers=headers)
+            '/api/0/test/issues?status=Closed&since=%s' % start, headers=headers)
         self.assertEqual(output.status_code, 200)
         data = json.loads(output.data)
         for idx in range(len(data['issues'])):
@@ -1258,7 +1179,7 @@ class PagureFlaskApiIssuetests(tests.Modeltests):
               "args": {
                 "assignee": None,
                 "author": None,
-                "since": '1431414700',
+                "since": start,
                 "status": "Closed",
                 "tags": []
               },
@@ -1269,7 +1190,7 @@ class PagureFlaskApiIssuetests(tests.Modeltests):
 
         # Test since when status is an absurd input
         output = self.app.get(
-            '/api/0/test/issues?status=hello&since=1431414700', headers=headers)
+            '/api/0/test/issues?status=hello&since=%s' % start, headers=headers)
         self.assertEqual(output.status_code, 200)
         data = json.loads(output.data)
         for idx in range(len(data['issues'])):
@@ -1281,7 +1202,7 @@ class PagureFlaskApiIssuetests(tests.Modeltests):
               "args": {
                 "assignee": None,
                 "author": None,
-                "since": '1431414700',
+                "since": start,
                 "status": "hello",
                 "tags": []
               },

From 3dd3899767b97febf232692426b0338717617131 Mon Sep 17 00:00:00 2001
From: Pierre-Yves Chibon <pingou@pingoured.fr>
Date: Mar 14 2017 14:55:57 +0000
Subject: [PATCH 9/9] Rework the unit-tests to test just the desired feature


---

diff --git a/tests/test_pagure_flask_api_issue.py b/tests/test_pagure_flask_api_issue.py
index ce69758..db066a8 100644
--- a/tests/test_pagure_flask_api_issue.py
+++ b/tests/test_pagure_flask_api_issue.py
@@ -1016,8 +1016,61 @@ class PagureFlaskApiIssuetests(tests.Modeltests):
 
     def test_api_view_issues_since(self):
         """ Test the api_view_issues method of the flask api for since option """
+
+
+        tests.create_projects(self.session)
+        tests.create_projects_git(
+            os.path.join(self.path, 'tickets'), bare=True)
+        tests.create_tokens(self.session)
+        tests.create_tokens_acl(self.session)
+
+        repo = pagure.lib.get_project(self.session, 'test')
+
+        # Create 1st tickets
         start = datetime.datetime.utcnow().strftime('%s')
-        self.test_api_new_issue()
+        issue = pagure.lib.model.Issue(
+            id=pagure.lib.get_next_id(self.session, repo.id),
+            project_id=repo.id,
+            title='Issue #1',
+            content='Description',
+            user_id=1,  # pingou
+            uid='issue#1',
+            private=False,
+        )
+        self.session.add(issue)
+        self.session.commit()
+
+        time.sleep(1)
+        middle = datetime.datetime.utcnow().strftime('%s')
+
+        # Create 2nd tickets
+        issue = pagure.lib.model.Issue(
+            id=pagure.lib.get_next_id(self.session, repo.id),
+            project_id=repo.id,
+            title='Issue #2',
+            content='Description',
+            user_id=1,  # pingou
+            uid='issue#2',
+            private=False,
+        )
+        self.session.add(issue)
+        self.session.commit()
+
+        time.sleep(1)
+        final = datetime.datetime.utcnow().strftime('%s')
+
+        # Create private issue
+        issue = pagure.lib.model.Issue(
+            id=pagure.lib.get_next_id(self.session, repo.id),
+            project_id=repo.id,
+            title='Issue #3',
+            content='Description',
+            user_id=1,  # pingou
+            uid='issue#3',
+            private=True,
+        )
+        self.session.add(issue)
+        self.session.commit()
 
         # Invalid repo
         output = self.app.get('/api/0/foo/issues')
@@ -1031,6 +1084,49 @@ class PagureFlaskApiIssuetests(tests.Modeltests):
             }
         )
 
+        LCL_ISSUES = [
+          {
+            'assignee': None,
+            'blocks': [],
+            'close_status': None,
+            'closed_at': None,
+            'comments': [],
+            'content': 'Description',
+            'custom_fields': [],
+            'date_created': '1431414800',
+            'depends': [],
+            'id': 2,
+            'last_updated': '1431414800',
+            'milestone': None,
+            'priority': None,
+            'private': False,
+            'status': 'Open',
+            'tags': [],
+            'title': 'Issue #2',
+            'user': {'fullname': 'PY C', 'name': 'pingou'}
+          },
+          {
+            'assignee': None,
+            'blocks': [],
+            'close_status': None,
+            'closed_at': None,
+            'comments': [],
+            'content': 'Description',
+            'custom_fields': [],
+            'date_created': '1431414800',
+            'depends': [],
+            'id': 1,
+            'last_updated': '1431414800',
+            'milestone': None,
+            'priority': None,
+            'private': False,
+            'status': 'Open',
+            'tags': [],
+            'title': 'Issue #1',
+            'user': {'fullname': 'PY C', 'name': 'pingou'}
+           }
+        ]
+
         # List all opened issues
         output = self.app.get('/api/0/test/issues')
         self.assertEqual(output.status_code, 200)
@@ -1048,38 +1144,21 @@ class PagureFlaskApiIssuetests(tests.Modeltests):
                 "status": None,
                 "tags": []
               },
-              "issues": FULL_ISSUE_LIST[3:],
-              "total_issues": 6
+              "issues": LCL_ISSUES,
+              "total_issues": 2
             }
         )
 
-        # Create private issue
-        repo = pagure.lib.get_project(self.session, 'test')
-        msg = pagure.lib.new_issue(
-            session=self.session,
-            repo=repo,
-            title='Test issue',
-            content='We should work on this',
-            user='pingou',
-            ticketfolder=None,
-            private=True,
-            milestone=""
-        )
-        self.session.commit()
-        self.assertEqual(msg.title, 'Test issue')
-
         time.sleep(1)
+        late = datetime.datetime.utcnow().strftime('%s')
 
-        headers = {'Authorization': 'token aaabbbcccddd'}
-
-        # Test since for a value before creation of issues
-        output = self.app.get(
-            '/api/0/test/issues?since=%s' % start, headers=headers)
+        # List all opened issues from the start
+        output = self.app.get('/api/0/test/issues?since=%s' % start)
         self.assertEqual(output.status_code, 200)
         data = json.loads(output.data)
         for idx in range(len(data['issues'])):
-            data['issues'][idx]['last_updated'] = '1431414800'
             data['issues'][idx]['date_created'] = '1431414800'
+            data['issues'][idx]['last_updated'] = '1431414800'
         self.assertDictEqual(
             data,
             {
@@ -1090,97 +1169,48 @@ class PagureFlaskApiIssuetests(tests.Modeltests):
                 "status": None,
                 "tags": []
               },
-              "issues": FULL_ISSUE_LIST,
-              "total_issues": 9
+              "issues": LCL_ISSUES,
+              "total_issues": 2
             }
         )
 
-        late = datetime.datetime.utcnow().strftime('%s')
-        # Test since for a value after creation of all the issues
-        output = self.app.get(
-            '/api/0/test/issues?since=%s' % late, headers=headers)
+        # List all opened issues from the middle
+        output = self.app.get('/api/0/test/issues?since=%s' % middle)
         self.assertEqual(output.status_code, 200)
         data = json.loads(output.data)
         for idx in range(len(data['issues'])):
-            data['issues'][idx]['last_updated'] = '1431414800'
             data['issues'][idx]['date_created'] = '1431414800'
+            data['issues'][idx]['last_updated'] = '1431414800'
         self.assertDictEqual(
             data,
             {
               "args": {
                 "assignee": None,
                 "author": None,
-                "since": late,
+                "since": middle,
                 "status": None,
                 "tags": []
               },
-              "issues": [],
-              "total_issues": 0
+              "issues": LCL_ISSUES[:1],
+              "total_issues": 1
             }
         )
 
-        # Test since when status is 'all'
-        output = self.app.get(
-            '/api/0/test/issues?status=all&since=%s' % start, headers=headers)
+        # List all opened issues at the end
+        output = self.app.get('/api/0/test/issues?since=%s' % final)
         self.assertEqual(output.status_code, 200)
         data = json.loads(output.data)
         for idx in range(len(data['issues'])):
-            data['issues'][idx]['last_updated'] = '1431414800'
             data['issues'][idx]['date_created'] = '1431414800'
-        self.assertDictEqual(
-            data,
-            {
-              "args": {
-                "assignee": None,
-                "author": None,
-                "since": start,
-                "status": "all",
-                "tags": []
-              },
-                "issues": FULL_ISSUE_LIST,
-              "total_issues": 9
-            }
-        )
-
-        # Test since when status is 'Open'
-        output = self.app.get(
-            '/api/0/test/issues?status=Open&since=%s' % start, headers=headers)
-        self.assertEqual(output.status_code, 200)
-        data = json.loads(output.data)
-        for idx in range(len(data['issues'])):
             data['issues'][idx]['last_updated'] = '1431414800'
-            data['issues'][idx]['date_created'] = '1431414800'
         self.assertDictEqual(
             data,
             {
               "args": {
                 "assignee": None,
                 "author": None,
-                "since": start,
-                "status": "Open",
-                "tags": []
-              },
-                "issues": FULL_ISSUE_LIST,
-              "total_issues": 9
-            }
-        )
-
-        # Test since when status is 'Closed'
-        output = self.app.get(
-            '/api/0/test/issues?status=Closed&since=%s' % start, headers=headers)
-        self.assertEqual(output.status_code, 200)
-        data = json.loads(output.data)
-        for idx in range(len(data['issues'])):
-            data['issues'][idx]['last_updated'] = '1431414800'
-            data['issues'][idx]['date_created'] = '1431414800'
-        self.assertDictEqual(
-            data,
-            {
-              "args": {
-                "assignee": None,
-                "author": None,
-                "since": start,
-                "status": "Closed",
+                "since": final,
+                "status": None,
                 "tags": []
               },
               "issues": [],
@@ -1188,9 +1218,11 @@ class PagureFlaskApiIssuetests(tests.Modeltests):
             }
         )
 
-        # Test since when status is an absurd input
+        headers = {'Authorization': 'token aaabbbcccddd'}
+
+        # Test since for a value before creation of issues
         output = self.app.get(
-            '/api/0/test/issues?status=hello&since=%s' % start, headers=headers)
+            '/api/0/test/issues?since=%s' % final, headers=headers)
         self.assertEqual(output.status_code, 200)
         data = json.loads(output.data)
         for idx in range(len(data['issues'])):
@@ -1202,12 +1234,31 @@ class PagureFlaskApiIssuetests(tests.Modeltests):
               "args": {
                 "assignee": None,
                 "author": None,
-                "since": start,
-                "status": "hello",
+                "since": final,
+                "status": None,
                 "tags": []
               },
-              "issues": [],
-              "total_issues": 0
+              "issues": [{
+                'assignee': None,
+                'blocks': [],
+                'close_status': None,
+                'closed_at': None,
+                'comments': [],
+                'content': 'Description',
+                'custom_fields': [],
+                'date_created': '1431414800',
+                'depends': [],
+                'id': 3,
+                'last_updated': '1431414800',
+                'milestone': None,
+                'priority': None,
+                'private': True,
+                'status': 'Open',
+                'tags': [],
+                'title': 'Issue #3',
+                'user': {'fullname': 'PY C', 'name': 'pingou'}}
+              ],
+              "total_issues": 1
             }
         )
 

