def setUp(self): self.config = testing.setUp() setup_routes(self.config) self.session = setup_database() self.paiduser = User(username='******', password='******') self.paiduser.add_paid_time(datetime.timedelta(days=30)) self.session.add(self.paiduser) self.session.flush() self.profile = Profile(uid=self.paiduser.id, name='testprofile') self.session.add(self.profile) self.freeuser = User(username='******', password='******') self.session.add(self.freeuser) duser = User(username='******', password='******') duser.is_active = False duser.add_paid_time(datetime.timedelta(days=30)) self.session.add(duser) self.gw0 = Gateway(name='gw0', token='simple_gateway', isp_name='', isp_url='', country='') self.session.add(self.gw0) self.gw1 = Gateway(name='gw1', token='disabled_gateway', isp_name='', isp_url='', country='') self.gw1.enabled = False self.session.add(self.gw1) self.gw2 = Gateway(name='gw2', token='ipv4_gateway', isp_name='', isp_url='', country='', ipv4='1.2.3.4') self.session.add(self.gw2) self.gw3 = Gateway(name='gw3', token='ipv6_gateway', isp_name='', isp_url='', country='', ipv6='1:2:3:4:5:6:7:8') self.session.add(self.gw3) self.session.flush() self.testheaders = { 'X-Gateway-Token': 'simple_gateway', 'X-Gateway-Version': 'alpha', }
def test_paid(self): user = User() self.assertFalse(user.is_paid) self.assertEqual(user.paid_days_left(), 0) user.add_paid_time(datetime.timedelta(days=1)) self.assertTrue(user.is_paid) self.assertEqual(user.paid_days_left(), 1) user.paid_until = datetime.datetime.fromtimestamp(1) user.add_paid_time(datetime.timedelta(days=1)) self.assertTrue(user.is_paid) self.assertEqual(user.paid_days_left(), 1)
def test_future(self): days = 7 u_exp5 = User(username='******', password='******', email='.') u_exp5.add_paid_time(timedelta(days=5)) self.session.add(u_exp5) u_exp10 = User(username='******', password='******', email='.') u_exp10.add_paid_time(timedelta(days=10)) self.session.add(u_exp10) # Same, with last_expiry_notice u_exp5l = User(username='******', password='******', email='.') u_exp5l.last_expiry_notice = datetime.now() - timedelta(days=1) u_exp5l.add_paid_time(timedelta(days=5)) self.session.add(u_exp5l) u_exp5ll = User(username='******', password='******', email='.') u_exp5ll.last_expiry_notice = datetime.now() - timedelta(days=30) u_exp5ll.add_paid_time(timedelta(days=5)) self.session.add(u_exp5ll) u_exp5 = self.session.query(User).filter_by(username='******').one() u_exp10 = self.session.query(User).filter_by(username='******').one() u_exp5l = self.session.query(User).filter_by(username='******').one() u_exp5ll = self.session.query(User).filter_by(username='******').one() users = expire_mail.get_future_expire(days) self.assertIn(u_exp5, users) self.assertNotIn(u_exp10, users) self.assertNotIn(u_exp5l, users) self.assertIn(u_exp5ll, users)
class TestGiftCodeModel(unittest.TestCase): def setUp(self): self.config = testing.setUp() self.session = setup_database() self.u = User(username='******', password='******') DBSession.add(self.u) self.session.flush() self.pu = User(username='******', password='******') self.pu.add_paid_time(datetime.timedelta(days=30)) DBSession.add(self.pu) self.session.flush() def tearDown(self): testing.tearDown() self.session.remove() def test_username_if_used(self): gc = GiftCode() self.assertIs(gc.username_if_used, False) gc.used = self.u.id gc.user = self.u self.assertEqual(gc.username_if_used, self.u.username) def test_use_freeonly(self): gc = GiftCode() gc.free_only = True self.assertRaises(models.AlreadyUsedGiftCode, gc.use, self.pu) gc.use(self.u) self.assertTrue(self.u.is_paid) def test_use_reuse(self): time = datetime.timedelta(days=30, hours=11) gc = GiftCode(time=time) gc.use(self.u) self.assertEqual(self.u.paid_time_left().days, time.days) self.assertRaises(models.AlreadyUsedGiftCode, gc.use, self.u) self.assertEqual(self.u.paid_time_left().days, time.days) gc.use(self.u, reuse=True) self.assertTrue(self.u.is_paid) self.assertEqual(self.u.paid_time_left().days, time.days*2)
class TestGiftCodeModel(unittest.TestCase): def setUp(self): self.config = testing.setUp() self.session = setup_database() self.u = User(username='******', password='******') DBSession.add(self.u) self.session.flush() self.pu = User(username='******', password='******') self.pu.add_paid_time(datetime.timedelta(days=30)) DBSession.add(self.pu) self.session.flush() def tearDown(self): testing.tearDown() self.session.remove() def test_username_if_used(self): gc = GiftCode() self.assertIs(gc.username_if_used, False) gc.used = self.u.id gc.user = self.u self.assertEqual(gc.username_if_used, self.u.username) def test_use_freeonly(self): gc = GiftCode() gc.free_only = True self.assertRaises(models.AlreadyUsedGiftCode, gc.use, self.pu) gc.use(self.u) self.assertTrue(self.u.is_paid) def test_use_reuse(self): time = datetime.timedelta(days=30, hours=11) gc = GiftCode(time=time) gc.use(self.u) self.assertEqual(self.u.paid_time_left().days, time.days) self.assertRaises(models.AlreadyUsedGiftCode, gc.use, self.u) self.assertEqual(self.u.paid_time_left().days, time.days) gc.use(self.u, reuse=True) self.assertTrue(self.u.is_paid) self.assertEqual(self.u.paid_time_left().days, time.days * 2)
def setUp(self): self.config = testing.setUp() setup_routes(self.config) self.session = setup_database() with transaction.manager: user = User(username='******', password='******') user.add_paid_time(datetime.timedelta(days=30)) baduser = User(username='******', password='******') self.session.add(user) self.session.add(baduser) with transaction.manager: token = APIAccessToken(token='apitoken') self.session.add(token) restricted_token = APIAccessToken(token='restricted_apitoken') restricted_token.remote_addr = '127.0.0.1' self.session.add(restricted_token) with transaction.manager: profile = Profile(uid=user.id, name='testprofile') self.session.add(profile)
class TestAPIViews(BaseTest): def setUp(self): super().setUp() self.paiduser = User(username='******', password='******') self.paiduser.add_paid_time(datetime.timedelta(days=30)) self.session.add(self.paiduser) self.session.flush() self.profile = Profile(uid=self.paiduser.id, name='testprofile') self.session.add(self.profile) self.freeuser = User(username='******', password='******') self.session.add(self.freeuser) duser = User(username='******', password='******') duser.is_active = False duser.add_paid_time(datetime.timedelta(days=30)) self.session.add(duser) self.gw0 = Gateway(name='gw0', token='simple_gateway', isp_name='', isp_url='', country='') self.session.add(self.gw0) self.gw1 = Gateway(name='gw1', token='disabled_gateway', isp_name='', isp_url='', country='') self.gw1.enabled = False self.session.add(self.gw1) self.gw2 = Gateway(name='gw2', token='ipv4_gateway', isp_name='', isp_url='', country='', ipv4='1.2.3.4') self.session.add(self.gw2) self.gw3 = Gateway(name='gw3', token='ipv6_gateway', isp_name='', isp_url='', country='', ipv6='1:2:3:4:5:6:7:8') self.session.add(self.gw3) self.session.flush() self.testheaders = { 'X-Gateway-Token': 'simple_gateway', 'X-Gateway-Version': 'alpha', } def assertSessionExists(self, **kwargs): sess = self.session.query(VPNSession) sess = sess.filter_by(**kwargs) sess = sess.all() self.assertGreaterEqual(len(sess), 1, msg='No session found for ' + str(kwargs)) def assertNSessionExists(self, n, **kwargs): sess = self.session.query(VPNSession) sess = sess.filter_by(**kwargs) sess = sess.all() msg = '%d != %d sessions found for %s' % (len(sess), n, str(kwargs)) self.assertEqual(len(sess), n, msg=msg) def test_api_auth(self): fn = views.api.require_api_token(None)(lambda req: True) req = DummyRequest(headers={ 'X-Gateway-Token': 'simple_gateway', 'X-Gateway-Version': 'alpha', }) self.assertEqual(fn(req), True) # Invalid or missing headers req = DummyRequest( headers={ 'X-Gateway-Token': 'simple_gateway', 'X-Gateway-Version': 'something_else', }) self.assertIsInstance(fn(req), httpexceptions.HTTPBadRequest) req = DummyRequest(headers={ 'X-Gateway-Version': 'alpha', }) self.assertIsInstance(fn(req), httpexceptions.HTTPBadRequest) req = DummyRequest(headers={ 'X-Gateway-Token': 'simple_gateway', }) self.assertIsInstance(fn(req), httpexceptions.HTTPBadRequest) # Invalid header content req = DummyRequest(headers={ 'X-Gateway-Token': 'unknown_gateway', 'X-Gateway-Version': 'alpha', }) self.assertIsInstance(fn(req), httpexceptions.HTTPForbidden) req = DummyRequest(headers={ 'X-Gateway-Token': 'disabled_gateway', 'X-Gateway-Version': 'alpha', }) self.assertIsInstance(fn(req), httpexceptions.HTTPForbidden) # Invalid source address req = DummyRequest(headers={ 'X-Gateway-Token': 'ipv4_gateway', 'X-Gateway-Version': 'alpha', }, remote_addr='1.2.3.4') self.assertEqual(fn(req), True) req = DummyRequest(headers={ 'X-Gateway-Token': 'ipv4_gateway', 'X-Gateway-Version': 'alpha', }, remote_addr='4.3.2.1') self.assertIsInstance(fn(req), httpexceptions.HTTPForbidden) req = DummyRequest(headers={ 'X-Gateway-Token': 'ipv6_gateway', 'X-Gateway-Version': 'alpha', }, remote_addr='1:2:3:4:5:6:7:8') self.assertEqual(fn(req), True) req = DummyRequest(headers={ 'X-Gateway-Token': 'ipv6_gateway', 'X-Gateway-Version': 'alpha', }, remote_addr='8:7:6:5:4:3:2:1') self.assertIsInstance(fn(req), httpexceptions.HTTPForbidden) def test_auth(self): req = DummyRequest(headers=self.testheaders, post={ 'username': '******', 'password': '******', }) resp = views.api.api_gateway_auth(req) self.assertIsInstance(resp, httpexceptions.HTTPOk) self.assertEqual(resp.body, b'') req = DummyRequest(headers=self.testheaders, post={ 'username': '******', }) resp = views.api.api_gateway_auth(req) self.assertIsInstance(resp, httpexceptions.HTTPBadRequest) self.assertEqual(resp.body, b'') req = DummyRequest(headers=self.testheaders, post={ 'username': '******', 'password': '******', }) resp = views.api.api_gateway_auth(req) self.assertIsInstance(resp, httpexceptions.HTTPForbidden) self.assertEqual(resp.body, b'') req = DummyRequest(headers=self.testheaders, post={ 'username': '******', 'password': '******', }) resp = views.api.api_gateway_auth(req) self.assertIsInstance(resp, httpexceptions.HTTPForbidden) self.assertEqual(resp.body, b'') req = DummyRequest(headers=self.testheaders, post={ 'username': '******', 'password': '******', }) resp = views.api.api_gateway_auth(req) self.assertIsInstance(resp, httpexceptions.HTTPOk) self.assertEqual(resp.body, b'') req = DummyRequest(headers=self.testheaders, post={ 'username': '******', 'password': '******', }) resp = views.api.api_gateway_auth(req) self.assertIsInstance(resp, httpexceptions.HTTPForbidden) self.assertEqual(resp.body, b'') def test_connect(self): req = DummyRequest(headers=self.testheaders, post={ 'username': '******', 'remote_addr': '1.2.3.4', }) resp = views.api.api_gateway_connect(req) self.assertIsInstance(resp, httpexceptions.HTTPOk) self.assertSessionExists(user_id=self.paiduser.id, disconnect_date=None) req = DummyRequest(headers=self.testheaders, post={ 'username': '******', 'remote_addr': '1.2.3.4', }) resp = views.api.api_gateway_connect(req) self.assertIsInstance(resp, httpexceptions.HTTPOk) req = DummyRequest(headers=self.testheaders, post={ 'username': '******', 'remote_addr': '1.2.3.4', }) resp = views.api.api_gateway_connect(req) self.assertIsInstance(resp, httpexceptions.HTTPForbidden) req = DummyRequest(headers=self.testheaders, post={ 'username': '******', }) resp = views.api.api_gateway_connect(req) self.assertIsInstance(resp, httpexceptions.HTTPBadRequest) self.assertEqual(resp.body, b'') req = DummyRequest(headers=self.testheaders, post={ 'remote_addr': '', }) resp = views.api.api_gateway_connect(req) self.assertIsInstance(resp, httpexceptions.HTTPBadRequest) self.assertEqual(resp.body, b'') req = DummyRequest(headers=self.testheaders, post={ 'username': '******', 'remote_addr': '', }) resp = views.api.api_gateway_connect(req) self.assertIsInstance(resp, httpexceptions.HTTPForbidden) self.assertEqual(resp.body, b'') req = DummyRequest(headers=self.testheaders, post={ 'username': '******', 'remote_addr': '', }) resp = views.api.api_gateway_connect(req) self.assertIsInstance(resp, httpexceptions.HTTPForbidden) self.assertEqual(resp.body, b'') def test_disconnect_one(self): req = DummyRequest(headers=self.testheaders, post={ 'username': '******', 'remote_addr': '1.2.3.4', }) resp = views.api.api_gateway_connect(req) self.assertIsInstance(resp, httpexceptions.HTTPOk) self.assertNSessionExists(1, user_id=self.paiduser.id, disconnect_date=None) # Missing POST data req = DummyRequest(headers=self.testheaders, post={ 'username': '******', }) resp = views.api.api_gateway_disconnect(req) self.assertIsInstance(resp, httpexceptions.HTTPBadRequest) self.assertNSessionExists(1, user_id=self.paiduser.id, disconnect_date=None) req = DummyRequest(headers=self.testheaders, post={ 'username': '******', 'bytes_up': 1337, 'bytes_down': 42, }) resp = views.api.api_gateway_disconnect(req) self.assertIsInstance(resp, httpexceptions.HTTPOk) self.assertNSessionExists(0, user_id=self.paiduser.id, disconnect_date=None) def test_disconnect_multiple(self): req = DummyRequest(headers=self.testheaders, post={ 'username': '******', 'remote_addr': '1.2.3.4', }) resp = views.api.api_gateway_connect(req) resp = views.api.api_gateway_connect(req) self.assertIsInstance(resp, httpexceptions.HTTPOk) self.assertNSessionExists(2, user_id=self.paiduser.id, disconnect_date=None) req = DummyRequest(headers=self.testheaders, post={ 'username': '******', 'bytes_up': 0, 'bytes_down': 0, }) resp = views.api.api_gateway_disconnect(req) self.assertIsInstance(resp, httpexceptions.HTTPOk) self.assertNSessionExists(0, user_id=self.paiduser.id, disconnect_date=None)
class TestAPIViews(BaseTest): def setUp(self): super().setUp() self.paiduser = User(username='******', password='******') self.paiduser.add_paid_time(datetime.timedelta(days=30)) self.session.add(self.paiduser) self.session.flush() self.profile = Profile(uid=self.paiduser.id, name='testprofile') self.session.add(self.profile) self.freeuser = User(username='******', password='******') self.session.add(self.freeuser) duser = User(username='******', password='******') duser.is_active = False duser.add_paid_time(datetime.timedelta(days=30)) self.session.add(duser) self.gw0 = Gateway(name='gw0', token='simple_gateway', isp_name='', isp_url='', country='') self.session.add(self.gw0) self.gw1 = Gateway(name='gw1', token='disabled_gateway', isp_name='', isp_url='', country='') self.gw1.enabled = False self.session.add(self.gw1) self.gw2 = Gateway(name='gw2', token='ipv4_gateway', isp_name='', isp_url='', country='', ipv4='1.2.3.4') self.session.add(self.gw2) self.gw3 = Gateway(name='gw3', token='ipv6_gateway', isp_name='', isp_url='', country='', ipv6='1:2:3:4:5:6:7:8') self.session.add(self.gw3) self.session.flush() self.testheaders = { 'X-Gateway-Token': 'simple_gateway', 'X-Gateway-Version': 'alpha', } def assertSessionExists(self, **kwargs): sess = self.session.query(VPNSession) sess = sess.filter_by(**kwargs) sess = sess.all() self.assertGreaterEqual(len(sess), 1, msg='No session found for ' + str(kwargs)) def assertNSessionExists(self, n, **kwargs): sess = self.session.query(VPNSession) sess = sess.filter_by(**kwargs) sess = sess.all() msg = '%d != %d sessions found for %s' % (len(sess), n, str(kwargs)) self.assertEqual(len(sess), n, msg=msg) def test_api_auth(self): fn = views.api.require_api_token(None)(lambda req: True) req = DummyRequest(headers={ 'X-Gateway-Token': 'simple_gateway', 'X-Gateway-Version': 'alpha', }) self.assertEqual(fn(req), True) # Invalid or missing headers req = DummyRequest(headers={ 'X-Gateway-Token': 'simple_gateway', 'X-Gateway-Version': 'something_else', }) self.assertIsInstance(fn(req), httpexceptions.HTTPBadRequest) req = DummyRequest(headers={ 'X-Gateway-Version': 'alpha', }) self.assertIsInstance(fn(req), httpexceptions.HTTPBadRequest) req = DummyRequest(headers={ 'X-Gateway-Token': 'simple_gateway', }) self.assertIsInstance(fn(req), httpexceptions.HTTPBadRequest) # Invalid header content req = DummyRequest(headers={ 'X-Gateway-Token': 'unknown_gateway', 'X-Gateway-Version': 'alpha', }) self.assertIsInstance(fn(req), httpexceptions.HTTPForbidden) req = DummyRequest(headers={ 'X-Gateway-Token': 'disabled_gateway', 'X-Gateway-Version': 'alpha', }) self.assertIsInstance(fn(req), httpexceptions.HTTPForbidden) # Invalid source address req = DummyRequest(headers={ 'X-Gateway-Token': 'ipv4_gateway', 'X-Gateway-Version': 'alpha', }, remote_addr='1.2.3.4') self.assertEqual(fn(req), True) req = DummyRequest(headers={ 'X-Gateway-Token': 'ipv4_gateway', 'X-Gateway-Version': 'alpha', }, remote_addr='4.3.2.1') self.assertIsInstance(fn(req), httpexceptions.HTTPForbidden) req = DummyRequest(headers={ 'X-Gateway-Token': 'ipv6_gateway', 'X-Gateway-Version': 'alpha', }, remote_addr='1:2:3:4:5:6:7:8') self.assertEqual(fn(req), True) req = DummyRequest(headers={ 'X-Gateway-Token': 'ipv6_gateway', 'X-Gateway-Version': 'alpha', }, remote_addr='8:7:6:5:4:3:2:1') self.assertIsInstance(fn(req), httpexceptions.HTTPForbidden) def test_auth(self): req = DummyRequest(headers=self.testheaders, post={ 'username': '******', 'password': '******', }) resp = views.api.api_gateway_auth(req) self.assertIsInstance(resp, httpexceptions.HTTPOk) self.assertEqual(resp.body, b'') req = DummyRequest(headers=self.testheaders, post={ 'username': '******', }) resp = views.api.api_gateway_auth(req) self.assertIsInstance(resp, httpexceptions.HTTPBadRequest) self.assertEqual(resp.body, b'') req = DummyRequest(headers=self.testheaders, post={ 'username': '******', 'password': '******', }) resp = views.api.api_gateway_auth(req) self.assertIsInstance(resp, httpexceptions.HTTPForbidden) self.assertEqual(resp.body, b'') req = DummyRequest(headers=self.testheaders, post={ 'username': '******', 'password': '******', }) resp = views.api.api_gateway_auth(req) self.assertIsInstance(resp, httpexceptions.HTTPForbidden) self.assertEqual(resp.body, b'') req = DummyRequest(headers=self.testheaders, post={ 'username': '******', 'password': '******', }) resp = views.api.api_gateway_auth(req) self.assertIsInstance(resp, httpexceptions.HTTPOk) self.assertEqual(resp.body, b'') req = DummyRequest(headers=self.testheaders, post={ 'username': '******', 'password': '******', }) resp = views.api.api_gateway_auth(req) self.assertIsInstance(resp, httpexceptions.HTTPForbidden) self.assertEqual(resp.body, b'') def test_connect(self): req = DummyRequest(headers=self.testheaders, post={ 'username': '******', 'remote_addr': '1.2.3.4', }) resp = views.api.api_gateway_connect(req) self.assertIsInstance(resp, httpexceptions.HTTPOk) self.assertSessionExists(user_id=self.paiduser.id, disconnect_date=None) req = DummyRequest(headers=self.testheaders, post={ 'username': '******', 'remote_addr': '1.2.3.4', }) resp = views.api.api_gateway_connect(req) self.assertIsInstance(resp, httpexceptions.HTTPOk) req = DummyRequest(headers=self.testheaders, post={ 'username': '******', 'remote_addr': '1.2.3.4', }) resp = views.api.api_gateway_connect(req) self.assertIsInstance(resp, httpexceptions.HTTPForbidden) req = DummyRequest(headers=self.testheaders, post={ 'username': '******', }) resp = views.api.api_gateway_connect(req) self.assertIsInstance(resp, httpexceptions.HTTPBadRequest) self.assertEqual(resp.body, b'') req = DummyRequest(headers=self.testheaders, post={ 'remote_addr': '', }) resp = views.api.api_gateway_connect(req) self.assertIsInstance(resp, httpexceptions.HTTPBadRequest) self.assertEqual(resp.body, b'') req = DummyRequest(headers=self.testheaders, post={ 'username': '******', 'remote_addr': '', }) resp = views.api.api_gateway_connect(req) self.assertIsInstance(resp, httpexceptions.HTTPForbidden) self.assertEqual(resp.body, b'') req = DummyRequest(headers=self.testheaders, post={ 'username': '******', 'remote_addr': '', }) resp = views.api.api_gateway_connect(req) self.assertIsInstance(resp, httpexceptions.HTTPForbidden) self.assertEqual(resp.body, b'') def test_disconnect_one(self): req = DummyRequest(headers=self.testheaders, post={ 'username': '******', 'remote_addr': '1.2.3.4', }) resp = views.api.api_gateway_connect(req) self.assertIsInstance(resp, httpexceptions.HTTPOk) self.assertNSessionExists(1, user_id=self.paiduser.id, disconnect_date=None) # Missing POST data req = DummyRequest(headers=self.testheaders, post={ 'username': '******', }) resp = views.api.api_gateway_disconnect(req) self.assertIsInstance(resp, httpexceptions.HTTPBadRequest) self.assertNSessionExists(1, user_id=self.paiduser.id, disconnect_date=None) req = DummyRequest(headers=self.testheaders, post={ 'username': '******', 'bytes_up': 1337, 'bytes_down': 42, }) resp = views.api.api_gateway_disconnect(req) self.assertIsInstance(resp, httpexceptions.HTTPOk) self.assertNSessionExists(0, user_id=self.paiduser.id, disconnect_date=None) def test_disconnect_multiple(self): req = DummyRequest(headers=self.testheaders, post={ 'username': '******', 'remote_addr': '1.2.3.4', }) resp = views.api.api_gateway_connect(req) resp = views.api.api_gateway_connect(req) self.assertIsInstance(resp, httpexceptions.HTTPOk) self.assertNSessionExists(2, user_id=self.paiduser.id, disconnect_date=None) req = DummyRequest(headers=self.testheaders, post={ 'username': '******', 'bytes_up': 0, 'bytes_down': 0, }) resp = views.api.api_gateway_disconnect(req) self.assertIsInstance(resp, httpexceptions.HTTPOk) self.assertNSessionExists(0, user_id=self.paiduser.id, disconnect_date=None)