示例#1
0
 def test_new_role(self):
     role_id = Role.get_max_id()
     self.login()
     url = url_for('auth.new_role', _external=True)
     data = {
         'name': 'test_role',
         'description': 'Test Role',
         'role_group_id': 1,
         'role_group_name':
         RoleGroup.get_by(id=RoleGroup.get_max_id()).name,
     }
     response = self.client.post(
         url,
         content_type=self.JSON,
         headers=self.set_api_headers(token=self.auth_token),
         data=json.dumps(data),
     )
     response.json = self.get_json_response(response)
     new_item = Role.get_by(name=data['name'])
     item_url = url_for('auth.get_role',
                        item_id=new_item.id,
                        _external=True)
     self.assertEqual(response.status_code, 201)
     self.assertTrue(response.json['data']['url'] == item_url)
     self.assertEqual(new_item.role_group.id, 1)
     Role.set_sequence_value(value=role_id)
     db.session.delete(new_item)
     db.session.commit()
示例#2
0
 def test_delete_role(self):
     self.login()
     role = Role.get_by(name='user')
     url = url_for('auth.delete_role', item_id=role.id, _external=True)
     response = self.client.delete(
         url,
         content_type=self.JSON,
         headers=self.set_api_headers(token=self.auth_token),
     )
     self.assertEqual(response.status_code, 204)
     role.set_not_erased()
示例#3
0
 def test_edit_role_exiting_role(self):
     self.login()
     role = Role.get_by(name='root')
     other_role = Role.get_by(name='user')
     url = url_for('auth.edit_role', item_id=role.id, _external=True)
     data = {
         'name': other_role.name,
         'description': "".join([role.description, '_edited']),
         'role_group_id': role.role_group_id + 1,
         'role_group_name': role.role_group.name,
     }
     response = self.client.put(
         url,
         content_type=self.JSON,
         headers=self.set_api_headers(token=self.auth_token),
         data=json.dumps(data),
     )
     response.json = self.get_json_response(response)
     self.assertEqual(response.status_code, 400)
     self.assertTrue(response.json['error'] == 'bad request')
示例#4
0
 def test_edit_role_no_data(self):
     self.login()
     role = Role.get_by(name='root')
     url = url_for('auth.edit_role', item_id=role.id, _external=True)
     data = {}
     response = self.client.put(
         url,
         content_type=self.JSON,
         headers=self.set_api_headers(token=self.auth_token),
         data=json.dumps(data),
     )
     response.json = self.get_json_response(response)
     self.assertEqual(response.status_code, 400)
     self.assertTrue(response.json['error'] == 'bad request')
示例#5
0
 def test_add_role_to_user_invalid_id(self):
     self.login()
     invalid_id = User.get_invalid_id()
     role = Role.get_by(id=1)
     url = url_for('auth.add_role_to_user',
                   item_id=invalid_id,
                   role_name=role.name,
                   _external=True)
     response = self.client.post(
         url,
         content_type=self.JSON,
         headers=self.set_api_headers(token=self.auth_token, ),
     )
     response.json = self.get_json_response(response)
     self.assertEqual(response.status_code, 404)
     self.assertTrue(response.json['error'] == 'not found')
示例#6
0
 def test_new_role_existing_role(self):
     max_role_id = Role.get_max_id()
     self.login()
     url = url_for('auth.new_role', _external=True)
     data = {
         'name': Role.get_by(id=Role.get_max_id()).name,
         'description': 'Duplicate Role',
         'role_group_id': RoleGroup.get_max_id(),
     }
     response = self.client.post(
         url,
         content_type=self.JSON,
         headers=self.set_api_headers(token=self.auth_token),
         data=json.dumps(data),
     )
     response.json = self.get_json_response(response)
     self.assertEqual(response.status_code, 400)
     self.assertTrue(response.json['error'] == 'bad request')
     Role.set_sequence_value(value=max_role_id)
示例#7
0
 def test_edit_role(self):
     self.login()
     role = Role.get_by(id=1)
     url = url_for('auth.edit_role', item_id=role.id, _external=True)
     data = {
         'name': "".join([role.name, '_edited']),
         'description': "".join([role.description, '_edited']),
         'role_group_id': role.role_group_id + 1,
         'role_group_name': role.role_group.name,
     }
     response = self.client.put(
         url,
         content_type=self.JSON,
         headers=self.set_api_headers(token=self.auth_token),
         data=json.dumps(data),
     )
     response.json = self.get_json_response(response)
     self.assertEqual(response.status_code, 200)
     item_url = url_for('auth.get_role', item_id=role.id, _external=True)
     self.assertTrue(response.json['data']['url'] == item_url)
     role.name, _ = role.name.split('_')
     role.description, _ = role.description.split('_')
     role.role_group_id -= 1
     db.session.commit()