Esempio n. 1
0
class KnowledgeViewsTest(TestCase):
    "Knowledge functional tests for views"

    username = "******"
    password = "******"
    prepared = False
    authentication_headers ={"CONTENT_TYPE": "application/json",
                             "HTTP_AUTHORIZATION" : "Basic YXBpX3Rlc3Q6YXBpX3Bhc3N3b3Jk" }
    content_type ='application/json'
    prepared = False

    def setUp(self):
        "Initial Setup"
        if not self.prepared:
            # Clean up first
            Object.objects.all().delete()

            # Create objects
            try:
                self.group = Group.objects.get(name='test')
            except Group.DoesNotExist:
                Group.objects.all().delete()
                self.group = Group(name='test')
                self.group.save()

            try:
                self.user = DjangoUser.objects.get(username=self.username)
                self.user.set_password(self.password)
                try:
                    self.profile = self.user.get_profile()
                except Exception:
                    User.objects.all().delete()
                    self.user = DjangoUser(username=self.username, password='')
                    self.user.set_password(self.password)
                    self.user.save()
            except DjangoUser.DoesNotExist:
                User.objects.all().delete()
                self.user = DjangoUser(username=self.username, password='')
                self.user.set_password(self.password)
                self.user.save()

            try:
                perspective = Perspective.objects.get(name='default')
            except Perspective.DoesNotExist:
                Perspective.objects.all().delete()
                perspective = Perspective(name='default')
                perspective.set_default_user()
                perspective.save()
            ModuleSetting.set('default_perspective', perspective.id)

            self.folder = KnowledgeFolder(name='test', treepath='test')
            self.folder.set_default_user()
            self.folder.save()

            self.category = KnowledgeCategory(name='test', treepath='test')
            self.category.set_default_user()
            self.category.save()

            self.item = KnowledgeItem(name='test', folder=self.folder,
                                      category=self.category, treepath='test')
            self.item.set_default_user()
            self.item.save()

            # parent folder
            self.parent = KnowledgeFolder(name='test', treepath='test')
            self.parent.set_default_user()
            self.parent.save()

            self.client = Client()

            self.prepared = True

    def test_unauthenticated_access(self):
        "Test index page at /api/knowledge/folders"
        response = self.client.get('/api/knowledge/folders')
        # Redirects as unauthenticated
        self.assertEquals(response.status_code, 401)

    def test_get_folders_list(self):
        """ Test index page api/knowledge/folders """
        response = self.client.get(path=reverse('api_knowledge_folders'), **self.authentication_headers)
        self.assertEquals(response.status_code, 200)

    def test_get_folder(self):
        response = self.client.get(path=reverse('api_knowledge_folders', kwargs={'object_ptr': self.folder.id}), **self.authentication_headers)
        self.assertEquals(response.status_code, 200)

    def test_update_folder(self):
        updates = {'name': 'Api folder update', 'parent': self.parent.id, 'details': '<p>api details</p>'}
        response = self.client.put(path=reverse('api_knowledge_folders', kwargs={'object_ptr': self.folder.id}),
                                   content_type=self.content_type,  data=json.dumps(updates), **self.authentication_headers)
        self.assertEquals(response.status_code, 200)

        data = json.loads(response.content)
        self.assertEquals(updates["name"], data["name"])
        self.assertEquals(updates["parent"], data["parent"]["id"])
        self.assertEquals(updates["details"], data["details"])

    def test_get_categories_list(self):
        """ Test index page api/knowledge/categories """
        response = self.client.get(path=reverse('api_knowledge_categories'), **self.authentication_headers)
        self.assertEquals(response.status_code, 200)

    def test_get_category(self):
        response = self.client.get(path=reverse('api_knowledge_categories', kwargs={'object_ptr': self.category.id}), **self.authentication_headers)
        self.assertEquals(response.status_code, 200)

    def test_update_category(self):
        updates = {'name': 'Api catagory update', 'details': '<p>api details</p>'}
        response = self.client.put(path=reverse('api_knowledge_categories', kwargs={'object_ptr': self.category.id}),
                                   content_type=self.content_type,  data=json.dumps(updates), **self.authentication_headers)
        self.assertEquals(response.status_code, 200)

        data = json.loads(response.content)
        self.assertEquals(updates["name"], data["name"])
        self.assertEquals(updates["details"], data["details"])

    def test_get_items_list(self):
        """ Test index page api/knowledge/items """
        response = self.client.get(path=reverse('api_knowledge_items'), **self.authentication_headers)
        self.assertEquals(response.status_code, 200)

    def test_get_item(self):
        response = self.client.get(path=reverse('api_knowledge_items', kwargs={'object_ptr': self.item.id}), **self.authentication_headers)
        self.assertEquals(response.status_code, 200)

    def test_update_item(self):
        updates = {'name': 'Api item update', 'folder': self.folder.id, 'category': self.category.id,
                   'body': '<p>api body</p>'}
        response = self.client.put(path=reverse('api_knowledge_items', kwargs={'object_ptr': self.item.id}),
                                   content_type=self.content_type,  data=json.dumps(updates), **self.authentication_headers)
        self.assertEquals(response.status_code, 400)
