def test_render_and_file(self): err_ini = utils.get_render('configs/config.ini', dict(config=dict(test=[]))) for line in err_ini.split('\n'): self.assertEqual(line[0], '#') self.assertTrue('# Invalid config.' in err_ini, err_ini) config_dict = dict(config=dict(main=dict(test_key="test_value"))) ini = utils.get_render('configs/config.ini', config_dict) with utils.tmp_file_context(ini, delete=False) as file: file_name = file.name file.write('\n') with open(file_name, 'r') as tmp_file: self.assertEqual(tmp_file.read(), test_config) try: self.assertFalse(utils.os.path.exists(file_name)) except AssertionError: # nocv utils.os.remove(file_name) try: with utils.tmp_file(ini) as file: file_name = file.name file.write('\n') with open(file_name, 'r') as tmp_file: self.assertEqual(tmp_file.read(), test_config) raise Exception('Normal') except AssertionError: # nocv raise except Exception: pass with utils.tmp_file_context() as file: with open(file.name, 'w') as output: with utils.redirect_stdany(output): print("Test") with open(file.name, 'r') as output: self.assertEqual(output.read(), "Test\n")
def test_api_user_update(self): User = self.get_model_class('django.contrib.auth.models.User') client = self._login() result = self.result( client.post, self.get_url('user'), 201, { "username": "******", "password": "******", "password2": "eadgbe", "is_active": True, "first_name": "user_f_name", "last_name": "user_l_name", "email": "*****@*****.**" }) id = str(result['id']) url = self.get_url('user', id) data = json.dumps({ 'last_name': 'some_new_last_name', 'email': "*****@*****.**" }) result = self.result(client.patch, url, 200, data, content_type="application/json") self.assertEqual(result['last_name'], "some_new_last_name") self.assertEqual(result['email'], "*****@*****.**") data = json.dumps({'email': 'some_new_error_mail'}) with redirect_stdany(): with self.settings(DEBUG=True): self.result(client.patch, url, 400, data, content_type="application/json") data = json.dumps({'last_name': 'some_new_last_name'}) self.result(client.patch, url, 200, data, content_type="application/json") self._logout(client) client = self.client_class() response = client.login(**{ 'username': "******", 'password': "******" }) self.assertTrue(response) admin = User.objects.get(username="******") self.result(client.patch, self.get_url('user', admin.id), 403, json.dumps(dict(last_name="some_last")), content_type="application/json") self._logout(client)