"""
This file demonstrates writing tests using the unittest module. These will pass
when you run "manage.py test".

Replace this with more appropriate tests for your application.
"""

from django.test import TestCase
from django.test.client import Client
from django.contrib.auth.models import User
from auth_mac.models import Credentials
import datetime
import hmac, hashlib, base64
import unittest
from auth_mac.tools import Signature

class Test_NoAuthorisation(TestCase):
  urls = "auth_mac.tests.urls"
  def test_access_restricted(self):
    "Tests accessing restricted functions without authentication"
    c = Client()
    response = c.get("/unattainable_resource")
    self.assertEqual(response.status_code, 401)
    self.assertEqual(response['WWW-Authenticate'], "MAC")
    
    response = c.get("/protected_resource")
    self.assertEqual(response.status_code, 401)
    self.assertEqual(response['WWW-Authenticate'], "MAC")



class Test_Signatures(TestCase):
  urls = "auth_mac.tests.urls"

  def setUp(self):
    # Create a user to authorise with
    self.user = User.objects.create_user("testuser", "test@test.com")
    self.user.save()
    # And, create a MAC access credentials for this user
    # self.credentials = Credentials(user=user, identifier="h480djs93hd8", key="489dks293j39")
    self.rfc_credentials = rfc_creds = Credentials(user=self.user, identifier="h480djs93hd8", key="489dks293j39")
  
  @unittest.expectedFailure
  def test_credential_object(self):
    """Test the credentials object using the IETF Draft values.
    See: http://tools.ietf.org/html/draft-ietf-oauth-v2-http-mac-01 """
        
    ms = Signature(self.rfc_credentials, host="example.com", port=80, method="GET")
    ms.sign_request(uri="/resource/1?b=1&a=2", timestamp="1336363200", nonce="dj83hs9s")
    # Validate that we calculated the base string correctly
    example_bs = "1336363200\ndj83hs9s\nGET\n/resource/1?b=1&a=2\nexample.com\n80\n\n"
    self.assertEqual(ms.base_string, example_bs)
    self.assertEqual(ms.signature, "bhCQXTVyfj5cmA9uKkPFx1zeOXM=")

  def test_possible_erroneous_credentials(self):
    "Test that we don't stray from our self-calculated output"
    # Same as above test, but with a different signature
    ms = Signature(self.rfc_credentials, host="example.com", port=80, method="GET")
    ms.sign_request(uri="/resource/1?b=1&a=2", timestamp="1336363200", nonce="dj83hs9s")
    example_bs = "1336363200\ndj83hs9s\nGET\n/resource/1?b=1&a=2\nexample.com\n80\n\n"
    self.assertEqual(ms.base_string, example_bs)
    self.assertEqual(ms.signature, "6T3zZzy2Emppni6bzL7kdRxUWL4=")


class TestRequest(TestCase):
  urls = "auth_mac.tests.urls"
  def test_dumping(self):
    c = Client()
    # print c.get("/dump_request", HTTP_AUTHORIZATION=)
    data = {"id": "h480djs93hd8","ts": "1336363200","nonce":"dj83hs9s","mac":"bhCQXTVyfj5cmA9uKkPFx1zeOXM="}
    # print build_authheader("MAC", data)
@@ -6,177 +6,41 @@
 his 
-file demonstrates writing tests using the unittest module. These will pass%0Awhen you run %22manage.py test%22.%0A%0AReplace this with more appropriate tests for your application.
+module tests the auth_mac package
 %0A%22%22%22
@@ -2471,187 +2471,347 @@
 =%22)%0A
-%0A%0Aclass TestRequest(TestCase
+  %0A  def test_creating_authheader(self
 ):%0A  
-urls = %22auth_mac.tests.urls%22%0A  def test_dumping(self):%0A    c = Client()%0A    # print c.get(%22/dump_request%22, HTTP_AUTHORIZATION=)%0A    data = %7B%22id%22: 
+  %22Test the creation of the auth header%22%0A    ms = Signature(self.rfc_credentials, host=%22example.com%22, port=80, method=%22GET%22)%0A    ms.update(uri=%22/resource/1?b=1&a=2%22, timestamp=%221336363200%22, nonce=%22dj83hs9s%22)%0A    expected_authheader = 'MAC nonce=%22dj83hs9s%22, mac=%226T3zZzy2Emppni6bzL7kdRxUWL4=%22, id=
 %22h48
@@ -2825,14 +2825,12 @@
 d8%22,
-%22ts%22: 
+ ts=
 %22133
@@ -2841,103 +2841,150 @@
 200%22
-,%22nonce%22:%22dj83hs9s%22,%22mac%22:%22bhCQXTVyfj5cmA9uKkPFx1zeOXM=%22%7D%0A    # print build_authheader(%22MAC%22, data)
+'%0A    header = ms.get_header()%0A    self.assertEqual(expected_authheader, header)%0A%0Aclass TestRequest(TestCase):%0A  urls = %22auth_mac.tests.urls%22%0A    

Add a test of the Authorization header functionality