Esempio n. 2
0
class KnowledgeViewsTest(TestCase):
    "Knowledge functional tests for views"

    username = "******"
    password = "******"
    prepared = False

    def setUp(self):
        "Initial Setup"
        if not self.prepared:
            # Clean up first
            Object.objects.all().delete()
            
            # Create objects
            try:
                self.group = Group.objects.get(name='test')
            except Group.DoesNotExist:
                Group.objects.all().delete()
                self.group = Group(name='test')
                self.group.save()
            
            try:
                self.user = DjangoUser.objects.get(username=self.username)
                self.user.set_password(self.password)
                try:
                    self.profile = self.user.get_profile()
                except Exception:
                    User.objects.all().delete()
                    self.user = DjangoUser(username=self.username, password='')
                    self.user.set_password(self.password)
                    self.user.save()
            except DjangoUser.DoesNotExist:
                User.objects.all().delete()
                self.user = DjangoUser(username=self.username, password='')
                self.user.set_password(self.password)
                self.user.save()
                
            try:
                perspective = Perspective.objects.get(name='default')
            except Perspective.DoesNotExist:
                Perspective.objects.all().delete()
                perspective = Perspective(name='default')
                perspective.set_default_user()
                perspective.save()
            ModuleSetting.set('default_perspective', perspective.id)
            
            self.folder = KnowledgeFolder(name='test', treepath='test')
            self.folder.set_default_user()
            self.folder.save()
            
            self.category = KnowledgeCategory(name='test', treepath='test')
            self.category.set_default_user()
            self.category.save()
            
            self.item = KnowledgeItem(name='test', folder=self.folder, 
                                      category=self.category, treepath='test')
            self.item.set_default_user()
            self.item.save()
            
            # parent folder
            self.parent = KnowledgeFolder(name='test', treepath='test')
            self.parent.set_default_user()
            self.parent.save()
            
            self.client = Client()
            
            self.prepared = True


    ######################################
    # Testing views when user is logged in
    ######################################          
        
    def test_index_login(self):
        "Test index page with login at /knowledge/"
        response = self.client.post('/accounts/login',
                                    {'username': self.username, 'password': self.password })
        self.assertRedirects(response, '/')
        response = self.client.get(reverse('knowledge'))
        self.assertEquals(response.status_code, 200)   
        
        
    def test_index_categories_login(self):
        "Test index page with login at /knowledge/categories/"
        response = self.client.post('/accounts/login',
                                    {'username': self.username, 'password': self.password })
        self.assertRedirects(response, '/')
        response = self.client.get(reverse('knowledge_categories'))
        self.assertEquals(response.status_code, 200)   
        

    # Knowledge folders
    
    def test_knowledge_folder_add(self):
        "Test index page with login at /knowledge/folder/add/"
        response = self.client.post('/accounts/login',
                                    {'username': self.username, 'password': self.password })
        self.assertRedirects(response, '/')
        response = self.client.get(reverse('knowledge_folder_add'))
        self.assertEquals(response.status_code, 200)
        
        
    def test_knowledge_folder_add_typed(self):
        "Test index page with login at /knowledge/folder/add/<folderPath>"
        response = self.client.post('/accounts/login',
                                    {'username': self.username, 'password': self.password })
        self.assertRedirects(response, '/')            
        response = self.client.get(reverse('knowledge_folder_add_folder', args=[self.parent.treepath]))
        self.assertEquals(response.status_code, 200)
 
            
    def test_knowledge_folder_view(self):
        "Test index page with login at /knowledge/folder/view/<knowledgeType_id>"
        response = self.client.post('/accounts/login',
                                    {'username': self.username, 'password': self.password })
        self.assertRedirects(response, '/')
        response = self.client.get(reverse('knowledge_folder_view', args=[self.folder.treepath]))
        self.assertEquals(response.status_code, 200)

        
    def test_knowledge_folder_edit(self):
        "Test index page with login at /knowledge/folder/edit/<knowledgeType_id>"
        response = self.client.post('/accounts/login',
                                    {'username': self.username, 'password': self.password })
        self.assertRedirects(response, '/')
        response = self.client.get(reverse('knowledge_folder_edit', args=[self.folder.id]))
        self.assertEquals(response.status_code, 200)

        
    def test_knowledge_folder_delete(self):
        "Test index page with login at /knowledge/folder/delete/<knowledgeType_id>"
        response = self.client.post('/accounts/login',
                                    {'username': self.username, 'password': self.password })
        self.assertRedirects(response, '/')
        response = self.client.get(reverse('knowledge_folder_delete', args=[self.folder.id]))
        self.assertEquals(response.status_code, 200)
        
        
    # Knowledge items
    
    def test_knowledge_item_add(self):
        "Test index page with login at /knowledge/item/add"
        response = self.client.post('/accounts/login',
                                    {'username': self.username, 'password': self.password })
        self.assertRedirects(response, '/')
        response = self.client.get(reverse('knowledge_item_add'))
        self.assertEquals(response.status_code, 200)
        

    def test_knowledge_item_add_typed(self):
        "Test index page with login at /knowledge/item/add/<folderPath>"
        response = self.client.post('/accounts/login',
                                    {'username': self.username, 'password': self.password })
        self.assertRedirects(response, '/')
        response = self.client.get(reverse('knowledge_item_add_folder', args=[self.folder.treepath]))
        self.assertEquals(response.status_code, 200)
            
    
    def test_knowledge_item_view(self):
        "Test index page with login at /knowledge/item/view/<knowledgeItem_id>"
        response = self.client.post('/accounts/login',
                                    {'username': self.username, 'password': self.password })
        self.assertRedirects(response, '/')
        response = self.client.get(reverse('knowledge_item_view', 
                                           args=[self.item.folder.treepath, self.item.treepath]))
        self.assertEquals(response.status_code, 200)
 
        
    def test_knowledge_item_edit(self):
        "Test index page with login at /knowledge/item/edit/<knowledgeItem_id>"
        response = self.client.post('/accounts/login',
                                    {'username': self.username, 'password': self.password })
        self.assertRedirects(response, '/')
        response = self.client.get(reverse('knowledge_item_edit', args=[self.item.id]))
        self.assertEquals(response.status_code, 200)

        
    def test_knowledge_item_delete(self):
        "Test index page with login at /knowledge/item/delete/<knowledgeItem_id>"
        response = self.client.post('/accounts/login',
                                    {'username': self.username, 'password': self.password })
        self.assertRedirects(response, '/')
        response = self.client.get(reverse('knowledge_item_delete', args=[self.item.id]))
        self.assertEquals(response.status_code, 200)


    # Knowledge categories

    def test_knowledge_category_add(self):
        "Test index page with login at /knowledge/category/add"
        response = self.client.post('/accounts/login',
                                    {'username': self.username, 'password': self.password })
        self.assertRedirects(response, '/')
        response = self.client.get(reverse('knowledge_category_add'))
        self.assertEquals(response.status_code, 200)
        
    
    def test_knowledge_category_view(self):
        "Test index page with login at /knowledge/category/view/<knowledgeCategory_id>"
        response = self.client.post('/accounts/login',
                                    {'username': self.username, 'password': self.password })
        self.assertRedirects(response, '/')
        response = self.client.get(reverse('knowledge_category_view', args=[self.category.treepath]))
        self.assertEquals(response.status_code, 200)

        
    def test_knowledge_category_edit(self):
        "Test index page with login at /knowledge/category/edit/<knowledgeCategory_id>"
        response = self.client.post('/accounts/login',
                                    {'username': self.username, 'password': self.password })
        self.assertRedirects(response, '/')
        response = self.client.get(reverse('knowledge_category_edit', args=[self.category.id]))
        self.assertEquals(response.status_code, 200)
        
        
    def test_knowledge_category_delete(self):
        "Test index page with login at /knowledge/category/delete/<knowledgeCategory_id>"
        response = self.client.post('/accounts/login',
                                    {'username': self.username, 'password': self.password })
        self.assertRedirects(response, '/')
        response = self.client.get(reverse('knowledge_category_delete', args=[self.category.id]))
        self.assertEquals(response.status_code, 200)
        

    ######################################
    # Testing views when user is not logged in
    ######################################  

    def test_index(self):
        "Test index page at /knowledge/"
        response = self.client.get('/knowledge/')
        # Redirects as unauthenticated
        self.assertRedirects(response, reverse('user_login')) 
        
    def test_index_categories_out(self):
        "Testing /knowledge/categories/"
        response = self.client.get(reverse('knowledge_categories'))
        self.assertRedirects(response, reverse('user_login')) 
        
    # Knowledge folders
    
    def test_knowledge_folder_add_out(self):
        "Testing /knowledge/folder/add/"
        response = self.client.get(reverse('knowledge_folder_add'))
        self.assertRedirects(response, reverse('user_login')) 
        
    def test_knowledge_folder_add_typed_out(self):
        "Testing /knowledge/folder/add/<folderPath>"          
        response = self.client.get(reverse('knowledge_folder_add_folder', args=[self.parent.treepath]))
        self.assertRedirects(response, reverse('user_login')) 
            
    def test_knowledge_folder_view_out(self):
        "Testing /knowledge/folder/view/<knowledgeType_id>"
        response = self.client.get(reverse('knowledge_folder_view', args=[self.folder.treepath]))
        self.assertRedirects(response, reverse('user_login')) 
        
    def test_knowledge_folder_edit_out(self):
        "Testing /knowledge/folder/edit/<knowledgeType_id>"
        response = self.client.get(reverse('knowledge_folder_edit', args=[self.folder.id]))
        self.assertRedirects(response, reverse('user_login')) 
        
    def test_knowledge_folder_delete_out(self):
        "Testing /knowledge/folder/delete/<knowledgeType_id>"
        response = self.client.get(reverse('knowledge_folder_delete', args=[self.folder.id]))
        self.assertRedirects(response, reverse('user_login')) 
        
        
    # Knowledge items
    
    def test_knowledge_item_add_out(self):
        "Testing /knowledge/item/add"
        response = self.client.get(reverse('knowledge_item_add'))
        self.assertRedirects(response, reverse('user_login')) 

    def test_knowledge_item_add_typed_out(self):
        "Testing /knowledge/item/add/<folderPath>"
        response = self.client.get(reverse('knowledge_item_add_folder', args=[self.folder.treepath]))
        self.assertRedirects(response, reverse('user_login'))     
    
    def test_knowledge_item_view_out(self):
        "Testing /knowledge/item/view/<knowledgeItem_id>"
        response = self.client.get(reverse('knowledge_item_view', 
                                           args=[self.item.folder.treepath, self.item.treepath]))
        self.assertRedirects(response, reverse('user_login')) 
        
    def test_knowledge_item_edit_out(self):
        "Testing /knowledge/item/edit/<knowledgeItem_id>"
        response = self.client.get(reverse('knowledge_item_edit', args=[self.item.id]))
        self.assertRedirects(response, reverse('user_login')) 
        
    def test_knowledge_item_delete_out(self):
        "Testing /knowledge/item/delete/<knowledgeItem_id>"
        response = self.client.get(reverse('knowledge_item_delete', args=[self.item.id]))
        self.assertRedirects(response, reverse('user_login')) 
        

    # Knowledge categories

    def test_knowledge_category_add_out(self):
        "Testing /knowledge/category/add"
        response = self.client.get(reverse('knowledge_category_add'))
        self.assertRedirects(response, reverse('user_login')) 
    
    def test_knowledge_category_view_out(self):
        "Testing /knowledge/category/view/<knowledgeCategory_id>"
        response = self.client.get(reverse('knowledge_category_view', args=[self.category.treepath]))
        self.assertRedirects(response, reverse('user_login')) 
        
    def test_knowledge_category_edit_out(self):
        "Testing /knowledge/category/edit/<knowledgeCategory_id>"
        response = self.client.get(reverse('knowledge_category_edit', args=[self.category.id]))
        self.assertRedirects(response, reverse('user_login')) 
        
    def test_knowledge_category_delete_out(self):
        "Testing /knowledge/category/delete/<knowledgeCategory_id>"
        response = self.client.get(reverse('knowledge_category_delete', args=[self.category.id]))
        self.assertRedirects(response, reverse('user_login')) 
