import json
from tornado import testing, httpserver

from qotr.server import make_application
from qotr.channels import Channels

class TestChannelHandler(testing.AsyncHTTPTestCase):
    '''
    Test the channel creation handler.
    '''

    port = None
    application = None

    def get_app(self):
        Channels.reset()
        return make_application()

    def test_create(self):
        salt = "common"
        channel_id = "test-channel"

        body = "&".join([
            "id={channel_id}",
            "salt={salt}",
            "key_hash=hmac-key"
        ]).format(**locals())

        response = json.loads(self.fetch(
            '/c/new', method='POST',
            body=body
        ).body.decode('utf8'))

        self.assertEqual({
            "salt": salt,
            "id": channel_id
        }, response)


    def test_confict(self):
        body = "&".join([
            "id=common",
            "salt=test-channel",
            "key_hash=hmac-key"
        ])

        self.fetch('/c/new', method='POST', body=body)
        response = json.loads(self.fetch(
            '/c/new', method='POST',
            body=body
        ).body.decode('utf8'))

        self.assertTrue("error" in response)

import json
from tornado import testing

from qotr.server import make_application
from qotr.channels import Channels

class TestChannelHandler(testing.AsyncHTTPTestCase):
    '''
    Test the channel creation handler.
    '''

    port = None
    application = None

    def get_app(self):
        Channels.reset()
        return make_application()

    def test_create(self):
        salt = "common"
        channel_id = "test-channel"
        key_hash = 'hmac-key'

        body = "&".join([
            "id={channel_id}",
            "salt={salt}",
            "key_hash={key_hash}"
        ]).format(**locals())

        response = json.loads(self.fetch(
            '/c/new', method='POST',
            body=body
        ).body.decode('utf8'))

        self.assertEqual({
            "salt": salt,
            "id": channel_id
        }, response)

        channel = Channels.get(channel_id)
        self.assertEqual(salt, channel.salt)
        self.assertEqual(key_hash, key_hash)


    def test_confict(self):
        body = "&".join([
            "id=common",
            "salt=test-channel",
            "key_hash=hmac-key"
        ])

        self.fetch('/c/new', method='POST', body=body)
        response = json.loads(self.fetch(
            '/c/new', method='POST',
            body=body
        ).body.decode('utf8'))

        self.assertTrue("error" in response)

Make sure that the channel creation and storage works
Make sure that the channel creation and storage works

Signed-off-by: Rohan Jain <f3a935f2cb7c3d75d1446a19169b923809d6e623@gmail.com>

  import json
- from tornado import testing, httpserver
+ from tornado import testing
  
  from qotr.server import make_application
  from qotr.channels import Channels
  
  class TestChannelHandler(testing.AsyncHTTPTestCase):
      '''
      Test the channel creation handler.
      '''
  
      port = None
      application = None
  
      def get_app(self):
          Channels.reset()
          return make_application()
  
      def test_create(self):
          salt = "common"
          channel_id = "test-channel"
+         key_hash = 'hmac-key'
  
          body = "&".join([
              "id={channel_id}",
              "salt={salt}",
-             "key_hash=hmac-key"
+             "key_hash={key_hash}"
          ]).format(**locals())
  
          response = json.loads(self.fetch(
              '/c/new', method='POST',
              body=body
          ).body.decode('utf8'))
  
          self.assertEqual({
              "salt": salt,
              "id": channel_id
          }, response)
+ 
+         channel = Channels.get(channel_id)
+         self.assertEqual(salt, channel.salt)
+         self.assertEqual(key_hash, key_hash)
  
  
      def test_confict(self):
          body = "&".join([
              "id=common",
              "salt=test-channel",
              "key_hash=hmac-key"
          ])
  
          self.fetch('/c/new', method='POST', body=body)
          response = json.loads(self.fetch(
              '/c/new', method='POST',
              body=body
          ).body.decode('utf8'))
  
          self.assertTrue("error" in response)
  
Make sure that the channel creation and storage works
## Code Before:
import json
from tornado import testing, httpserver

from qotr.server import make_application
from qotr.channels import Channels

class TestChannelHandler(testing.AsyncHTTPTestCase):
    '''
    Test the channel creation handler.
    '''

    port = None
    application = None

    def get_app(self):
        Channels.reset()
        return make_application()

    def test_create(self):
        salt = "common"
        channel_id = "test-channel"

        body = "&".join([
            "id={channel_id}",
            "salt={salt}",
            "key_hash=hmac-key"
        ]).format(**locals())

        response = json.loads(self.fetch(
            '/c/new', method='POST',
            body=body
        ).body.decode('utf8'))

        self.assertEqual({
            "salt": salt,
            "id": channel_id
        }, response)


    def test_confict(self):
        body = "&".join([
            "id=common",
            "salt=test-channel",
            "key_hash=hmac-key"
        ])

        self.fetch('/c/new', method='POST', body=body)
        response = json.loads(self.fetch(
            '/c/new', method='POST',
            body=body
        ).body.decode('utf8'))

        self.assertTrue("error" in response)

## Instruction:
Make sure that the channel creation and storage works
## Code After:
import json
from tornado import testing

from qotr.server import make_application
from qotr.channels import Channels

class TestChannelHandler(testing.AsyncHTTPTestCase):
    '''
    Test the channel creation handler.
    '''

    port = None
    application = None

    def get_app(self):
        Channels.reset()
        return make_application()

    def test_create(self):
        salt = "common"
        channel_id = "test-channel"
        key_hash = 'hmac-key'

        body = "&".join([
            "id={channel_id}",
            "salt={salt}",
            "key_hash={key_hash}"
        ]).format(**locals())

        response = json.loads(self.fetch(
            '/c/new', method='POST',
            body=body
        ).body.decode('utf8'))

        self.assertEqual({
            "salt": salt,
            "id": channel_id
        }, response)

        channel = Channels.get(channel_id)
        self.assertEqual(salt, channel.salt)
        self.assertEqual(key_hash, key_hash)


    def test_confict(self):
        body = "&".join([
            "id=common",
            "salt=test-channel",
            "key_hash=hmac-key"
        ])

        self.fetch('/c/new', method='POST', body=body)
        response = json.loads(self.fetch(
            '/c/new', method='POST',
            body=body
        ).body.decode('utf8'))

        self.assertTrue("error" in response)
