#
# CDR-Stats License
# http://www.cdr-stats.org
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this file,
# You can obtain one at http://mozilla.org/MPL/2.0/.
#
# Copyright (C) 2011-2013 Star2Billing S.L.
#
# The Initial Developer of the Original Code is
# Arezqui Belaid <info@star2billing.com>
#

from common.utils import BaseAuthenticatedClient
#from tastypie.test import ResourceTestCase
import simplejson


class ApiTestCase(BaseAuthenticatedClient):
    """Test cases for CDR-Stats API."""
    fixtures = ['auth_user.json', 'country_dialcode.json', 
                'hangup_cause.json', 'switch.json',
                'voip_gateway.json', 'voip_provider.json',
                'user_profile.json', 'voip_billing.json']

    def test_switch(self):
        """Test Function to create a switch"""
        # Create
        data = simplejson.dumps({"name": "local", "ipaddress": "127.0.0.2"})
        response = self.client.post('/api/v1/switch/', data,
            content_type='application/json', **self.extra)
        self.assertEqual(response.status_code, 201)

        # Read
        response = self.client.get('/api/v1/switch/?format=json', **self.extra)
        self.assertEqual(response.status_code, 200)

        # Update
        data = simplejson.dumps({"name": "localhost", "ipaddress": "127.0.0.1"})
        response = self.client.put('/api/v1/switch/1/', data,
            content_type='application/json', **self.extra)
        self.assertEqual(response.status_code, 204)

        # Delete
        response = self.client.delete('/api/v1/switch/1/', **self.extra)
        self.assertEqual(response.status_code, 204)

    def test_voip_rate(self):
        """Test Function to read voip rate"""
        # Read
        response = self.client.get('/api/v1/voip_rate/?format=json', **self.extra)
        self.assertEqual(response.status_code, 200)

        response = self.client.get('/api/v1/voip_rate/?dialcode=34&format=json', **self.extra)
        self.assertEqual(response.status_code, 200)

        response = self.client.get('/api/v1/voip_rate/?recipient_phone_no=34650784355&format=json', **self.extra)
        self.assertEqual(response.status_code, 200)

    def test_voip_call(self):
        """Test Function to create a switch"""        
        # Read
        response = self.client.get('/api/v1/voip_call/?format=json', **self.extra)
        self.assertEqual(response.status_code, 200)

        # Create
        data = simplejson.dumps({"accountcode":"1000", "answer_uepoch":"1359403221", "billmsec":"12960", 
            "billsec":"104", "caller_id_name":"29914046", "caller_id_number":"29914046", 
            "destination_number":"032287971777", "direction":"inbound", "duration":"154.0", 
            "end_uepoch":"1359403221", "hangup_cause_id":"16", "mduration":"12960", 
            "read_codec":"G711", "remote_media_ip":"192.168.1.21", "resource_uri":"", 
            "start_uepoch":"1359403221", "switch_id":"1", "write_codec":"G711"})
        response = self.client.post('/api/v1/voip_call/', data,
            content_type='application/json', **self.extra)
        #self.assertEqual(response.status_code, 201)

    def test_playground_view(self):
        """Test Function to create a api list view"""
        response = self.client.get("/api-explorer/")
        self.assertEqual(response.status_code, 200)

@@ -483,22 +483,16 @@
 %0Aimport 
-simple
 json%0A%0A%0Ac
@@ -632,17 +632,16 @@
 e.json',
- 
 %0A       
@@ -901,38 +901,32 @@
 %0A        data = 
-simple
 json.dumps(%7B%22nam
@@ -1310,38 +1310,32 @@
 %0A        data = 
-simple
 json.dumps(%7B%22nam
@@ -2313,24 +2313,16 @@
 witch%22%22%22
-        
 %0A       
@@ -2392,38 +2392,24 @@
 format=json'
-, **self.extra
 )%0A        se
@@ -2437,35 +2437,35 @@
 se.status_code, 
-200
+401
 )%0A%0A        # Cre
@@ -2487,14 +2487,8 @@
 a = 
-simple
 json
@@ -2558,33 +2558,32 @@
 llmsec%22:%2212960%22,
- 
 %0A            %22bi
@@ -2655,17 +2655,16 @@
 914046%22,
- 
 %0A       
@@ -2747,17 +2747,16 @@
 %22154.0%22,
- 
 %0A       
@@ -2831,17 +2831,16 @@
 %2212960%22,
- 
 %0A       
@@ -2917,17 +2917,16 @@
 uri%22:%22%22,
- 
 %0A       

Update api testcase