Esempio n. 3
0
class KnowledgeViewsTest(TestCase):

    "Knowledge functional tests for views"

    username = "******"
    password = "******"
    prepared = False
    authentication_headers = {
        "CONTENT_TYPE": "application/json",
        "HTTP_AUTHORIZATION": "Basic YXBpX3Rlc3Q6YXBpX3Bhc3N3b3Jk"
    }
    content_type = 'application/json'
    prepared = False

    def setUp(self):
        "Initial Setup"
        if not self.prepared:
            # Clean up first
            Object.objects.all().delete()

            # Create objects
            try:
                self.group = Group.objects.get(name='test')
            except Group.DoesNotExist:
                Group.objects.all().delete()
                self.group = Group(name='test')
                self.group.save()

            try:
                self.user = DjangoUser.objects.get(username=self.username)
                self.user.set_password(self.password)
                try:
                    self.profile = self.user.get_profile()
                except Exception:
                    User.objects.all().delete()
                    self.user = DjangoUser(username=self.username, password='')
                    self.user.set_password(self.password)
                    self.user.save()
            except DjangoUser.DoesNotExist:
                User.objects.all().delete()
                self.user = DjangoUser(username=self.username, password='')
                self.user.set_password(self.password)
                self.user.save()

            try:
                perspective = Perspective.objects.get(name='default')
            except Perspective.DoesNotExist:
                Perspective.objects.all().delete()
                perspective = Perspective(name='default')
                perspective.set_default_user()
                perspective.save()
            ModuleSetting.set('default_perspective', perspective.id)

            self.folder = KnowledgeFolder(name='test', treepath='test')
            self.folder.set_default_user()
            self.folder.save()

            self.category = KnowledgeCategory(name='test', treepath='test')
            self.category.set_default_user()
            self.category.save()

            self.item = KnowledgeItem(name='test',
                                      folder=self.folder,
                                      category=self.category,
                                      treepath='test')
            self.item.set_default_user()
            self.item.save()

            # parent folder
            self.parent = KnowledgeFolder(name='test', treepath='test')
            self.parent.set_default_user()
            self.parent.save()

            self.client = Client()

            self.prepared = True

    def test_unauthenticated_access(self):
        "Test index page at /api/knowledge/folders"
        response = self.client.get('/api/knowledge/folders')
        # Redirects as unauthenticated
        self.assertEquals(response.status_code, 401)

    def test_get_folders_list(self):
        """ Test index page api/knowledge/folders """
        response = self.client.get(path=reverse('api_knowledge_folders'),
                                   **self.authentication_headers)
        self.assertEquals(response.status_code, 200)

    def test_get_folder(self):
        response = self.client.get(path=reverse(
            'api_knowledge_folders', kwargs={'object_ptr': self.folder.id}),
                                   **self.authentication_headers)
        self.assertEquals(response.status_code, 200)

    def test_update_folder(self):
        updates = {
            'name': 'Api folder update',
            'parent': self.parent.id,
            'details': '<p>api details</p>'
        }
        response = self.client.put(path=reverse(
            'api_knowledge_folders', kwargs={'object_ptr': self.folder.id}),
                                   content_type=self.content_type,
                                   data=json.dumps(updates),
                                   **self.authentication_headers)
        self.assertEquals(response.status_code, 200)

        data = json.loads(response.content)
        self.assertEquals(updates["name"], data["name"])
        self.assertEquals(updates["parent"], data["parent"]["id"])
        self.assertEquals(updates["details"], data["details"])

    def test_get_categories_list(self):
        """ Test index page api/knowledge/categories """
        response = self.client.get(path=reverse('api_knowledge_categories'),
                                   **self.authentication_headers)
        self.assertEquals(response.status_code, 200)

    def test_get_category(self):
        response = self.client.get(path=reverse(
            'api_knowledge_categories',
            kwargs={'object_ptr': self.category.id}),
                                   **self.authentication_headers)
        self.assertEquals(response.status_code, 200)

    def test_update_category(self):
        updates = {
            'name': 'Api catagory update',
            'details': '<p>api details</p>'
        }
        response = self.client.put(path=reverse(
            'api_knowledge_categories',
            kwargs={'object_ptr': self.category.id}),
                                   content_type=self.content_type,
                                   data=json.dumps(updates),
                                   **self.authentication_headers)
        self.assertEquals(response.status_code, 200)

        data = json.loads(response.content)
        self.assertEquals(updates["name"], data["name"])
        self.assertEquals(updates["details"], data["details"])

    def test_get_items_list(self):
        """ Test index page api/knowledge/items """
        response = self.client.get(path=reverse('api_knowledge_items'),
                                   **self.authentication_headers)
        self.assertEquals(response.status_code, 200)

    def test_get_item(self):
        response = self.client.get(path=reverse(
            'api_knowledge_items', kwargs={'object_ptr': self.item.id}),
                                   **self.authentication_headers)
        self.assertEquals(response.status_code, 200)

    def test_update_item(self):
        updates = {
            'name': 'Api item update',
            'folder': self.folder.id,
            'category': self.category.id,
            'body': '<p>api body</p>'
        }
        response = self.client.put(path=reverse(
            'api_knowledge_items', kwargs={'object_ptr': self.item.id}),
                                   content_type=self.content_type,
                                   data=json.dumps(updates),
                                   **self.authentication_headers)
        self.assertEquals(response.status_code, 400)