from django.test import TransactionTestCase
from restclients_core.exceptions import DataFailureException
from uw_bridge.models import BridgeUser
from sis_provisioner.models import UwAccount, get_now
from sis_provisioner.dao import DataFailureException
from sis_provisioner.dao.bridge import BridgeUsers
from sis_provisioner.tests import fdao_bridge_override
from sis_provisioner.tests.dao import (
    get_regid, get_mock_bridge_user, new_regid_custom_field)


@fdao_bridge_override
class TestBridge(TransactionTestCase):

    @classmethod
    def setUpClass(cls):
        TestBridge.bridge = BridgeUsers()

    def test_get_all_bridge_users(self):
        busers = TestBridge.bridge.get_all_bridge_users()
        self.assertEqual(len(busers), 6)
        buser = busers[0]
        self.assertEqual(buser.bridge_id, 194)
        self.assertEqual(buser.netid, 'ellen')
        buser = busers[1]
        self.assertEqual(buser.bridge_id, 195)
        self.assertEqual(buser.netid, 'javerage')
        buser = busers[2]
        self.assertEqual(buser.bridge_id, 198)
        self.assertEqual(buser.netid, 'tyler')
        buser = busers[3]
        self.assertEqual(buser.bridge_id, 199)
        self.assertEqual(buser.netid, 'alumni')
        buser = busers[4]
        self.assertEqual(buser.bridge_id, 200)
        self.assertEqual(buser.netid, 'leftuw')
        buser = busers[5]
        self.assertEqual(buser.bridge_id, 204)
        self.assertEqual(buser.netid, 'retiree')

    def test_delete_bridge_user(self):
        # normal case
        user = get_mock_bridge_user(0,
                                    'leftuw',
                                    "leftuw@uw.edu",
                                    "Who LEFT",
                                    "WHO",
                                    "LEFT",
                                    "10000000000000000000000000000FFF")
        self.assertRaises(DataFailureException,
                          TestBridge.bridge.delete_bridge_user,
                          user)
        user.bridge_id = 200
        self.assertTrue(TestBridge.bridge.delete_bridge_user(user))

    def test_change_uwnetid(self):
        buser = TestBridge.bridge.get_user_by_bridgeid(198)
        self.assertEqual(buser.netid, 'tyler')

        uw_acc = UwAccount(netid='faculty',
                           bridge_id=198,
                           prev_netid='tyler',
                           last_updated=get_now())
        bridge_user = TestBridge.bridge.change_uwnetid(uw_acc)
        self.assertEqual(bridge_user.netid, 'faculty')

    def test_get_bridge_user(self):
        self.assertRaises(DataFailureException,
                          TestBridge.bridge.get_user_by_uwnetid,
                          "error500")

        self.assertIsNone(TestBridge.bridge.get_user_by_uwnetid("none"))

        # terminated
        self.assertIsNone(TestBridge.bridge.get_user_by_uwnetid("staff"))

        buser = TestBridge.bridge.get_user_by_uwnetid("javerage")
        self.assertIsNotNone(buser)
        self.assertEqual(buser.bridge_id, 195)
        self.assertEqual(buser.netid, "javerage")
        self.assertEqual(len(buser.custom_fields), 1)
        self.assertEqual(get_regid(buser),
                         "9136CCB8F66711D5BE060004AC494FFE")

        buser = TestBridge.bridge.get_user_by_bridgeid(204)
        self.assertIsNotNone(buser)
        self.assertEqual(buser.bridge_id, 204)
        self.assertEqual(buser.netid, 'retiree')
        self.assertEqual(buser.email, 'retiree@uw.edu')
        self.assertEqual(get_regid(buser),
                         "10000000000000000000000000000066")

        self.assertRaises(DataFailureException,
                          TestBridge.bridge.get_user_by_bridgeid,
                          250)

    def test_restore_bridge_user(self):
        uw_acc = UwAccount(netid='staff',
                           bridge_id=196,
                           last_updated=get_now())
        buser = TestBridge.bridge.restore_bridge_user(uw_acc)
        self.assertEqual(buser.netid, 'staff')

        uw_acc = UwAccount(netid='staff',
                           last_updated=get_now())
        buser = TestBridge.bridge.restore_bridge_user(uw_acc)
        self.assertEqual(buser.bridge_id, 196)

        self.assertRaises(DataFailureException,
                          TestBridge.bridge.restore_bridge_user,
                          UwAccount(netid='none',
                                    bridge_id=300,
                                    last_updated=get_now()))

    def test_update_user(self):
        user = get_mock_bridge_user(198,
                                    'faculty',
                                    'faculty@uw.edu',
                                    'William E Faculty',
                                    "William E",
                                    "Faculty",
                                    "10000000000000000000000000000005")
        ret_user = TestBridge.bridge.update_user(user)
        self.assertEqual(ret_user.netid, "faculty")

        user = get_mock_bridge_user(300,
                                    'none',
                                    "none@uw.edu",
                                    "No Name",
                                    "No",
                                    "Name",
                                    "10000000000000000000000000000000")
        self.assertRaises(DataFailureException,
                          TestBridge.bridge.update_user, user)

@@ -738,17 +738,17 @@
 users), 
-6
+7
 )%0A      
@@ -767,16 +767,141 @@
 sers%5B0%5D%0A
+        self.assertEqual(buser.bridge_id, 100)%0A        self.assertEqual(buser.netid, 'not_in_pws')%0A        buser = busers%5B1%5D%0A
         
@@ -1001,33 +1001,33 @@
  buser = busers%5B
-1
+2
 %5D%0A        self.a
@@ -1124,33 +1124,33 @@
  buser = busers%5B
-2
+3
 %5D%0A        self.a
@@ -1244,33 +1244,33 @@
  buser = busers%5B
-3
+4
 %5D%0A        self.a
@@ -1365,33 +1365,33 @@
  buser = busers%5B
-4
+5
 %5D%0A        self.a
@@ -1486,33 +1486,33 @@
  buser = busers%5B
-5
+6
 %5D%0A        self.a

fix test