from django.test import TestCase
from django.test import Client

from base.models import Version

import os
import tempfile

def get_test_file_path(file_name):
    dir = os.path.dirname(__file__)
    return os.path.join(dir, "testdata/%s" % (file_name))

def remove_dir(top):
    for root, dirs, files in os.walk(top, topdown=False):
        for name in files:
            os.remove(os.path.join(root, name))
        for name in dirs:
            os.rmdir(os.path.join(root, name))

class TestCrashUpload(TestCase):

    def setUp(self):
        self.version = Version.objects.create(major_version = 1,
                minor_version = 2, micro_version = 3, patch_version = 4)
        self.tmp_dir = tempfile.mkdtemp()

    def tearDown(self):
        remove_dir(self.tmp_dir)

    def test_uploadCorrectCrash(self):
        c = Client()
        with self.settings(TEMP_UPLOAD_DIR=self.tmp_dir):
            with open(get_test_file_path("test")) as f:
                response = c.post('/submit/', {'upload_file_minidump':f, 'Version': '1.2.3.4'})
        self.assertEqual(response.status_code, 200)

    def test_uploadInvalidVersion(self):
        c = Client()
        with open(get_test_file_path("test")) as f:
            response = c.post('/submit/', {'upload_file_minidump':f, 'Version': '2.3.4.5'})
        self.assertEqual(response.status_code, 500)

    def test_uploadInvalidFile(self):
        c = Client()
        with open(get_test_file_path("test")) as f:
            response = c.post('/submit/', {'Version': self.version})
        self.assertEqual(response.status_code, 400)

    def test_uploadMetadata(self):
        c = Client()
        with open(get_test_file_path("test")) as f:
            response = c.post('/submit/', {'upload_file_minidump':f, 'Version': '1.2.3.4'})
        self.assertEqual(response.status_code, 200)

@@ -1817,28 +1817,663 @@
 (response.status_code, 200)%0A
+%0A    def test_uploadDeprecatedAttribs(self):%0A        c = Client()%0A        with open(get_test_file_path(%22test%22)) as f:%0A            response = c.post('/submit/', %7B'upload_file_minidump':f, 'Version': '1.2.3.4', 'AdapterDeviceId': 'Device1', 'AdapterVendorId': 'Vendor1'%7D)%0A        self.assertEqual(response.status_code, 200)%0A%0A    def test_uploadJsonAttribs(self):%0A        c = Client()%0A        with open(get_test_file_path(%22test%22)) as f:%0A            response = c.post('/submit/', %7B'upload_file_minidump':f, 'Version': '1.2.3.4', 'AdditionalData': '%7B %22key1%22 : %22val1%22, %22key2%22 : %22val2%22%7D'%7D)%0A        self.assertEqual(response.status_code, 200)%0A

add tests for new json based additional data upload