def test_callback_success(settings, django_user_model): """ the callback method should authenticate successfully with an auth_code and nonce. We have to fake this because we can't hit okta with a fake auth code. """ settings.MIDDLEWARE = ("django.contrib.sessions.middleware.SessionMiddleware",) nonce = "123456" user = django_user_model.objects.create_user("testuser", "*****@*****.**") with patch( "okta_oauth2.backend.TokenValidator.tokens_from_auth_code", Mock(return_value=(user, None)), ): c = Client() c.cookies = SimpleCookie( {"okta-oauth-state": "cookie-state", "okta-oauth-nonce": nonce} ) response = c.get( reverse("okta_oauth2:callback"), {"code": "123456", "state": "cookie-state"} ) assert response.status_code == 302 assert response.url == "/"
def test_dislike_resource(self): """Disliking a resource""" c = Client() id = ResourcePage.objects.get(slug='test').id c.cookies = SimpleCookie({'ldmw_session': '1234'}) c.post('/like/', {'id': id, 'like': -1}, HTTP_REFERER='/') like = Likes.objects.get(user_hash="1234", resource_id=id) self.assertEqual(like.like_value, -1)
def test_theme_cookie(self): c = Client() t = random.choice(self.threadDB.objects.all()) style = random.choice(('makaba', 'umnochan', 'burichan')) highlight_style = random.choice(('native', 'tango', 'manni')) c.cookies = SimpleCookie({ 'style': style, 'highlight_style': highlight_style, }) response = c.get('/' + t.board.link + '/thread/' + str(t.id), **meta) self.assertTrue(response.content.decode().find('%s.css' % style)) self.assertTrue(response.content.decode().find('%s.css' % highlight_style))
def test_undoing_dislike(self): """Disliking a resource twice should undo dislike""" c = Client() id = ResourcePage.objects.get(slug='test').id c.cookies = SimpleCookie({'ldmw_session': '1234'}) c.post('/like/', {'id': id, 'like': -1}, HTTP_REFERER='/') c.post('/like/', {'id': id, 'like': -1}, HTTP_REFERER='/') self.assertRaises( Likes.DoesNotExist, Likes.objects.get, user_hash="1234", resource_id=id )
def test_get_worktypes_all_json(self): cl = Client() #======================================================================= # Good requests #======================================================================= cl.cookies = Cookie.SimpleCookie() cl.cookies["lang"]="en" response = cl.get('/rnr/get_worktypes_all_json', content_type='application/json', HTTP_X_REQUESTED_WITH='XMLHttpRequest') wt_objects = WorkTypeDescription.objects.filter(worktypelang__language_name='English').values('worktype__slug','worktypedesc') self.assertEqual(response.content, simplejson.dumps(list(wt_objects))) #======================================================================= # Bad request, post/json, should return Http400 #======================================================================= response = cl.post('/rnr/get_worktypes_all_json', content_type='application/json', HTTP_X_REQUESTED_WITH='XMLHttpRequest') self.assertEqual(response.status_code, 400) response = cl.get('/rnr/get_worktypes_all_json') self.assertEqual(response.status_code, 400)
def test_failed_authentication_redirects_to_login(client, settings, django_user_model): settings.MIDDLEWARE = ("django.contrib.sessions.middleware.SessionMiddleware",) nonce = "123456" # Creating a user to make sure there's actually one that *could* be returned. django_user_model.objects.create_user("testuser", "*****@*****.**") with patch("okta_oauth2.views.authenticate", Mock(return_value=None)): c = Client() c.cookies = SimpleCookie( {"okta-oauth-state": "cookie-state", "okta-oauth-nonce": nonce} ) response = c.get( reverse("okta_oauth2:callback"), {"code": "123456", "state": "cookie-state"} ) assert response.status_code == 302 assert response.url == reverse("okta_oauth2:login")
def client(self): client = Client() client.cookies = SimpleCookie() for k,v in self.test_app.cookies.items(): client.cookies[k] = v return client
def client(self): client = Client() client.cookies = SimpleCookie() for k, v in self.test_app.cookies.items(): client.cookies[k] = v return client
def graphql_query( query, op_name=None, input_data=None, variables=None, headers=None, client=None, graphql_url=None, login_token=None, ): """ Args: query (string) - GraphQL query to run op_name (string) - If the query is a mutation or named query, you must supply the op_name. For annon queries ("{ ... }"), should be None (default). input_data (dict) - If provided, the $input variable in GraphQL will be set to this value. If both ``input_data`` and ``variables``, are provided, the ``input`` field in the ``variables`` dict will be overwritten with this value. variables (dict) - If provided, the "variables" field in GraphQL will be set to this value. headers (dict) - If provided, the headers in POST request to GRAPHQL_URL will be set to this value. client (django.test.Client) - Test client. Defaults to django.test.Client. graphql_url (string) - URL to graphql endpoint. Defaults to "/graphql". login_token (string) - Sets a token cookie for auth. Returns: Response object from client """ if client is None: client = Client() if not graphql_url: graphql_url = DEFAULT_GRAPHQL_URL body = {"query": query} if op_name: body["operationName"] = op_name if variables: body["variables"] = variables if input_data: if variables in body: body["variables"]["input"] = input_data else: body["variables"] = {"input": input_data} # Pretty much the only difference in the function to add a token cookie if login_token: client.cookies = SimpleCookie({"token": login_token}) else: # Since the token persists, send an empty one if the query doesn't # explicitly provide login_token. client.cookies = SimpleCookie({"token": ""}) if headers: resp = client.post(graphql_url, json.dumps(body), content_type="application/json", **headers) else: resp = client.post(graphql_url, json.dumps(body), content_type="application/json") return resp
def client(self): client = Client() client.cookies = SimpleCookie(self.test_app.cookies) return client
def test_get_works_json(self): #======================================================================= # Good requests, select one work #======================================================================= cl = Client() cl.cookies = Cookie.SimpleCookie() cl.cookies["lang"]='en' data = {'work_filter_number':'1401216', 'work_filter_pending':'false', 'work_filter_upcoming':'false', 'work_filter_completed':'true', 'work_filter_from':'', 'work_filter_to':'' } response = cl.get('/rnr/get_works_json', data=data, content_type='application/json', HTTP_X_REQUESTED_WITH='XMLHttpRequest') result_list = simplejson.loads(response.content) #check if one work returned and slug = faeD1C2Cf3Dd self.assertEqual(1, len(result_list)) self.assertEqual('faeD1C2Cf3Dd', result_list[0]["slug"]) #add upcoming work and filter upcoming wt = WorkType.objects.get(id=1) wr = Region.objects.get(id=1) data = {'work_number':'test_upcoming', 'work_start_datetime':'12/12/2019 15:00',\ 'work_end_datetime':'12/12/2019 16:00', 'work_type':wt.slug, 'work_region':wr.slug} response = cl.post('/rnr/add_new_work', data=data, HTTP_X_REQUESTED_WITH='XMLHttpRequest') data = {'work_filter_number':'', 'work_filter_pending':'false', 'work_filter_upcoming':'true', 'work_filter_completed':'false', 'work_filter_from':'', 'work_filter_to':'' } response = cl.get('/rnr/get_works_json', data=data, content_type='application/json', HTTP_X_REQUESTED_WITH='XMLHttpRequest') result_list = simplejson.loads(response.content) self.assertEqual(1, len(result_list)) self.assertEqual('test_upcoming', result_list[0]["work_number"]) #add pending work and test filter data = {'work_number':'test_pending', 'work_start_datetime':'12/12/2014 15:00',\ 'work_end_datetime':'12/12/2015 16:00', 'work_type':wt.slug, 'work_region':wr.slug} response = cl.post('/rnr/add_new_work', data=data, HTTP_X_REQUESTED_WITH='XMLHttpRequest') data = {'work_filter_number':'', 'work_filter_pending':'true', 'work_filter_upcoming':'false', 'work_filter_completed':'false', 'work_filter_from':'', 'work_filter_to':'' } response = cl.get('/rnr/get_works_json', data=data, content_type='application/json', HTTP_X_REQUESTED_WITH='XMLHttpRequest') result_list = simplejson.loads(response.content) self.assertEqual(1, len(result_list)) self.assertEqual('test_pending', result_list[0]["work_number"]) #check with work_filter_from data = {'work_filter_number':'', 'work_filter_pending':'true', 'work_filter_upcoming':'true', 'work_filter_completed':'false', 'work_filter_from':'2015-12-12', 'work_filter_to':'' } response = cl.get('/rnr/get_works_json', data=data, content_type='application/json', HTTP_X_REQUESTED_WITH='XMLHttpRequest') result_list = simplejson.loads(response.content) self.assertEqual(1, len(result_list)) self.assertEqual('test_upcoming', result_list[0]["work_number"]) #check with both filter set data = {'work_filter_number':'', 'work_filter_pending':'true', 'work_filter_upcoming':'true', 'work_filter_completed':'false', 'work_filter_from':'2014-12-11', 'work_filter_to':'2015-12-13' } response = cl.get('/rnr/get_works_json', data=data, content_type='application/json', HTTP_X_REQUESTED_WITH='XMLHttpRequest') result_list = simplejson.loads(response.content) self.assertEqual(1, len(result_list)) self.assertEqual('test_pending', result_list[0]["work_number"]) #======================================================================= # Bad requests should return Http400 #======================================================================= data = {'work_filter_number':'', 'work_filter_upcoming':'true', 'work_filter_completed':'false', 'work_filter_from':'2014-12-11', 'work_filter_to':'2015-12-13' } response = cl.get('/rnr/get_works_json', data=data, content_type='application/json', HTTP_X_REQUESTED_WITH='XMLHttpRequest') self.assertEqual(response.status_code, 400) response = cl.post('/rnr/get_works_json', content_type='application/json', HTTP_X_REQUESTED_WITH='XMLHttpRequest') self.assertEqual(response.status_code, 400) response = cl.post('/rnr/get_works_json') self.assertEqual(response.status_code, 400) response = cl.get('/rnr/get_works_json') self.assertEqual(response.status_code, 400)