def test_render_details_view_with_incorrect_id(self): client = Client() response = client.get('/quests/999') self.assertEqual(response.status_code, 404)
def setUp(self): """ Create client to conduct unit tests. """ self.client = Client()
def test_blog_not_authenicated(self): client = Client() client.logout() url = reverse('post_new') response = self.client.get(url) self.assertEqual(response.status_code, 302)
def test_index(): client = Client() url = reverse('index') response = client.get(url) assert response.status_code == 200
def setUp(self): self.client = Client() self.user = User.objects.create_user('testuser', 'unknown@localhost', 'passwd') dsmr_consumption.services.compact_all()
def setUpClass(cls): super(AssertResponseTest, cls).setUpClass() cls.response = Client().get("/admin/login/")
def setUp(self): User.objects.create(email=self.email_pre_cadastrado) self.client = Client()
def setUp(self): self.posts = list(Post.objects.all()) self.client = Client() self.user = User.objects.create(username="******", is_superuser=True)
def test_stock_create_get(self): # stock_object = mixer.blend("stock.Stock") client = Client() response = client.get("/stock/create", follow=True) assert response.status_code == 200, "Status Code should be 200"
def setUp(self): User.objects.create_user('admin', '*****@*****.**', 'admin123456') self.c = Client()
def setUp(self): self.user = User.objects.create_user(username='******', password='******') self.post = Post.objects.create(author=self.user, **self.dummy_data) self.client = Client()
def setUp(self): self.client = Client() self.question_model = mommy.make('polls.Question') self.choice_models = mommy.make('polls.Choice', question=self.question_model, _quantity=3)
def test_rendering_update_form(self): test_quest = Quest(title="Quest2", body="Quest2 body") test_quest.save() client = Client() response = client.get('/quests/' + str(test_quest.id) + '/update') self.assertEqual(response.status_code, 200)
def test_deleting_quest_with_bad_id(self): client = Client() response = client.get('/quests/' + 'error' + '/delete') self.assertEqual(response.status_code, 404)
def test_editor_without_organization_get_queryset(self, admin_user, editor_user): client = Client() client.force_login(editor_user) response = client.get(reverse("admin:users_user_changelist")) assert response.content.count(b'field-email') == 1
def setUp(self): self.user, self.username, self.password = create_user() self.c = Client(HTTP_HOST=settings.RSR_DOMAIN) self.c.login(username=self.username, password=self.password) self.parent_project = Project.objects.create( title="Parent project", subtitle="Parent project (subtitle)") self.parent_project.publish() self.child_project1 = Project.objects.create( title="Child project 1", subtitle="Child project 1 (subtitle)") self.child_project1.publish() self.child_project2 = Project.objects.create( title="Child project 2", subtitle="Child project 2 (subtitle)") self.child_project2.publish() RelatedProject.objects.create( project=self.parent_project, related_project=self.child_project1, relation=RelatedProject.PROJECT_RELATION_CHILD) RelatedProject.objects.create( project=self.parent_project, related_project=self.child_project2, relation=RelatedProject.PROJECT_RELATION_CHILD) # Create results framework self.result = Result.objects.create(project=self.parent_project, title="Result #1", type="1") self.indicator1 = Indicator.objects.create(result=self.result, title="Indicator #1", measure="1") self.today = datetime.date.today() self.period1 = IndicatorPeriod.objects.create( indicator=self.indicator1, period_start=self.today, period_end=self.today + datetime.timedelta(days=1), target_value="100") self.period2 = IndicatorPeriod.objects.create( indicator=self.indicator1, period_start=self.today + datetime.timedelta(days=1), period_end=self.today + datetime.timedelta(days=2), target_value="200") self.indicator2 = Indicator.objects.create(result=self.result, title="Indicator #2", measure="1") # self.period3 = IndicatorPeriod.objects.create( # indicator=self.indicator2, # period_start=today + datetime.timedelta(days=3), # period_end=today + datetime.timedelta(days=4), target_value="300" # ) # self.period4 = IndicatorPeriod.objects.create( # indicator=self.indicator2, # period_start=today + datetime.timedelta(days=5), # period_end=today + datetime.timedelta(days=6), target_value="400" # ) # Import results framework into child self.import_status1, self.import_message1 = self.child_project1.import_results( ) self.import_status2, self.import_message2 = self.child_project2.import_results( )
def test_superuser_get_queryset(self, admin_user, editor_user): client = Client() client.force_login(admin_user) response = client.get(reverse("admin:users_user_changelist")) assert response.content.count(b'field-email') == 2
def test_login_view(self): c = Client() response = c.post('/login/',{'username':'******', 'password': '******'}) self.assertEqual(response.status_code, 200)
def setUp(self): super(CategoryDetailViewTestCase, self).setUp() self._c = Client()
def test_head_works(self): csrf_client = Client(enforce_csrf_checks=True) r = csrf_client.head(self.url) self.assertEqual(r.status_code, 200) self.assertEqual(Ping.objects.count(), 1)
def setUp(self): self.client = Client() self.test_user1 = User.objects.create_user(username=self.test_user_name, password=self.test_user_pass) self.login = self.client.login(username=self.test_user_name, password=self.test_user_pass)
def test_csrf_middleware_is_enabled(): csrf_client = Client(enforce_csrf_checks=True) checkout_url = reverse("checkout:index") response = csrf_client.post(checkout_url) assert response.status_code == 403
def setUp(self): self.client = Client()
def setUp(self): self.client = Client() self.factory = RequestFactory()
def setUp(self): # Every test needs a client. self.client = Client() # needed to setup django django.setup()
def setUp(self): self.client = Client() self.privileged_user = ProfileFactory.create().\ user_object grant_privilege(self.privileged_user, 'Ticketing - Admin') self.url = reverse(self.view_name, urlconf='ticketing.urls')
def setUp(self): self.client = Client(HTTP_X_REQUESTED_WITH='XMLHttpRequest') self.admin = User.objects.create_superuser('admin', 'admin@localhost', 'admin')
class ApiViewCorrectPermissionsMixin(object): """Mixins for user testing the views is logged if the user has the required permissions.""" csrf_client = Client(enforce_csrf_checks=True) def load_response_content(self, response): response_content = response.content if six.PY3: response_content = str(response_content, encoding='utf8') return json.loads(response_content) def assertJSONDataVideoNames(self, response, video_names, test_dependency_count=True): json_response = self.load_response_content(response) self.assertIn('data', json_response) self.assertTrue( all('attributes' in item for item in json_response['data'])) self.assertTrue( all('name' in item['attributes'] for item in json_response['data'])) self.assertTrue( all('url' in item['attributes'] for item in json_response['data'])) self.assertTrue( all('stream_url' in item['attributes'] for item in json_response['data'])) self.assertTrue( all('provider_name' in item['attributes'] for item in json_response['data'])) self.assertTrue( all('tags' in item['attributes'] for item in json_response['data'])) if test_dependency_count: self.assertTrue( all('dependency_count' in item['attributes'] for item in json_response['data'])) response_names = [ item['attributes']['name'] for item in json_response['data'] ] self.assertEqual(video_names, response_names) def assertJSONDataSurveyLink(self, response, survey_link): json_response = self.load_response_content(response) self.assertIn('data', json_response) self.assertTrue('attributes' in json_response['data']) link = json_response['data']['attributes'].get('link', None) self.assertEqual(link, survey_link) def watch_and_next_videos(self, video_name, next_videos): with NeoGraph() as graph: obj = Video.select(graph).where( '_.name = "{}"'.format(video_name)).first() response = self.client.post( reverse('api:watch_videos'), json.dumps({ 'data': { 'type': 'watches', 'attributes': { 'video_id': obj.id, 'date': datetime.now().isoformat(), 'rating': 1, 'progress': 1, } } }), content_type='application/vnd.api+json', ) self.assertEqual(response.status_code, 204) response = self.client.get(reverse('api:next_videos')) self.assertEqual(response.status_code, 200) self.assertJSONDataVideoNames(response, next_videos) def dismiss_and_next_videos(self, video_name, next_videos): with NeoGraph() as graph: obj = Video.select(graph).where( '_.name = "{}"'.format(video_name)).first() response = self.client.post( reverse('api:watch_videos'), json.dumps({ 'data': { 'type': 'watches', 'attributes': { 'video_id': obj.id, 'date': datetime.now().isoformat(), 'rating': -1, 'progress': 0, } } }), content_type='application/vnd.api+json', ) self.assertEqual(response.status_code, 204) response = self.client.get(reverse('api:next_videos')) self.assertEqual(response.status_code, 200) self.assertJSONDataVideoNames(response, next_videos) def watch_and_history(self, video_name, history_videos): with NeoGraph() as graph: obj = Video.select(graph).where( '_.name = "{}"'.format(video_name)).first() response = self.client.post( reverse('api:watch_videos'), json.dumps({ 'data': { 'type': 'watches', 'attributes': { 'video_id': obj.id, 'date': datetime.now().isoformat(), 'rating': 1, 'progress': 1, } } }), content_type='application/vnd.api+json', ) self.assertEqual(response.status_code, 204) response = self.client.get(reverse('api:history')) self.assertEqual(response.status_code, 200) self.assertJSONDataVideoNames(response, history_videos, test_dependency_count=False) def dismiss_and_history(self, video_name, history_videos): with NeoGraph() as graph: obj = Video.select(graph).where( '_.name = "{}"'.format(video_name)).first() response = self.client.post( reverse('api:watch_videos'), json.dumps({ 'data': { 'type': 'watches', 'attributes': { 'video_id': obj.id, 'date': datetime.now().isoformat(), 'rating': -1, 'progress': 0, } } }), content_type='application/vnd.api+json', ) self.assertEqual(response.status_code, 204) response = self.client.get(reverse('api:history')) self.assertEqual(response.status_code, 200) self.assertJSONDataVideoNames(response, history_videos) def watch_and_survey(self, video_name, survey_link): with NeoGraph() as graph: obj = Video.select(graph).where( '_.name = "{}"'.format(video_name)).first() response = self.client.post( reverse('api:watch_videos'), json.dumps({ 'data': { 'type': 'watches', 'attributes': { 'video_id': obj.id, 'date': datetime.now().isoformat(), 'rating': 1, 'progress': 1, } } }), content_type='application/vnd.api+json', ) self.assertEqual(response.status_code, 204) response = self.client.get(reverse('api:survey_latest')) self.assertEqual(response.status_code, 200) self.assertJSONDataSurveyLink(response, survey_link) def dismiss_and_survey(self, video_name, survey_link): with NeoGraph() as graph: obj = Video.select(graph).where( '_.name = "{}"'.format(video_name)).first() response = self.client.post( reverse('api:watch_videos'), json.dumps({ 'data': { 'type': 'watches', 'attributes': { 'video_id': obj.id, 'date': datetime.now().isoformat(), 'rating': -1, 'progress': 0, } } }), content_type='application/vnd.api+json', ) self.assertEqual(response.status_code, 204) response = self.client.get(reverse('api:survey_latest')) self.assertEqual(response.status_code, 200) self.assertJSONDataSurveyLink(response, survey_link) def complete_survey(self, survey_id): response = self.client.post( reverse('api:survey_complete', kwargs={'pk': survey_id})) self.assertEqual(response.status_code, 204) # GET /api/next/ def test_next_videos_view(self): response = self.client.get(reverse('api:next_videos')) self.assertEqual(response.status_code, 200) self.assertJSONDataVideoNames(response, ['C']) def test_next_videos_view_workflow(self): response = self.client.get(reverse('api:next_videos')) self.assertEqual(response.status_code, 200) self.assertJSONDataVideoNames(response, ['C']) self.watch_and_next_videos('C', ['A']) self.watch_and_next_videos('A', ['B']) self.watch_and_next_videos('B', []) def test_next_videos_view_workflow_dimiss(self): response = self.client.get(reverse('api:next_videos')) self.assertEqual(response.status_code, 200) self.assertJSONDataVideoNames(response, ['C']) self.dismiss_and_next_videos('C', ['A']) self.dismiss_and_next_videos('A', []) # GET /api/watch/ def test_get_watch_video(self): response = self.client.get(reverse('api:watch_videos')) self.assertEqual(response.status_code, 405) # POST /api/watch/ def test_post_watch_video(self): with NeoGraph() as graph: obj = Video.select(graph).where( '_.name = "{}"'.format('A')).first() response = self.client.post( reverse('api:watch_videos'), json.dumps({ 'data': { 'type': 'watches', 'attributes': { 'video_id': obj.id, 'date': datetime.now().isoformat(), 'rating': 1, 'progress': 1, } } }), content_type='application/vnd.api+json', ) self.assertEqual(response.status_code, 204, response.content) def test_post_watch_video_again(self): with NeoGraph() as graph: obj = Video.select(graph).where( '_.name = "{}"'.format('A')).first() response = self.client.post( reverse('api:watch_videos'), json.dumps({ 'data': { 'type': 'watches', 'attributes': { 'video_id': obj.id, 'date': datetime.now().isoformat(), 'rating': 1, 'progress': 1, } } }), content_type='application/vnd.api+json', ) self.assertEqual(response.status_code, 204, response.content) response = self.client.post( reverse('api:watch_videos'), json.dumps({ 'data': { 'type': 'watches', 'attributes': { 'video_id': obj.id, 'date': datetime.now().isoformat(), 'rating': 0.5, 'progress': 1, } } }), content_type='application/vnd.api+json', ) self.assertEqual(response.status_code, 204, response.content) def test_post_watch_video_missing_data(self): response = self.client.post( reverse('api:watch_videos'), content_type='application/vnd.api+json', ) self.assertEqual(response.status_code, 400) def test_post_watch_video_missing_date(self): with NeoGraph() as graph: obj = Video.select(graph).where( '_.name = "{}"'.format('A')).first() response = self.client.post( reverse('api:watch_videos'), json.dumps({ 'data': { 'type': 'watches', 'attributes': { 'video_id': obj.id, 'rating': 1, 'progress': 1, } } }), content_type='application/vnd.api+json', ) self.assertEqual(response.status_code, 400) def test_post_watch_video_missing_rating(self): with NeoGraph() as graph: obj = Video.select(graph).where( '_.name = "{}"'.format('A')).first() response = self.client.post( reverse('api:watch_videos'), json.dumps({ 'data': { 'type': 'watches', 'attributes': { 'video_id': obj.id, 'date': datetime.now().isoformat(), 'progress': 1, } } }), content_type='application/vnd.api+json', ) self.assertEqual(response.status_code, 400) def test_post_watch_video_missing_progress(self): with NeoGraph() as graph: obj = Video.select(graph).where( '_.name = "{}"'.format('A')).first() response = self.client.post( reverse('api:watch_videos'), json.dumps({ 'data': { 'type': 'watches', 'attributes': { 'video_id': obj.id, 'date': datetime.now().isoformat(), 'rating': 1, } } }), content_type='application/vnd.api+json', ) self.assertEqual(response.status_code, 400) def test_post_watch_video_wrong_id(self): response = self.client.post( reverse('api:watch_videos'), json.dumps({ 'data': { 'type': 'watches', 'attributes': { 'video_id': '1234567', } } }), content_type='application/vnd.api+json', ) self.assertEqual(response.status_code, 400) # GET /api/history def test_get_history(self): response = self.client.get(reverse('api:history')) self.assertEqual(response.status_code, 200) json_content = self.assertJSONDataVideoNames(response, []) def test_get_history_workflow(self): response = self.client.get(reverse('api:history')) self.assertEqual(response.status_code, 200) json_content = self.assertJSONDataVideoNames(response, []) self.watch_and_history('C', ['C']) self.watch_and_history('A', ['A', 'C']) self.watch_and_history('B', ['B', 'A', 'C']) def test_get_history_workflow_dismiss(self): response = self.client.get(reverse('api:history')) self.assertEqual(response.status_code, 200) json_content = self.assertJSONDataVideoNames(response, []) self.dismiss_and_history('C', []) self.dismiss_and_history('A', []) # GET /api/preferences/ def test_get_preferences(self): response = self.client.get(reverse('api:preferences')) self.assertEqual(response.status_code, 200) json = self.load_response_content(response) self.assertEqual(json.get('data'), [{ u'type': u'preferences', u'id': u'2', u'attributes': { u'name': u'aMusic', u'weight': 0.5, } }, { u'type': u'preferences', u'id': u'1', u'attributes': { u'name': u'Category', u'weight': 0.75, } }]) # PATCH /api/preferences/<pk>/ def test_patch_preferences(self): response = self.client.patch(reverse('api:preference_update', kwargs={'pk': 1}), json.dumps({ u'data': { u'type': 'preferences', u'id': u'1', u'attributes': { u'weight': 0.25, }, } }), content_type='application/vnd.api+json') self.assertEqual(response.status_code, 204, response.content) def test_patch_preferences_new(self): response = self.client.patch(reverse('api:preference_update', kwargs={'pk': 2}), json.dumps({ u'data': { u'type': 'preferences', u'id': u'2', u'attributes': { u'weight': 0.1, }, } }), content_type='application/vnd.api+json') self.assertEqual(response.status_code, 204, response.content) def test_patch_preferences_missing_id(self): response = self.client.patch(reverse('api:preference_update', kwargs={'pk': 1}), json.dumps({ u'data': { u'type': 'preferences', u'attributes': { u'weight': 0.25, }, } }), content_type='application/vnd.api+json') self.assertEqual(response.status_code, 400) def test_patch_preferences_missing_weight(self): response = self.client.patch(reverse('api:preference_update', kwargs={'pk': 1}), json.dumps({ u'data': { u'type': 'preferences', u'id': u'1', u'attributes': {}, } }), content_type='application/vnd.api+json') self.assertEqual(response.status_code, 400) json_response = self.load_response_content(response) self.assertEqual(json_response.get('errors'), {u'title': u'Invalid preference updates'}) def test_patch_preferences_id_mismatch(self): response = self.client.patch(reverse('api:preference_update', kwargs={'pk': 1}), json.dumps({ u'data': { u'type': 'preferences', u'id': u'0', u'attributes': { u'weight': 0.25, }, } }), content_type='application/vnd.api+json') self.assertEqual(response.status_code, 400) json_response = self.load_response_content(response) self.assertEqual(json_response.get('errors'), {u'title': u'Invalid preference updates'}) def test_patch_preferences_not_existing(self): response = self.client.patch(reverse('api:preference_update', kwargs={'pk': 0}), json.dumps({ u'data': { u'type': 'preferences', u'id': u'0', u'attributes': { u'weight': 0.25, }, } }), content_type='application/vnd.api+json') self.assertEqual(response.status_code, 400, response.content) json_response = self.load_response_content(response) self.assertEqual(json_response.get('errors'), { u'id': u'0', u'title': u'Found non-existing category id' }) # GET /api/surveys/latest def test_get_latest_survey(self): response = self.client.get(reverse('api:survey_latest')) self.assertEqual(response.status_code, 200) json = self.load_response_content(response) self.assertEqual( json, {'data': { 'type': 'surveys', 'id': None, 'attributes': {} }}) def test_get_survey_workflow(self): response = self.client.get(reverse('api:survey_latest')) self.assertEqual(response.status_code, 200) json_content = self.assertJSONDataSurveyLink(response, None) self.watch_and_survey('C', 'https://www.google.com/') self.watch_and_survey('A', 'https://www.github.com/') self.watch_and_survey('B', 'https://www.github.com/') def test_get_survey_workflow_complete_1(self): response = self.client.get(reverse('api:survey_latest')) self.assertEqual(response.status_code, 200) json_content = self.assertJSONDataSurveyLink(response, None) self.watch_and_survey('C', 'https://www.google.com/') self.complete_survey(1) self.watch_and_survey('A', 'https://www.facebook.com/') self.complete_survey(2) self.watch_and_survey('B', 'https://www.facebook.com/') def test_get_survey_workflow_complete_2(self): response = self.client.get(reverse('api:survey_latest')) self.assertEqual(response.status_code, 200) json_content = self.assertJSONDataSurveyLink(response, None) self.watch_and_survey('C', 'https://www.google.com/') self.watch_and_survey('A', 'https://www.github.com/') self.complete_survey(2) self.watch_and_survey('B', 'https://www.github.com/') def test_get_survey_workflow_dismiss_1(self): response = self.client.get(reverse('api:survey_latest')) self.assertEqual(response.status_code, 200) json_content = self.assertJSONDataSurveyLink(response, None) self.dismiss_and_survey('C', None) self.watch_and_survey('A', 'https://www.google.com/') self.watch_and_survey('B', 'https://www.github.com/') def test_get_survey_workflow_dismiss_2(self): response = self.client.get(reverse('api:survey_latest')) self.assertEqual(response.status_code, 200) json_content = self.assertJSONDataSurveyLink(response, None) self.watch_and_survey('C', 'https://www.google.com/') self.dismiss_and_survey('A', 'https://www.google.com/') self.watch_and_survey('B', 'https://www.github.com/') def test_get_survey_workflow_dismiss_3(self): response = self.client.get(reverse('api:survey_latest')) self.assertEqual(response.status_code, 200) json_content = self.assertJSONDataSurveyLink(response, None) self.watch_and_survey('C', 'https://www.google.com/') self.dismiss_and_survey('A', 'https://www.google.com/') self.dismiss_and_survey('B', 'https://www.google.com/') # PATCH /api/surveys/<pk>/complete def test_post_complete_survey(self): response = self.client.post( reverse('api:survey_complete', kwargs={'pk': 1})) self.assertEqual(response.status_code, 204)
import json from django.contrib.auth.models import User from django.test import TestCase, Client from django.urls import reverse from rest_framework import status from social_network_api.models import Post client = Client() def get_access_token(user_data): response = client.post(reverse('token_obtain_pair'), data=user_data, content_type='application/json') access_token = response.data['access'] if response.status_code == status.HTTP_200_OK: return access_token else: return None class SignInTest(TestCase): def setUp(self): self.valid_request = { 'user': {'username': '******', 'password': '******', 'password_confirm': '1111', 'first_name': 'Alexandr', 'last_name': 'Ivanov',
def test_render_list_view_without_any_quests(self): client = Client() response = client.get('/quests/') self.assertEqual(response.status_code, 200)