# -*- coding: utf8 -*-

import mock
import nose
import requests

import aliyunauth


class TestSlb(object):
    CORRECT_URL = (
        "http://slb.example.com/"
        "?AccessKeyId=access-key"
        "&Action=Test"
        "&Foo=bar"
        "&Format=json"
        "&Signature=4SDI5sAk3SXCP5Vp3dkzBuT1xtQ%3D"
        "&SignatureMethod=HMAC-SHA1"
        "&SignatureNonce=mock-nonce"
        "&SignatureVersion=1.0"
        "&Timestamp=mock-date"
        "&Version=2015-04-20"
    )

    def setup(self):
        req_obj = requests.Request(
            "GET",
            "http://slb.example.com/",
            params={"Action": "Test", "Foo": "bar"},
            auth=aliyunauth.SlbAuth("access-key", "secret-key", "xml")
        )
        self.req = req_obj

    def test_attrs(self):
        nose.tools.eq_(aliyunauth.SlbAuth.VERSION, "2015-04-20")
        nose.tools.eq_(aliyunauth.SlbAuth.SERVICE, "smb")

    @mock.patch("time.strftime")
    @mock.patch("uuid.uuid4")
    def test_auth(self, mock_uuid, mock_time):
        fake_uuid = mock.MagicMock()
        fake_uuid.hex = "mock-nonce"
        mock_uuid.return_value = fake_uuid
        mock_time.return_value = "mock-date"

        req = self.req.prepare()
        nose.tools.eq_(req.url, self.CORRECT_URL)

# -*- coding: utf8 -*-

import mock
import nose
import requests

import aliyunauth


class TestCms(object):
    CORRECT_URL = (
        "http://cms.example.com/"
        "?AccessKeyId=access-key"
        "&Action=Test"
        "&Foo=bar"
        "&Format=json"
        "&Signature=4SDI5sAk3SXCP5Vp3dkzBuT1xtQ%3D"
        "&SignatureMethod=HMAC-SHA1"
        "&SignatureNonce=mock-nonce"
        "&SignatureVersion=1.0"
        "&Timestamp=mock-date"
        "&Version=2015-04-20"
    )

    def setup(self):
        req_obj = requests.Request(
            "GET",
            "http://cms.example.com/",
            params={"Action": "Test", "Foo": "bar"},
            auth=aliyunauth.CmsAuth("access-key", "secret-key", "xml")
        )
        self.req = req_obj

    def test_attrs(self):
        nose.tools.eq_(aliyunauth.CmsAuth.VERSION, "2015-04-20")
        nose.tools.eq_(aliyunauth.CmsAuth.SERVICE, "smb")

    @mock.patch("time.strftime")
    @mock.patch("uuid.uuid4")
    def test_auth(self, mock_uuid, mock_time):
        fake_uuid = mock.MagicMock()
        fake_uuid.hex = "mock-nonce"
        mock_uuid.return_value = fake_uuid
        mock_time.return_value = "mock-date"

        req = self.req.prepare()
        nose.tools.eq_(req.url, self.CORRECT_URL)

fix test case