# -*- coding: utf-8 -*-
# Copyright (c) 2012 Geoff Wilson <gmwils@gmail.com>

import mock

from tornado.testing import AsyncHTTPTestCase
from cihui.data import account


class AccountDataTest(AsyncHTTPTestCase):
    def get_app(self):
        self.app = mock.Mock()
        return self.app

    def setUp(self):
        AsyncHTTPTestCase.setUp(self)
        self.db = mock.Mock()
        self.callback = mock.Mock()
        self.accountdata = account.AccountData('', self.db)


class AuthenticateAccountTest(AccountDataTest):
    def test_authenticate_basic_account(self):
        self.assertTrue(
            self.accountdata.authenticate_api_user('user', 'secret'))

    def test_no_authenticate_basic_account(self):
        self.assertFalse(
            self.accountdata.authenticate_api_user('user', 'badpassword'))


class GetAccountTest(AccountDataTest):
    def test_get_account_sql(self):
        self.accountdata.get_account('user@example.com', self.callback)
        self.db.execute.assert_called_once()
        self.assertEqual(self.accountdata.callbacks['0|user@example.com'], self.callback)

    # TODO(gmwils): improve testing for accounts

# -*- coding: utf-8 -*-
# Copyright (c) 2012 Geoff Wilson <gmwils@gmail.com>

import mock
import unittest

from tornado.testing import AsyncHTTPTestCase
from cihui.data import account


class DigestTest(unittest.TestCase):
    def test_identical_digest(self):
        d1 = account.build_password_digest('password', 'salt')
        d2 = account.build_password_digest('password', 'salt')

        self.assertEqual(d1, d2)

    def test_different_salt(self):
        d1 = account.build_password_digest('password', 'salt1')
        d2 = account.build_password_digest('password', 'salt2')

        self.assertNotEqual(d1, d2)

    def test_different_passwd(self):
        d1 = account.build_password_digest('password1', 'salt')
        d2 = account.build_password_digest('password2', 'salt')

        self.assertNotEqual(d1, d2)


class AccountDataTest(AsyncHTTPTestCase):
    def get_app(self):
        self.app = mock.Mock()
        return self.app

    def setUp(self):
        AsyncHTTPTestCase.setUp(self)
        self.db = mock.Mock()
        self.callback = mock.Mock()
        self.accountdata = account.AccountData('', self.db)


class AuthenticateAccountTest(AccountDataTest):
    def test_authenticate_basic_account(self):
        self.assertTrue(
            self.accountdata.authenticate_api_user('user', 'secret'))

    def test_no_authenticate_basic_account(self):
        self.assertFalse(
            self.accountdata.authenticate_api_user('user', 'badpassword'))


class GetAccountTest(AccountDataTest):
    def test_get_account_sql(self):
        self.accountdata.get_account('user@example.com', self.callback)
        self.db.execute.assert_called_once()
        self.assertEqual(self.accountdata.callbacks['0|user@example.com'], self.callback)

    # TODO(gmwils): improve testing for accounts

Add digest test cases