Repository: freedomofpress/securedrop

Issue:
Add integration tests for `admin_edit_user`
Integration tests need to be added to the endpoint for `admin_edit_user`.

From this comment: https://github.com/freedomofpress/securedrop/pull/1509#issuecomment-323562439


Discussion:

Resolving patch:
diff --git a/securedrop/journalist.py b/securedrop/journalist.py
--- a/securedrop/journalist.py
+++ b/securedrop/journalist.py
@@ -298,7 +298,7 @@ def admin_edit_user(user_id):
     user = Journalist.query.get(user_id)
 
     if request.method == 'POST':
-        if request.form['username']:
+        if request.form.get('username', None):
             new_username = request.form['username']
 
             try:


Test patch:
diff --git a/securedrop/tests/test_journalist.py b/securedrop/tests/test_journalist.py
--- a/securedrop/tests/test_journalist.py
+++ b/securedrop/tests/test_journalist.py
@@ -316,7 +316,7 @@ def test_admin_add_user_when_username_already_in_use(self):
         resp = self.client.post(url_for('admin_add_user'),
                                 data=dict(username=self.admin.username,
                                           password=VALID_PASSWORD,
-                                          is_admin=False))
+                                          is_admin=None))
         self.assertIn('That username is already in use', resp.data)
 
     def test_max_password_length(self):
@@ -344,13 +344,12 @@ def test_admin_edits_user_password_too_long_warning(self):
         overly_long_password = VALID_PASSWORD + \
             'a' * (Journalist.MAX_PASSWORD_LEN - len(VALID_PASSWORD) + 1)
 
-        resp = self.client.post(
+        self.client.post(
             url_for('admin_new_password', user_id=self.user.id),
-            data=dict(username=self.user.username, is_admin=False,
+            data=dict(username=self.user.username, is_admin=None,
                       password=overly_long_password),
             follow_redirects=True)
 
-        print resp.data.decode('utf-8')
         self.assertMessageFlashed('You submitted a bad password! '
                                   'Password not changed.', 'error')
 
@@ -375,7 +374,7 @@ def test_admin_add_user_password_too_long_warning(self):
             url_for('admin_add_user'),
             data=dict(username='dellsberg',
                       password=overly_long_password,
-                      is_admin=False))
+                      is_admin=None))
 
         self.assertMessageFlashed('There was an error with the autogenerated '
                                   'password. User not created. '
@@ -389,7 +388,7 @@ def test_admin_edits_user_invalid_username(self):
 
         self.client.post(
             url_for('admin_edit_user', user_id=self.user.id),
-            data=dict(username=new_username, is_admin=False))
+            data=dict(username=new_username, is_admin=None))
 
         self.assertMessageFlashed('Username "{}" is already taken!'.format(
             new_username), 'error')
@@ -534,9 +533,7 @@ def test_admin_add_user(self):
         resp = self.client.post(url_for('admin_add_user'),
                                 data=dict(username='dellsberg',
                                           password=VALID_PASSWORD,
-                                          is_admin=False))
-
-        print resp.data.decode('utf-8')
+                                          is_admin=None))
 
         self.assertRedirects(resp, url_for('admin_new_user_two_factor',
                                            uid=max_journalist_pk+1))
@@ -546,7 +543,7 @@ def test_admin_add_user_without_username(self):
         resp = self.client.post(url_for('admin_add_user'),
                                 data=dict(username='',
                                           password=VALID_PASSWORD,
-                                          is_admin=False))
+                                          is_admin=None))
         self.assertIn('Invalid username', resp.data)
 
     def test_admin_add_user_too_short_username(self):
@@ -556,9 +553,47 @@ def test_admin_add_user_too_short_username(self):
                                 data=dict(username=username,
                                           password='pentagonpapers',
                                           password_again='pentagonpapers',
-                                          is_admin=False))
+                                          is_admin=None))
         self.assertIn('Invalid username', resp.data)
 
+    def test_admin_sets_user_to_admin(self):
+        self._login_admin()
+        new_user = 'admin-set-user-to-admin-test'
+        resp = self.client.post(url_for('admin_add_user'),
+                                data=dict(username=new_user,
+                                          password=VALID_PASSWORD,
+                                          is_admin=None))
+        assert resp.status_code in (200, 302)
+        journo = Journalist.query.filter(Journalist.username == new_user).one()
+        assert not journo.is_admin
+
+        resp = self.client.post(url_for('admin_edit_user', user_id=journo.id),
+                                data=dict(is_admin=True))
+        assert resp.status_code in (200, 302), resp.data.decode('utf-8')
+
+        # there are better ways to do this, but flake8 complains
+        journo = Journalist.query.filter(Journalist.username == new_user).one()
+        assert journo.is_admin is True
+
+    def test_admin_renames_user(self):
+        self._login_admin()
+        new_user = 'admin-renames-user-test'
+        resp = self.client.post(url_for('admin_add_user'),
+                                data=dict(username=new_user,
+                                          password=VALID_PASSWORD,
+                                          is_admin=None))
+        assert resp.status_code in (200, 302)
+        journo = Journalist.query.filter(Journalist.username == new_user).one()
+
+        new_user = new_user + 'a'
+        resp = self.client.post(url_for('admin_edit_user', user_id=journo.id),
+                                data=dict(username=new_user))
+        assert resp.status_code in (200, 302), resp.data.decode('utf-8')
+
+        # the following will throw an exception if new_user is not found
+        # therefore asserting it has been created
+        Journalist.query.filter(Journalist.username == new_user).one()
+
     @patch('journalist.app.logger.error')
     @patch('journalist.Journalist',
            side_effect=IntegrityError('STATEMENT', 'PARAMETERS', None))
@@ -570,7 +605,7 @@ def test_admin_add_user_integrity_error(self,
         self.client.post(url_for('admin_add_user'),
                          data=dict(username='username',
                                    password=VALID_PASSWORD,
-                                   is_admin=False))
+                                   is_admin=None))
 
         mocked_error_logger.assert_called_once_with(
             "Adding user 'username' failed: (__builtin__.NoneType) "