from observertest import BaseObserverToscaTest

from core.models import Site, Deployment, User, ControllerUser

# Note that as a side effect, these tests will also create a Site

class ObserverUserTest(BaseObserverToscaTest):
    tests = ["create_user"]
    # hide_observer_output = False # uncomment to display lots of stuff to screen

    def cleanup(self):
        # We don't want to leak resources, so we make sure to let the observer
        # attempt to delete these objects.
        self.try_to_delete(User, purge=False, email="johndoe@foo.bar")
        self.try_to_delete(Site, purge=False, login_base="testsite")
        self.run_observer()
        self.try_to_delete(User, purge=True, email="johndoe@foo.bar")
        self.try_to_delete(Site, purge=True, login_base="testsite")

    def assert_nouser(self, email):
        assert(not User.objects.filter(email=email))

    def assert_user(self, email, **kwargs):
        obj = User.objects.get(email=email)
        assert(obj)
        for (k,v) in kwargs.items():
            if (getattr(obj,k,None) != v):
                print "Object %s property '%s' is '%s' and should be '%s'" % (obj, k, getattr(obj,k,None), v)
                assert(False)
        return obj

    def create_user(self):
        self.assert_noobj(Site, "testsite")
                                            props={"no-delete": True}) +  \
"""
    testsite:
      type: tosca.nodes.Site
      properties:
          site_url: http://opencloud.us/
      requirements:
          - deployment:
               node: %s
               relationship: tosca.relationships.SiteDeployment
               requirements:
                   - controller:
                       node: %s
                       relationship: tosca.relationships.UsesController
    johndoe@foo.bar:
      type: tosca.nodes.User
      properties:
          password: letmein
          firstname: john
          lastname: doe
      requirements:
          - site:
              node: testsite
              relationship: tosca.relationships.MemberOfSite
""" % (self.get_usable_deployment(), self.get_usable_controller()))

        testsite = self.assert_obj(Site, "testsite")
        testuser = self.assert_user("johndoe@foo.bar")

        self.run_model_policy(save_output="/tmp/usertest:create_user:model_policy")

        # make sure a ControllerSite object was created
        cu = ControllerUser.objects.filter(user=testuser)
        assert(len(cu) == 1)

        self.run_observer(save_output="/tmp/usertest:create_user:observer")

        testuser = self.assert_user("johndoe@foo.bar")

        cu = ControllerUser.objects.filter(user=testuser)
        assert(len(cu) == 1)
        assert(cu[0].kuser_id is not None)
        assert(cu[0].kuser_id != "")

if __name__ == "__main__":
    ObserverUserTest()
        self.assert_nouser("johndoe@foo.bar")
        self.execute(self.make_nodetemplate(self.get_usable_deployment(), "tosca.nodes.Deployment",