import model
from app import app
import unittest
import tempfile
import hashlib
from StringIO import StringIO
import os

class UploadTestCase(unittest.TestCase):
    def setUp(self):
        model.setURI('sqlite:///:memory:')
        model.syncdb()

        app.config['UPLOAD_PATH'] = tempfile.mkdtemp()
        try:
            model.Page.create(title='index', slug='index')
        except:
            pass
        self.app = app.test_client()
        self.app.post('/index', data={'body': '', 'message': ''})

    def test_empty_upload(self):
      self.app.post('/index/attach', data={
        'file': (StringIO(''), 'empty.txt')
      })
      sha = hashlib.sha256()
      sha.update('')
      emptySha = sha.hexdigest()
      uploadedFile = os.path.join(app.config['UPLOAD_PATH'],
        model.Attachment.hashPath(emptySha, 'empty.txt'))

      self.assertTrue(os.path.exists(uploadedFile))
      self.assertEqual(self.app.get('/index/file/empty_txt').data, '')

    def test_simple_upload(self):
      self.app.post('/index/attach', data={
        'file': (StringIO('FOOBAR'), 'foo.bar')
      })
      sha = hashlib.sha256()
      sha.update('FOOBAR')
      emptySha = sha.hexdigest()
      uploadedFile = os.path.join(app.config['UPLOAD_PATH'],
        model.Attachment.hashPath(emptySha, 'foo.bar'))

      self.assertTrue(os.path.exists(uploadedFile))
      self.assertEqual(self.app.get('/index/file/foo_bar').data, 'FOOBAR')

import model
from app import app
import unittest
import tempfile
import hashlib
from StringIO import StringIO
import os

class UploadTestCase(unittest.TestCase):
    def setUp(self):
        model.setURI('sqlite:///:memory:')
        model.syncdb()

        model.Page.create(title='index', slug='index')

        app.config['UPLOAD_PATH'] = tempfile.mkdtemp()
        self.app = app.test_client()
        self.app.post('/index', data={'body': '', 'message': ''})

    def test_empty_upload(self):
      self.app.post('/index/attach', data={
        'file': (StringIO(''), 'empty.txt')
      })
      sha = hashlib.sha256()
      sha.update('')
      emptySha = sha.hexdigest()
      uploadedFile = os.path.join(app.config['UPLOAD_PATH'],
        model.Attachment.hashPath(emptySha, 'empty.txt'))

      self.assertTrue(os.path.exists(uploadedFile))
      self.assertEqual(self.app.get('/index/file/empty_txt').data, '')

    def test_simple_upload(self):
      self.app.post('/index/attach', data={
        'file': (StringIO('FOOBAR'), 'foo.bar')
      })
      sha = hashlib.sha256()
      sha.update('FOOBAR')
      emptySha = sha.hexdigest()
      uploadedFile = os.path.join(app.config['UPLOAD_PATH'],
        model.Attachment.hashPath(emptySha, 'foo.bar'))

      self.assertTrue(os.path.exists(uploadedFile))
      self.assertEqual(self.app.get('/index/file/foo_bar').data, 'FOOBAR')

Clean up page creation in upload test