def test_get_context_data(self, get_mock): request = RequestFactory().get('/') request.META['HTTP_HOST'] = 'localhost:3333' view = Login() view.request = request response_mock = Mock(status_code=200) response_mock.json.return_value = { "name": "oauth", "data": { "authorizeUrl": "http://something.com/?redirect=__redirect_url__" } } get_mock.return_value = response_mock data = view.get_context_data() self.assertDictEqual(data["scheme_info"], { "name": "oauth", "data": { "authorizeUrl": "http://something.com/?redirect=__redirect_url__" }, }) self.assertEqual( data["authorize_url"], "http://something.com/?redirect=http://localhost:3333/auth/callback/" )
def test_should_send_request_post_to_tsuru_with_args_expected(self, post): data = {'username': '******', 'password': '******'} request = RequestFactory().post('/', data) expected_url = '%s/users/[email protected]/tokens' % settings.TSURU_HOST Login.as_view()(request) self.assertEqual(1, post.call_count) post.assert_called_with(expected_url, data='{"password": "******"}')
def test_should_set_username_in_the_session(self, post): post.return_value = Mock(status_code=200, text='{"token": "t"}') data = {'username': '******', 'password': '******'} request = RequestFactory().post('/', data) request.session = {} Login.as_view()(request) self.assertEqual(data["username"], request.session["username"])
def test_should_set_token_in_the_session(self, post): data = {'username': '******', 'password': '******'} request = RequestFactory().post('/', data) request.session = {} text = '{"token": "my beautiful token"}' post.return_value = Mock(status_code=200, text=text) Login.as_view()(request) self.assertEqual('type my beautiful token', request.session["tsuru_token"])
def test_should_set_is_admin_to_false_when_its_missing(self, post): response_mock = Mock(status_code=200) response_mock.json.return_value = {"token": "my beautiful token"} post.return_value = response_mock data = {'username': '******', 'password': '******'} request = RequestFactory().post('/', data) request.session = {} Login.as_view()(request) self.assertFalse(request.session["is_admin"])
def test_should_send_request_post_to_tsuru_with_args_expected( self, get_mock, post): data = {"username": "******", "password": "******"} request = RequestFactory().post("/", data) request.session = {"next_url": "/"} expected_url = "%s/users/[email protected]/tokens" % settings.TSURU_HOST Login.as_view()(request) self.assertEqual(1, post.call_count) post.assert_called_with(expected_url, data='{"password": "******"}')
def test_should_send_request_post_to_tsuru_with_args_expected(self, get_mock, post): data = {"username": "******", "password": "******"} request = RequestFactory().post("/", data) request.session = {"next_url": "/"} expected_url = "%s/users/[email protected]/tokens" % settings.TSURU_HOST Login.as_view()(request) self.assertEqual(1, post.call_count) post.assert_called_with(expected_url, data='{"password": "******"}')
def test_should_set_is_admin_in_the_session(self, post): response_mock = Mock(status_code=200) response_mock.json.return_value = {"token": "my beautiful token", "is_admin": True} post.return_value = response_mock data = {"username": "******", "password": "******"} request = RequestFactory().post("/", data) request.session = {} Login.as_view()(request) self.assertTrue(request.session["is_admin"])
def test_get_context_data(self, get_mock): view = Login() response_mock = Mock(status_code=200) response_mock.json.return_value = {"name": "oauth"} get_mock.return_value = response_mock data = view.get_context_data() self.assertDictEqual(data["scheme_info"], {"name": "oauth"})
def test_should_set_username_in_the_session(self, post): response_mock = Mock(status_code=200) response_mock.json.return_value = {"token": "my beautiful token", "is_admin": True} post.return_value = response_mock data = {'username': '******', 'password': '******'} request = RequestFactory().post('/', data) request.session = {} Login.as_view()(request) self.assertEqual(data["username"], request.session["username"])
def test_should_set_is_admin_in_the_session(self, post): response_mock = Mock(status_code=200) response_mock.json.return_value = { "token": "my beautiful token", "is_admin": True } post.return_value = response_mock data = {"username": "******", "password": "******"} request = RequestFactory().post("/", data) request.session = {} Login.as_view()(request) self.assertTrue(request.session["is_admin"])
def test_redirect_to_team_creation_settings_true(self, post): data = {'username': '******', 'password': '******'} request = RequestFactory().post('/intro', data) request.session = {} text = '{"token": "my beautiful token"}' post.return_value = Mock(status_code=200, text=text) response = Login.as_view()(request) self.assertIsInstance(response, HttpResponseRedirect) self.assertEqual('/intro', response['Location']) response = Login.as_view()(request) self.assertIsInstance(response, HttpResponseRedirect) self.assertEqual('/apps', response['Location']) Intro.objects.filter(email=data['username']).delete()
def test_scheme_info(self, get_mock): view = Login() expected_url = '{}/auth/scheme'.format(settings.TSURU_HOST) self.assertDictEqual(view.scheme_info(), {}) get_mock.assert_called_with(expected_url) response_mock = Mock(status_code=200) response_mock.json.return_value = {"name": "oauth"} get_mock.return_value = response_mock self.assertDictEqual(view.scheme_info(), {"name": "oauth"}) get_mock.assert_called_with(expected_url)
def test_login_get(self, get_mock): request = RequestFactory().get("/") request.session = {"next_url": "/"} response = Login.as_view()(request) self.assertIn("auth/login.html", response.template_name) form = response.context_data["form"] self.assertIsInstance(form, LoginForm)
def test_get_context_data_with_data_is_none(self, get_mock): request = RequestFactory().get("/") request.META["HTTP_HOST"] = "localhost:3333" request.session = {"next_url": "/"} view = Login() view.request = request response_mock = Mock(status_code=200) response_mock.json.return_value = {"name": "native", "data": None} get_mock.return_value = response_mock data = view.get_context_data() self.assertDictEqual(data["scheme_info"], { "name": "native", "data": None, })
def test_should_validate_data_from_post(self): data = {'username': '******', 'password': ''} request = RequestFactory().post('/', data) response = Login.as_view()(request) form = response.context_data['form'] self.assertIn('auth/login.html', response.template_name) self.assertIsInstance(form, LoginForm) self.assertEqual('invalid name', form.data['username'])
def test_should_validate_data_from_post(self, get_mock): data = {'username': '******', 'password': ''} request = RequestFactory().post('/', data) response = Login.as_view()(request) form = response.context_data['form'] self.assertIn('auth/login.html', response.template_name) self.assertIsInstance(form, LoginForm) self.assertEqual('invalid name', form.data['username'])
def test_should_validate_data_from_post(self, get_mock): data = {"username": "******", "password": ""} request = RequestFactory().post("/", data) request.session = {"next_url": "/"} response = Login.as_view()(request) form = response.context_data["form"] self.assertIn("auth/login.html", response.template_name) self.assertIsInstance(form, LoginForm) self.assertEqual("invalid name", form.data["username"])
def test_redirect_to_team_creation_when_login_is_successful(self, post): data = {'username': '******', 'password': '******'} request = RequestFactory().post('/', data) request.session = {} text = '{"token": "my beautiful token"}' post.return_value = Mock(status_code=200, text=text) response = Login.as_view()(request) self.assertIsInstance(response, HttpResponseRedirect) self.assertEqual('/apps', response['Location'])
def test_get_context_data_with_data_is_none(self, get_mock): request = RequestFactory().get('/') request.META['HTTP_HOST'] = 'localhost:3333' view = Login() view.request = request response_mock = Mock(status_code=200) response_mock.json.return_value = { "name": "native", "data": None } get_mock.return_value = response_mock data = view.get_context_data() self.assertDictEqual(data["scheme_info"], { "name": "native", "data": None, })
def test_redirect_to_apps_when_the_intro_alread_exist(self, post): data = {'username': '******', 'password': '******'} intro = Intro.objects.create(email=data['username']) request = RequestFactory().post('/', data) request.session = {} text = '{"token": "my beautiful token"}' post.return_value = Mock(status_code=200, text=text) response = Login.as_view()(request) self.assertIsInstance(response, HttpResponseRedirect) self.assertEqual('/apps', response['Location']) intro.delete()
def test_redirect_to_apps(self, post): response_mock = Mock(status_code=200) response_mock.json.return_value = {"token": "my beautiful token", "is_admin": True} post.return_value = response_mock data = {"username": "******", "password": "******"} request = RequestFactory().post("/", data) request.session = {} response = Login.as_view()(request) self.assertIsInstance(response, HttpResponseRedirect) self.assertEqual("/apps", response["Location"])
def test_redirect_to_apps(self, post): response_mock = Mock(status_code=200) response_mock.json.return_value = {"token": "my beautiful token", "is_admin": True} post.return_value = response_mock data = {'username': '******', 'password': '******'} request = RequestFactory().post('/', data) request.session = {} response = Login.as_view()(request) self.assertIsInstance(response, HttpResponseRedirect) self.assertEqual('/apps', response['Location'])
def test_redirect_to_team_creation_settings_false(self, post): response_mock = Mock(status_code=200) response_mock.json.return_value = {"token": "my beautiful token", "is_admin": True} post.return_value = response_mock data = {'username': '******', 'password': '******'} request = RequestFactory().post('/', data) request.session = {} response = Login.as_view()(request) self.assertIsInstance(response, HttpResponseRedirect) self.assertEqual('/apps', response['Location'])
def test_redirect_to_apps(self, post): response_mock = Mock(status_code=200) response_mock.json.return_value = { "token": "my beautiful token", "is_admin": True } post.return_value = response_mock data = {"username": "******", "password": "******"} request = RequestFactory().post("/", data) request.session = {} response = Login.as_view()(request) self.assertIsInstance(response, HttpResponseRedirect) self.assertEqual("/apps", response["Location"])
from django.conf.urls import patterns, include, url from django.contrib.auth.decorators import login_required from auth.views import Register, Login, Logout from django.contrib import admin admin.autodiscover() urlpatterns = patterns('', url(r'^register/$', login_required(Register.as_view()), name='register'), url(r'^login/$', Login.as_view(), name='login'), url(r'^logout/$', Logout.as_view(), name='logout'), )
from django.urls import path from auth.views import Login, Logout urlpatterns = [ path('login/', Login.as_view(), name="auth_login"), path('logout/', Logout.as_view(), name="auth_logout"), ]
def test_login_get(self): request = RequestFactory().get('/') response = Login.as_view()(request) self.assertIn('auth/login.html', response.template_name) form = response.context_data['form'] self.assertIsInstance(form, LoginForm)
from django.urls import path from auth.views import ListUsers, Login, GoogleSignIn from rest_framework_simplejwt import views as jwt_views urlpatterns = [ path('users', ListUsers.as_view()), path('auth/login', Login.as_view()), path('auth/google/signin', GoogleSignIn.as_view()), path('token', jwt_views.TokenObtainPairView.as_view(), name='token_obtain_pair'), path('token/refresh', jwt_views.TokenRefreshView.as_view(), name='token_refresh'), path('token/verify', jwt_views.TokenVerifyView.as_view(), name='token_verify'), ]
""" from django.conf.urls import url, include from django.contrib import admin from django.views.generic import RedirectView from article.views import ArticleDelete, ArticleList, ArticleDetail, ArticleEdit, ArticleUpdate from auth.views import Login, Logout from comment.views import CommentAdd,CommentDelete urlpatterns = [ url(r'^admin/', admin.site.urls), url(r'^$', RedirectView.as_view(url='article')), url(r'^article/$', ArticleList.as_view(), name='article_list'), url(r'^tag/(?P<tag_id>[0-9]+)/$', ArticleList.as_view()), url(r'^article/(?P<article_id>[0-9]+)/$', ArticleDetail.as_view()), url(r'^article/edit/(?P<article_id>[0-9]+)/$', ArticleEdit.as_view()), url(r'^article/update/$', ArticleUpdate.as_view(), name='article_edit'), url(r'^article/delete/$', ArticleDelete.as_view(), name='article_delete'), url(r'^article/add/$', ArticleEdit.as_view(), name='article_add'), url(r'^comment/add/$',CommentAdd.as_view(),name='comment_add'), url(r'^comment/delete/$',CommentDelete.as_view(),name='comment_delete'), url(r'^login/$', Login.as_view(), name='login'), url(r'^logout/$', Logout.as_view(), name='logout') ]
from django.urls import path from auth.views import Login, signup, Logout urlpatterns = [ path('login/', Login.as_view(), name='login'), path('logout/', Logout, name='logout'), path('signup/', signup, name='signup') ]
def test_login_get(self, get_mock): request = RequestFactory().get('/') response = Login.as_view()(request) self.assertIn('auth/login.html', response.template_name) form = response.context_data['form'] self.assertIsInstance(form, LoginForm)