Advertisement
Guest User

Untitled

a guest
Jul 29th, 2017
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.56 KB | None | 0 0
  1. import unittest
  2. from pyramid import testing
  3. from paste.deploy.loadwsgi import appconfig
  4.  
  5. from webtest import TestApp
  6. from mock import Mock
  7.  
  8. from sqlalchemy import engine_from_config
  9. from sqlalchemy.orm import sessionmaker
  10. from app.db import Session
  11. from app.db import Entity # base declarative object
  12. from app import main
  13. import os
  14. here = os.path.dirname(__file__)
  15. settings = appconfig('config:' + os.path.join(here, '../../', 'test.ini'))
  16.  
  17. class BaseTestCase(unittest.TestCase):
  18. @classmethod
  19. def setUpClass(cls):
  20. cls.engine = engine_from_config(settings, prefix='sqlalchemy.')
  21. cls.Session = sessionmaker()
  22.  
  23. def setUp(self):
  24. connection = self.engine.connect()
  25.  
  26. # begin a non-ORM transaction
  27. self.trans = connection.begin()
  28.  
  29. # bind an individual Session to the connection
  30. Session.configure(bind=connection)
  31. self.session = self.Session(bind=connection)
  32. Entity.session = self.session
  33.  
  34. class UnitTestBase(BaseTestCase):
  35. def setUp(self):
  36. self.config = testing.setUp(request=testing.DummyRequest())
  37. super(UnitTestBase, self).setUp()
  38.  
  39. def get_csrf_request(self, post=None):
  40. csrf = 'abc'
  41.  
  42. if not u'csrf_token' in post.keys():
  43. post.update({
  44. 'csrf_token': csrf
  45. })
  46.  
  47. request = testing.DummyRequest(post)
  48.  
  49. request.session = Mock()
  50. csrf_token = Mock()
  51. csrf_token.return_value = csrf
  52.  
  53. request.session.get_csrf_token = csrf_token
  54.  
  55. return request
  56.  
  57.  
  58.  
  59. class TestViews(UnitTestBase):
  60. def test_login_fails_empty(self):
  61. """ Make sure we can't login with empty credentials"""
  62. from app.accounts.views import LoginView
  63. self.config.add_route('index', '/')
  64. self.config.add_route('dashboard', '/')
  65.  
  66. request = testing.DummyRequest(post={
  67. 'submit': True,
  68. })
  69.  
  70. view = LoginView(request)
  71. response = view.post()
  72. errors = response['errors']
  73.  
  74. assert errors[0].node.name == u'csrf_token'
  75. assert errors[0].msg == u'Required'
  76. assert errors[1].node.name == u'Username'
  77. assert errors[1].msg == u'Required'
  78. assert errors[2].node.name == u'Password'
  79. assert errors[2].msg == u'Required'
  80.  
  81.  
  82. def test_login_succeeds(self):
  83. """ Make sure we can login """
  84. admin = User(username='sontek', password='temp', kind=u'admin')
  85. admin.activated = True
  86. self.session.add(admin)
  87. self.session.flush()
  88.  
  89. from app.accounts.views import LoginView
  90. self.config.add_route('index', '/')
  91. self.config.add_route('dashboard', '/dashboard')
  92.  
  93. request = self.get_csrf_request(post={
  94. 'submit': True,
  95. 'Username': 'sontek',
  96. 'Password': 'temp',
  97. })
  98.  
  99. view = LoginView(request)
  100. response = view.post()
  101.  
  102. assert response.status_int == 302
  103.  
  104.  
  105. class IntegrationTestBase(BaseTestCase):
  106. @classmethod
  107. def setUpClass(cls):
  108. cls.app = main({}, **settings)
  109. super(IntegrationTestBase, cls).setUpClass()
  110.  
  111. def setUp(self):
  112. self.app = TestApp(self.app)
  113. self.config = testing.setUp()
  114. super(IntegrationTestBase, self).setUp()
  115.  
  116.  
  117.  
  118. class TestViews(IntegrationTestBase):
  119. def test_get_login(self):
  120. """ Call the login view, make sure routes are working """
  121. res = self.app.get('/login')
  122. self.assertEqual(res.status_int, 200)
  123.  
  124. def test_empty_login(self):
  125. """ Empty login fails """
  126. res = self.app.post('/login', {'submit': True})
  127.  
  128. assert "There was a problem with your submission" in res.body
  129. assert "Required" in res.body
  130. assert res.status_int == 200
  131.  
  132. def test_valid_login(self):
  133. """ Call the login view, make sure routes are working """
  134. admin = User(username='sontek', password='temp', kind=u'admin')
  135. admin.activated = True
  136. self.session.add(admin)
  137. self.session.flush()
  138.  
  139. res = self.app.get('/login')
  140.  
  141. csrf = res.form.fields['csrf_token'][0].value
  142.  
  143. res = self.app.post('/login',
  144. {
  145. 'submit': True,
  146. 'Username': 'sontek',
  147. 'Password': 'temp',
  148. 'csrf_token': csrf
  149. }
  150. )
  151.  
  152. assert res.status_int == 302
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement