#!/usr/bin/env python
#-*- coding: utf-8-*-


import unittest
from app.models import User, Role, Permission, AnonymousUser
from app import create_app, db



class UserModelTestCase(unittest.TestCase):


	def setUp(self):
		self.app = create_app('testing')
		self.app_context = self.app.app_context()
		self.app_context.push()
		db.create_all()
		Role.insert_roles()

	def tearDown(self):
		db.session.remove()
		db.drop_all()
		self.app_context.pop()

	def test_password_setter(self):
		u = User(password='qqq')
		self.assertTrue(u.password_hash is not None)

	def test_no_password_getter(self):
		u = User(password='qqq')
		self.assertTrue(u.verify_password('qqq'))
		self.assertFalse(u.verify_password('aaa'))

	def test_password_salts_are_random(self):
		u = User(password='qqq')
		u2 = User(password='aaa')
		self.assertTrue(u.password_hash != u2.password_hash)

	def test_roles_and_permissions(self):
		u = User(email='esmlll@qq.com', password='cat')
		self.assertTrue(u.can(Permission.WRITE_ARTICLES))
		self.assertFalse(u.can(Permission.MODERATE_COMMENTS))

	def test_anonymous_user(self):
		u=AnonymousUser()
		self.assertFalse(u.can(Permission.FOLLOW))