#!/usr/bin/env python

import os

from mock import MagicMock

from serf_master import SerfHandler, SerfHandlerProxy


class TestSerfHandler:
    def setup(self):
        os.environ = {
            'SERF_SELF_NAME': 'local',
            'SERF_SELF_ROLE': 'web',
            'SERF_EVENT': 'member-join',
            'SERF_USER_EVENT': 'deploy',
        }
        self.load_handler()

    def load_handler(self):
        self.handler = SerfHandler()

    def test_name_set_from_env(self):
        assert self.handler.name == 'local'

    def test_role_set_from_env(self):
        assert self.handler.role == 'web'

    def test_event_set_from_env(self):
        assert self.handler.event == 'member_join'

    def test_custom_event(self):
        os.environ['SERF_EVENT'] = 'user'
        self.load_handler()
        assert self.handler.event == 'deploy'


class TestSerfHandlerNegativeCases:

    def setup(self):
        os.environ = {
            'SERF_SELF_NAME': 'null',
            'SERF_SELF_ROLE': 'null',
            'SERF_EVENT': 'member-join',
        }
        self.handler = SerfHandlerProxy()
        self.handler.log = MagicMock(return_value=True)
        self.handler.handlers = {}
        assert len(self.handler.handlers) == 0

    def test_no_handler(self):
        self.handler.run()
        self.handler.log.assert_called_with("no handler for role")

    def test_no_method_implemented(self):
        self.handler.register('default', SerfHandler())
        assert len(self.handler.handlers) == 1
        self.handler.run()
        self.handler.log.assert_called_with("event not implemented by class")


class TestSerfHandlerProxyCustomEvent:

    def test_method_implemented(self):
        os.environ = {
            'SERF_SELF_NAME': 'null',
            'SERF_SELF_ROLE': 'null',
            'SERF_EVENT': 'user',
            'SERF_USER_EVENT': 'implemented',
        }
        handler = SerfHandlerProxy()
        handler.handlers = {}
        sample = SerfHandler()
        sample.implemented = MagicMock(return_value=True)
        assert len(handler.handlers) == 0
        handler.register('default', sample)
        assert len(handler.handlers) == 1
        handler.run()
        sample.implemented.assert_called_with()


class TestSerfHandlerProxyStandardEvent:
    def setup(self):
        os.environ = {
            'SERF_SELF_NAME': 'null',
            'SERF_SELF_ROLE': 'web',
            'SERF_EVENT': 'member-join',
        }
        self.handler = SerfHandlerProxy()
        self.sample = SerfHandler()
        self.sample.member_join = MagicMock(return_value=True)
        self.handler.handlers = {}

    def test_standard_events_called(self):
        self.handler.register('default', self.sample)
        assert len(self.handler.handlers) == 1
        assert 'default' in self.handler.handlers
        self.handler.run()
        self.sample.member_join.assert_called_with()

    def test_role_registration(self):
        self.handler.register('web', self.sample)
        assert len(self.handler.handlers) == 1
        assert 'web' in self.handler.handlers
        self.handler.run()
        self.sample.member_join.assert_called_with()

@@ -1157,43 +1157,8 @@
 ue)%0A
-        self.handler.handlers = %7B%7D%0A
     
@@ -1200,16 +1200,16 @@
 s) == 0%0A
+
 %0A    def
@@ -1888,38 +1888,8 @@
 y()%0A
-        handler.handlers = %7B%7D%0A
     
@@ -2454,32 +2454,32 @@
 = SerfHandler()%0A
+
         self.sam
@@ -2528,43 +2528,8 @@
 rue)
-%0A        self.handler.handlers = %7B%7D
 %0A%0A  

remove unneeded test lines after correcting class