def setUpClass(cls): """ Set up all the required data here. - Setup admin user - Setup hr user - Setup member :return: None """ api_key, cls._superuser_key = h.create_superuser() h.create_all_users(cls._superuser_key) api_key, cls._admin_key = h.get_api_key("admin") api_key, cls._hr_key = h.get_api_key("hr") api_key, cls._member_key = h.get_api_key("member") return None
def test_employee_update_parameters(self): """ Test employee update by different parameters :return: """ api_headers_admin = { "HTTP_API_KEY": self._admin_key, 'content_type': 'application/json; charset=UTF-8', 'Accept': 'application/json' } api_headers_member = { "HTTP_API_KEY": self._member_key, 'content_type': 'application/json; charset=UTF-8', 'Accept': 'application/json' } api_headers_hr = { "HTTP_API_KEY": self._hr_key, 'content_type': 'application/json; charset=UTF-8', 'Accept': 'application/json' } _data = copy.deepcopy(test_data.test_data_member_role) res, code = h.create_employee(api_headers_admin, _data) self.assertEqual(code, 200) _employee_id = res['result']['employee_id'] _id = res['result']['id'] # ***************** Case 1: update work_email, first_name and last_name by admin _update_data = { "id": _id, "last_name": "testmy_name", "work_email": "*****@*****.**" } res, code = h.update_employee(api_headers_admin, _update_data) self.assertEqual(code, 200) self.assertEqual(res['result']['last_name'], _update_data['last_name']) self.assertEqual(res['result']['work_email'], _update_data['work_email']) # ***************** Case 2: update work_email and last_name by hr _update_data = { "id": _employee_id, "last_name": "testmy2_name", "work_email": "*****@*****.**", "first_name": "testmy2_firstname" } res, code = h.update_employee(api_headers_hr, _update_data) self.assertEqual(code, 200) self.assertEqual(res['result']['last_name'], _update_data['last_name']) self.assertEqual(res['result']['work_email'], _update_data['work_email']) self.assertEqual(res['result']['first_name'], _update_data['first_name']) # ***************** Case 3: change joining_date by self api_key, _key = h.get_api_key(email="*****@*****.**") api_headers_self = { "HTTP_API_KEY": _key, 'content_type': 'application/json; charset=UTF-8', 'Accept': 'application/json' } _update_data = { "id": _employee_id, "joining_date": "1992-07-09", } with self.assertRaises(core_err.NotAuthorizedError): res, code = h.update_employee(api_headers_self, _update_data) # ***************** Case 4: change work_country_code by self _update_data = { "id": _employee_id, "work_country_code": "ie", } with self.assertRaises(core_err.NotAuthorizedError): res, code = h.update_employee(api_headers_self, _update_data) _update_data = { "id": _employee_id, "position": "dev", } res, code = h.update_employee(api_headers_self, _update_data) self.assertEqual(res['result']['position'].lower(), _update_data['position'].lower()) # ***************** Case 5: change bio by self _update_data = { "id": _employee_id, "bio": "My name is fun and this is new app", } res, code = h.update_employee(api_headers_self, _update_data) self.assertEqual(code, 200) self.assertEqual(res['result']['bio'].lower(), _update_data['bio'].lower()) # ****************** Case 6: Link error _update_data = {"id": _employee_id, "social_github": "iasdasde"} with self.assertRaises(core_err.ValidationError): res, code = h.update_employee(api_headers_self, _update_data) _update_data = {"id": _employee_id, "social_github": ""} res, code = h.update_employee(api_headers_self, _update_data) self.assertEqual(code, 200) self.assertEqual(res['result']['social_github'].lower(), _update_data['social_github'].lower())
def test_employee_update_employee_id(self): """ Try changing employee_id by admin, member, hr and self :return: """ api_headers_admin = { "HTTP_API_KEY": self._admin_key, 'content_type': 'application/json; charset=UTF-8', 'Accept': 'application/json' } api_headers_member = { "HTTP_API_KEY": self._member_key, 'content_type': 'application/json; charset=UTF-8', 'Accept': 'application/json' } api_headers_hr = { "HTTP_API_KEY": self._hr_key, 'content_type': 'application/json; charset=UTF-8', 'Accept': 'application/json' } _data = copy.deepcopy(test_data.test_data_member_role) res, code = h.create_employee(api_headers_admin, _data) self.assertEqual(code, 200) _employee_id = res['result']['employee_id'] _id = res['result']['id'] _email = res['result']['work_email'] # ***************** Case 1: change employee id by admin _update_data = { "id": _id, "employee_id": "DLX-test-123", } res, code = h.update_employee(api_headers_admin, _update_data) self.assertEqual(code, 200) self.assertEqual(res['result']['employee_id'].lower(), _update_data['employee_id'].lower()) # ***************** Case 2: change employee id by hr using employee id _update_data = { "id": "DLX-test-123", "employee_id": "DLX-test-1234", } res, code = h.update_employee(api_headers_hr, _update_data) self.assertEqual(code, 200) self.assertEqual(res['result']['employee_id'].lower(), _update_data['employee_id'].lower()) # ***************** Case 3: change employee id by member _update_data = { "id": _id, "employee_id": "DLX-test-1234567", } with self.assertRaises(core_err.NotAuthorizedError): res, code = h.update_employee(api_headers_member, _update_data) # ***************** Case 4: change employee id by self api_key, _key = h.get_api_key(email=_email) api_headers_self = { "HTTP_API_KEY": _key, 'content_type': 'application/json; charset=UTF-8', 'Accept': 'application/json' } _update_data = { "id": _id, "employee_id": "DLX-test-my-123", } with self.assertRaises(core_err.NotAuthorizedError): res, code = h.update_employee(api_headers_self, _update_data)
def test_employee_update_by_role(self): """ Profiles with member role should not be allowed to update others profile :return: """ api_headers_admin = { "HTTP_API_KEY": self._admin_key, 'content_type': 'application/json; charset=UTF-8', 'Accept': 'application/json' } api_headers_member = { "HTTP_API_KEY": self._member_key, 'content_type': 'application/json; charset=UTF-8', 'Accept': 'application/json' } api_headers_hr = { "HTTP_API_KEY": self._hr_key, 'content_type': 'application/json; charset=UTF-8', 'Accept': 'application/json' } # Create employee by admin data = copy.deepcopy(test_data.test_data_member_role) _email_id = data['work_email'] _employee_id = data['employee_id'] res, code = h.create_employee(api_headers_admin, data) self.assertEqual(code, 200) _id = res['result']["id"] # ***************** Case 1: employee update by member role with self.assertRaises(core_err.NotAuthorizedError): _data = copy.deepcopy(test_data.test_data_member_role) _update_data = { "id": _data['employee_id'], "first_name": "asdasdasd" } res, code = h.update_employee(api_headers_member, _update_data) # ***************** Case 2: employee update by self - update by employee id api_key, _key_self = h.get_api_key(email=_email_id) api_headers_self = { "HTTP_API_KEY": _key_self, 'content_type': 'application/json; charset=UTF-8', 'Accept': 'application/json' } _update_data = {"id": _employee_id, "first_name": "test_update_name"} res, code = h.update_employee(api_headers_self, _update_data) self.assertEqual(code, 200) self.assertEqual(res['result']['first_name'], _update_data['first_name']) # ****************** Case 3: employee update by self - update by id _update_data = {"id": _id, "last_name": "test_update_name"} res, code = h.update_employee(api_headers_self, _update_data) self.assertEqual(code, 200) self.assertEqual(res['result']['last_name'], _update_data['last_name']) # ****************** Case 4: employee update by admin _update_data = {"joining_date": '2010-07-09', "id": _employee_id} res, code = h.update_employee(api_headers_admin, _update_data) self.assertEqual(code, 200) self.assertEqual(res['result']['joining_date'], _update_data['joining_date']) # ****************** Case 5: employee update by hr _update_data = {"nationality_code": 'es', "id": _employee_id} res, code = h.update_employee(api_headers_hr, _update_data) self.assertEqual(res['result']['nationality_code'], _update_data['nationality_code']) self.assertEqual(code, 200)
def test_employee_show(self): """ Test employee show. Data is different for different user types :return: """ # Create an employee member api_headers_admin = { "HTTP_API_KEY": self._admin_key, 'content_type': 'application/json; charset=UTF-8', 'Accept': 'application/json' } api_headers_hr = { "HTTP_API_KEY": self._hr_key, 'content_type': 'application/json; charset=UTF-8', 'Accept': 'application/json' } api_headers_member = { "HTTP_API_KEY": self._member_key, 'content_type': 'application/json; charset=UTF-8', 'Accept': 'application/json' } employee_id = test_data.test_data_member_role['employee_id'] res, code = h.create_employee(api_headers_admin, test_data.test_data_member_role) self.assertEqual(code, 200) # ********* Employee view by admin -> others profile res, code = h.show_employee(api_headers_admin, {"id": employee_id}) self.assertEqual(code, 200) expected_keys = h.get_schema_keys_given_role("show", "admin") result = h.delete_keys_from_employee_show(res['result'], del_keys=('id', 'created_at', 'updated_at', 'avatar')) actual_keys = set([x for x in result]) self.assertFalse( expected_keys.difference(actual_keys) or actual_keys.difference(expected_keys)) # ********* Employee view by hr -> others profile res, code = h.show_employee(api_headers_hr, {"id": employee_id}) self.assertEqual(code, 200) expected_keys = h.get_schema_keys_given_role("show", "hr") result = h.delete_keys_from_employee_show(res['result'], del_keys=('id', 'created_at', 'updated_at', 'avatar')) actual_keys = set([x for x in result]) self.assertFalse( expected_keys.difference(actual_keys) or actual_keys.difference(expected_keys)) # ********* Employee view by member -> others profile res, code = h.show_employee(api_headers_member, {"id": employee_id}) self.assertEqual(code, 200) expected_keys = h.get_schema_keys_given_role("show", "all") result = h.delete_keys_from_employee_show(res['result'], del_keys=('id', 'created_at', 'updated_at', 'avatar')) actual_keys = set([x for x in result]) self.assertFalse( expected_keys.difference(actual_keys) or actual_keys.difference(expected_keys)) # ********* Employee view by member -> his/her own profile res, code = h.show_employee(api_headers_member, {"id": employee_id}) self.assertEqual(code, 200) _email = res['result']['work_email'] api_key, _key = h.get_api_key(email=_email) api_headers_self = { "HTTP_API_KEY": _key, 'content_type': 'application/json; charset=UTF-8', 'Accept': 'application/json' } res, code = h.show_employee(api_headers_self, {"id": employee_id}) self.assertEqual(code, 200) expected_keys = h.get_schema_keys_given_role("show", "member") result = h.delete_keys_from_employee_show(res['result'], del_keys=('id', 'created_at', 'updated_at', 'avatar')) actual_keys = set([x for x in result]) self.assertFalse( expected_keys.difference(actual_keys) or actual_keys.difference(expected_keys))