Exemplo n.º 1
0
def post_create(request):
    #todo isPublished affects
    """
    Render and process a form to add a Post.
    """
    #Check permission
    if not can_create_post(request.user):
        return render(request, "main/no_permission.html")

    if not (request.POST or request.GET):
        form = PostCreateForm()
        return render(request, 'post/post_create.html', {'form': form})
    else:
        #Form POST request is submitted
        if request.POST:
            form = PostCreateForm(request.POST)
        else:
            form = PostCreateForm(request.GET)
        if form.is_valid():
            print "form valid"
            model_instance = form.save(commit=False)
            model_instance.created_by_id = request.user.id
            model_instance.created_at = timezone.now()
            model_instance.save()
            #Although isPublished = false, it still shared on facebook
            social_center = SocialCenter()
            social_center.publish(
                model_instance.title, model_instance.content,
                request.build_absolute_uri("/post/post_retrieve/%d" %
                                           model_instance.id))
            return redirect("/post/post_retrieve/%d/" % model_instance.id)
        else:
            return render(request, 'post/post_create.html', {'form': form})
Exemplo n.º 2
0
def post_create(request):
    # todo isPublished affects
    """
    Render and process a form to add a Post.
    """
    # Check permission
    if not can_create_post(request.user):
        return render(request, "main/no_permission.html")

    if not (request.POST or request.GET):
        form = PostCreateForm()
        return render(request, "post/post_create.html", {"form": form})
    else:
        # Form POST request is submitted
        if request.POST:
            form = PostCreateForm(request.POST)
        else:
            form = PostCreateForm(request.GET)
        if form.is_valid():
            print "form valid"
            model_instance = form.save(commit=False)
            model_instance.created_by_id = request.user.id
            model_instance.created_at = timezone.now()
            model_instance.save()
            # Although isPublished = false, it still shared on facebook
            social_center = SocialCenter()
            social_center.publish(
                model_instance.title,
                model_instance.content,
                request.build_absolute_uri("/post/post_retrieve/%d" % model_instance.id),
            )
            return redirect("/post/post_retrieve/%d/" % model_instance.id)
        else:
            return render(request, "post/post_create.html", {"form": form})
Exemplo n.º 3
0
    def test_social_center_is_singleton(self):
        social_center1 = SocialCenter()
        social_center2 = SocialCenter()

        self.assertIs(social_center1, social_center2)

        del social_center1
        del social_center2
Exemplo n.º 4
0
 def setUpClass(self):
     self.user = self.generate_user_with_permissions()
     self.test_bot = TestBot()
     self.test_paged_bot = TestBotWithPages()
     self.social_center = SocialCenter()
     self.default_site_info = self.social_center.get_sites()
     self.social_center.add_site(self.TEST_SITE, self.test_bot)
     self.social_center.add_site(self.TEST_PAGED_SITE, self.test_paged_bot)
Exemplo n.º 5
0
 def setUpClass(self):
     self.test_bot = TestBot()
     self.test_paged_bot = TestBotWithPages()
     self.social_center = SocialCenter()
     self.social_center.add_site(self.TEST_SITE, self.test_bot)
     self.social_center.add_site(self.TEST_PAGED_SITE, self.test_paged_bot)
     self.social_center.authenticate(self.TEST_SITE, "test_main_token",
                                     "test_sub_token")
     self.social_center.authenticate(self.TEST_PAGED_SITE,
                                     "test_main_token", "test_sub_token")
Exemplo n.º 6
0
class SocialCenterExposeTestCase(TestCase):
    """Test Case 3: Social Center Must Expose Social Network Properties
    """
    TEST_SITE = "test"
    
    @classmethod
    def setUpClass(self):
        self.test_bot = TestBot()
        self.social_center = SocialCenter()
        self.social_center.add_site(self.TEST_SITE, self.test_bot)
    
    @classmethod
    def tearDownClass(self):
        self.social_center.remove_site(self.TEST_SITE)
        del self.social_center
        del self.test_bot
    
    def test_returns_bot_properties(self):
        #import pdb; pdb.set_trace()
        self.assertEqual(self.social_center.must_select_page(self.TEST_SITE),
                         self.test_bot.must_select_page())
        
        self.assertEqual(self.social_center.get_site_name(self.TEST_SITE),
                         self.test_bot.get_site_name())
        
        self.assertEqual(self.social_center.get_client_token_name(self.TEST_SITE),
                         self.test_bot.get_client_token_name())
        
        self.assertEqual(self.social_center.get_account_name(self.TEST_SITE),
                         self.test_bot.get_account_name())
        
        self.assertEqual(self.social_center.get_account_url(self.TEST_SITE),
                         self.test_bot.get_account_url())
Exemplo n.º 7
0
class SocialCenterExposeTestCase(TestCase):
    """Test Case 3: Social Center Must Expose Social Network Properties
    """
    TEST_SITE = "test"

    @classmethod
    def setUpClass(self):
        self.test_bot = TestBot()
        self.social_center = SocialCenter()
        self.social_center.add_site(self.TEST_SITE, self.test_bot)

    @classmethod
    def tearDownClass(self):
        self.social_center.remove_site(self.TEST_SITE)
        del self.social_center
        del self.test_bot

    def test_returns_bot_properties(self):
        #import pdb; pdb.set_trace()
        self.assertEqual(self.social_center.must_select_page(self.TEST_SITE),
                         self.test_bot.must_select_page())

        self.assertEqual(self.social_center.get_site_name(self.TEST_SITE),
                         self.test_bot.get_site_name())

        self.assertEqual(
            self.social_center.get_client_token_name(self.TEST_SITE),
            self.test_bot.get_client_token_name())

        self.assertEqual(self.social_center.get_account_name(self.TEST_SITE),
                         self.test_bot.get_account_name())

        self.assertEqual(self.social_center.get_account_url(self.TEST_SITE),
                         self.test_bot.get_account_url())
Exemplo n.º 8
0
 def setUpClass(self):
     self.user = self.generate_user_with_permissions()
     self.test_bot = TestBot()
     self.test_paged_bot = TestBotWithPages()
     self.social_center = SocialCenter()
     self.default_site_info = self.social_center.get_sites()
     self.social_center.add_site(self.TEST_SITE, self.test_bot)
     self.social_center.add_site(self.TEST_PAGED_SITE, self.test_paged_bot)
Exemplo n.º 9
0
 def setUpClass(self):
     self.test_bot = TestBot()
     self.test_paged_bot = TestBotWithPages()
     self.social_center = SocialCenter()
     self.social_center.add_site(self.TEST_SITE, self.test_bot)
     self.social_center.add_site(self.TEST_PAGED_SITE, self.test_paged_bot)
     self.social_center.authenticate(self.TEST_SITE, "test_main_token", "test_sub_token")
     self.social_center.authenticate(self.TEST_PAGED_SITE, "test_main_token", "test_sub_token")
Exemplo n.º 10
0
class SocialCenterAddStubTestCase(TestCase):
    """Test Case 2: Social Center Must Add Stubs
    """
    TEST_SITE = "test"
    
    @classmethod
    def setUpClass(self):
        self.test_bot = TestBot()
        self.social_center = SocialCenter()
    
    @classmethod
    def tearDownClass(self):
        del self.social_center
        del self.test_bot
    
    def test_non_string_site_names(self):
        self.assertRaises(ValueError, self.social_center.add_site, 1342, TestBot())
    
    def test_non_social_bots(self):
        self.assertRaises(ValueError, self.social_center.add_site, "invalid site", 434)
    
    def test_can_add_stubs(self):
        self.social_center.add_site(self.TEST_SITE, self.test_bot)
        
        self.assertTrue(self.social_center.has_site(self.TEST_SITE))
        
        sites = self.social_center.get_sites()
        self.assertIn(self.TEST_SITE, sites)
        
        self.social_center.remove_site(self.TEST_SITE)
Exemplo n.º 11
0
class SocialCenterAddStubTestCase(TestCase):
    """Test Case 2: Social Center Must Add Stubs
    """
    TEST_SITE = "test"

    @classmethod
    def setUpClass(self):
        self.test_bot = TestBot()
        self.social_center = SocialCenter()

    @classmethod
    def tearDownClass(self):
        del self.social_center
        del self.test_bot

    def test_non_string_site_names(self):
        self.assertRaises(ValueError, self.social_center.add_site, 1342,
                          TestBot())

    def test_non_social_bots(self):
        self.assertRaises(ValueError, self.social_center.add_site,
                          "invalid site", 434)

    def test_can_add_stubs(self):
        self.social_center.add_site(self.TEST_SITE, self.test_bot)

        self.assertTrue(self.social_center.has_site(self.TEST_SITE))

        sites = self.social_center.get_sites()
        self.assertIn(self.TEST_SITE, sites)

        self.social_center.remove_site(self.TEST_SITE)
Exemplo n.º 12
0
 def setUpClass(self):
     self.test_bot = TestBot()
     self.social_center = SocialCenter()
Exemplo n.º 13
0
class RequestTestCase(TestCase):
    """Test Case 9: Social Center Views Must Render
    """
    TEST_SITE = "test"
    TEST_PAGED_SITE = "test_paged"
    
    @staticmethod
    def generate_user_with_permissions():
        admin_group = Group.objects.create(name="CMSAdmin")
        user = User.objects.create_user(username='******')
        user.groups.add(admin_group)
        # user.user_permissions.add(Permission.objects.get(codename="add_socialtoken"))
        # user.user_permissions.add(Permission.objects.get(codename="change_socialtoken"))
        # user.user_permissions.add(Permission.objects.get(codename="delete_socialtoken"))
        # user = User.objects.get(pk=user.pk)
        return user
    
    @staticmethod
    def generate_get_request(user):
        get_request = HttpRequest()
        get_request.method = "GET"
        get_request.session = {}
        get_request.user = user
        get_request.META["SERVER_NAME"] = "testapp.com"
        get_request.META["SERVER_PORT"] = "12345"
        
        return get_request
    
    @classmethod
    def setUpClass(self):
        self.user = self.generate_user_with_permissions()
        self.test_bot = TestBot()
        self.test_paged_bot = TestBotWithPages()
        self.social_center = SocialCenter()
        self.default_site_info = self.social_center.get_sites()
        self.social_center.add_site(self.TEST_SITE, self.test_bot)
        self.social_center.add_site(self.TEST_PAGED_SITE, self.test_paged_bot)
    
    @classmethod
    def tearDownClass(self):
        self.user.delete()
        self.social_center.logout(self.TEST_SITE)
        self.social_center.logout(self.TEST_PAGED_SITE)
        
        self.social_center.remove_site(self.TEST_SITE)
        self.social_center.remove_site(self.TEST_PAGED_SITE)
        
        del self.social_center
        del self.test_bot
        del self.test_paged_bot
    
    def test_social_view_has_correct_elements(self):
        sites = self.social_center.get_sites()
        for site_id, site in sites.items():
            site["auth_uri"] = "/social/%s/auth" % site_id
        expected_html = render_to_string("socialnetwork-main.html", { "sites" : sites })
        
        request = self.generate_get_request(self.user)
        request.method = "POST"
        invalid_response = sn_views.social(request)
        request.method = "GET"
        valid_response = sn_views.social(request)
        
        self.assertNotEqual(invalid_response.content.decode(), expected_html)
        self.assertEqual(valid_response.content.decode(), expected_html)
        
        # Test view changes after one site has been authenticated
        self.social_center.authenticate(self.TEST_SITE, "main_token", "sub_token")
        updated_sites = self.social_center.get_sites()
        for site_id, site in updated_sites.items():
            site["auth_uri"] = "/social/%s/auth" % site_id
        updated_sites[self.TEST_SITE]["logout_uri"] = "/social/%s/logout" % self.TEST_SITE
        
        request.method = "GET"
        updated_valid_response = sn_views.social(request)
        updated_expected_html = render_to_string("socialnetwork-main.html", { "sites" : updated_sites })
        self.social_center.logout(self.TEST_SITE)
        self.assertEqual(updated_valid_response.content.decode(), updated_expected_html)
    
    def test_social_test_view_has_correct_elements(self):
        expected_html = render_to_string("socialnetwork-post-test.html", { "social_post_uri" : "/social/post" })
        
        request = self.generate_get_request(self.user)
        request.method = "POST"
        invalid_response = sn_views.social_test(request)
        request.method = "GET"
        valid_response = sn_views.social_test(request)
        
        self.assertNotEqual(invalid_response.content.decode(), expected_html)
        self.assertEqual(valid_response.content.decode(), expected_html)
    
    def test_social_auth_view_has_correct_elements(self):
        get_request = self.generate_get_request(self.user)
        
        not_found_response = sn_views.social_auth(get_request, "non_existant_site")
        self.assertEqual(not_found_response.status_code, 404)
        
        self.social_center.authenticate(self.TEST_SITE, "main_token", "sub_token")
        already_logged_in_response = sn_views.social_auth(get_request, self.TEST_SITE)
        self.assertEqual(already_logged_in_response.status_code, 302)
        self.assertEqual(already_logged_in_response.url, "/social/")
        
        self.social_center.logout(self.TEST_SITE)
        test_auth_data_key = "%s_auth_data" % self.TEST_SITE
        callback_url = get_request.build_absolute_uri("/social/%s/callback" % self.TEST_SITE)
        oauth_url, auth_data = self.social_center.start_authentication(self.TEST_SITE, callback_url)
        
        auth_response = sn_views.social_auth(get_request, self.TEST_SITE)
        self.assertIn(test_auth_data_key, get_request.session)
        self.assertEqual(get_request.session[test_auth_data_key], auth_data)
        self.assertEqual(auth_response.status_code, 302)
        self.assertEqual(auth_response.url, oauth_url)
    
    def test_social_callback_view_has_correct_elements(self):
        get_request = self.generate_get_request(self.user)
        
        not_found_response = sn_views.social_callback(get_request, "non_existant_site")
        self.assertEqual(not_found_response.status_code, 404)
        
        self.assertRaises(Exception, sn_views.social_callback, get_request, self.TEST_SITE)
        self.assertRaises(Exception, sn_views.social_callback, get_request, self.TEST_PAGED_SITE)
        
        test_client_token_name = self.test_bot.get_client_token_name()
        test_paged_client_token_name = self.test_paged_bot.get_client_token_name()
        test_auth_data_key = "%s_auth_data" % self.TEST_SITE
        test_paged_auth_data_key = "%s_auth_data" % self.TEST_PAGED_SITE
        
        test_auth_data = self.test_bot.start_authentication("http://testapp.com/")[1]
        test_paged_auth_data = self.test_paged_bot.start_authentication("http://testapp.com/")[1]
        
        test_request = copy.deepcopy(get_request)
        
        test_request.session = { test_auth_data_key : test_auth_data}
        test_request.GET[test_client_token_name] = "invalid_code"
        test_invalid_code_response = sn_views.social_callback(test_request, self.TEST_SITE)
        self.assertEqual(test_invalid_code_response.status_code, 500)
        
        test_request.session = { test_auth_data_key : test_auth_data}
        test_request.GET[test_client_token_name] = "valid_code"
        test_valid_code_response = sn_views.social_callback(test_request, self.TEST_SITE)
        self.assertEqual(test_valid_code_response.status_code, 302)
        self.assertEqual(test_valid_code_response.url, "/social/")
        
        test_paged_request = copy.deepcopy(get_request)
        
        test_paged_request.session = { test_paged_auth_data_key : test_paged_auth_data}
        test_paged_request.GET[test_paged_client_token_name] = self.test_paged_bot._valid_client_token
        test_paged_valid_code_response = sn_views.social_callback(test_paged_request, self.TEST_PAGED_SITE)
        
        pages = self.social_center.get_pages(self.TEST_PAGED_SITE, self.test_paged_bot._valid_main_token)
        expected_html = render_to_string("socialnetwork-select-page.html", {
                "pages" : pages,
                "root_uri" : "/social/",
                "process_uri" : "/social/%s/page" % self.TEST_PAGED_SITE
            })
        
        self.assertEqual(test_paged_valid_code_response.content.decode(), expected_html)
Exemplo n.º 14
0
 def setUpClass(self):
     self.test_bot = TestBot()
     self.social_center = SocialCenter()
     self.social_center.add_site(self.TEST_SITE, self.test_bot)
Exemplo n.º 15
0
class SocialCenterPublishStubTestCase(TestCase):
    """Test Case 6: Social Center Must Publish Posts on Stub
    """
    TEST_SITE = "test"
    
    @classmethod
    def setUpClass(self):
        self.test_bot = TestBot()
        self.social_center = SocialCenter()
        self.social_center.add_site(self.TEST_SITE, self.test_bot)
    
    @classmethod
    def tearDownClass(self):
        self.social_center.logout(self.TEST_SITE)
        self.social_center.remove_site(self.TEST_SITE)
        del self.social_center
        del self.test_bot
    
    def test_cannot_post_while_logged_out(self):
        title = "Test title"
        content = "Test content"
        link = "Test link"
        
        self.assertFalse(self.social_center.is_logged_in(self.TEST_SITE))
        
        logged_out_valid_results = self.social_center.publish(title, content, link)
        logged_out_invalid_results = self.social_center.publish(None, None, None)
        
        # Results are always returned
        self.assertTrue(logged_out_valid_results)
        self.assertTrue(logged_out_invalid_results)
        
        self.assertIn(self.TEST_SITE, logged_out_valid_results)
        valid_result = logged_out_valid_results[self.TEST_SITE]
        self.assertNotIn("title", valid_result)
        self.assertFalse(valid_result["logged_in"])
        
        self.assertIn(self.TEST_SITE, logged_out_invalid_results)
        invalid_result = logged_out_invalid_results[self.TEST_SITE]
        self.assertNotIn("title", valid_result)
        self.assertFalse(valid_result["logged_in"])
    
    def test_can_post_while_logged_in(self):
        title = "Test title"
        content = "Test content"
        link = "Test link"
        
        self.social_center.authenticate(self.TEST_SITE, "main_token", "sub_token")
        #import pdb; pdb.set_trace()
        logged_in_valid_results = self.social_center.publish(title, content, link)
        logged_in_invalid_results = self.social_center.publish(None, None, None)
        
        # Results are always returned
        self.assertTrue(logged_in_valid_results)
        self.assertTrue(logged_in_invalid_results)
        
        self.assertIn(self.TEST_SITE, logged_in_valid_results)
        valid_result = logged_in_valid_results[self.TEST_SITE]
        self.assertNotIn("error", valid_result)
        self.assertEqual(valid_result["title"], title)
        self.assertEqual(valid_result["content"], content)
        self.assertEqual(valid_result["link"], link)
        
        self.assertIn(self.TEST_SITE, logged_in_invalid_results)
        invalid_result = logged_in_invalid_results[self.TEST_SITE]
        self.assertIn("error", invalid_result)
Exemplo n.º 16
0
class SocialCenterPageTestCase(TestCase):
    """Test Case 5: Social Center Must Retrieve Pages
    """
    TEST_SITE = "test"
    TEST_PAGED_SITE = "test_paged"
    
    @classmethod
    def setUpClass(self):
        self.test_bot = TestBot()
        self.test_paged_bot = TestBotWithPages()
        self.social_center = SocialCenter()
        self.social_center.add_site(self.TEST_SITE, self.test_bot)
        self.social_center.add_site(self.TEST_PAGED_SITE, self.test_paged_bot)
        self.social_center.authenticate(self.TEST_SITE, "test_main_token", "test_sub_token")
        self.social_center.authenticate(self.TEST_PAGED_SITE, "test_main_token", "test_sub_token")
    
    @classmethod
    def tearDownClass(self):
        self.social_center.logout(self.TEST_SITE)
        self.social_center.logout(self.TEST_PAGED_SITE)
        
        self.social_center.remove_site(self.TEST_SITE)
        self.social_center.remove_site(self.TEST_PAGED_SITE)
        
        del self.social_center
        del self.test_bot
        del self.test_paged_bot
    
    def test_case(self):
        self.only_paged_site_has_pages()
        self.can_get_pages()
    
    def only_paged_site_has_pages(self):
        self.assertFalse(self.social_center.must_select_page(self.TEST_SITE))
        self.assertTrue(self.social_center.must_select_page(self.TEST_PAGED_SITE))
        
        self.assertRaises(NotImplementedError, self.social_center.get_pages, self.TEST_SITE, "any_token")
        # Should not raise any errors:
        pages = self.social_center.get_pages(self.TEST_PAGED_SITE, self.test_paged_bot._valid_main_token)
    
    def can_get_pages(self):
        self.social_center.authenticate(self.TEST_PAGED_SITE, "test_main_token", "test_sub_token")
        
        invalid_pages = self.social_center.get_pages(self.TEST_PAGED_SITE, "invalid_request_token")
        self.assertFalse(invalid_pages)
        
        valid_pages = self.social_center.get_pages(self.TEST_PAGED_SITE, self.test_paged_bot._valid_main_token)
        self.assertTrue(valid_pages)
        self.assertEqual(len(valid_pages), 10)
        for i in range(10):
            page = valid_pages[i]
            self.assertEqual(page["id"], str(i+1))
            self.assertEqual(page["name"], "Page " + str(i+1))
Exemplo n.º 17
0
class SocialCenterAuthTestCase(TestCase):
    """Test Case 4: Social Center Must Authenticate Stub
    """
    TEST_SITE = "test"
    
    @classmethod
    def setUpClass(self):
        self.test_bot = TestBot()
        self.social_center = SocialCenter()
        self.social_center.add_site(self.TEST_SITE, self.test_bot)
    
    @classmethod
    def tearDownClass(self):
        self.social_center.logout(self.TEST_SITE)
        self.social_center.remove_site(self.TEST_SITE)
        del self.social_center
        del self.test_bot
    
    def test_case(self):
        self.is_initially_logged_out()
        self.can_start_auth()
        self.can_process_client_token()
        self.can_authenticate()
    
    def is_initially_logged_out(self):
        self.assertFalse(self.social_center.is_logged_in(self.TEST_SITE))
    
    def can_start_auth(self):
        callback_url = "http://testapp.com/"
        
        redirect_url, auth_data = self.social_center.start_authentication(self.TEST_SITE, callback_url)
        
        self.assertEqual(redirect_url, "http://test.com/auth?redirect_uri=%s" % urllib.quote_plus(callback_url))
        self.assertEqual(auth_data["session_token"], self.test_bot._session_token)
        self.assertEqual(auth_data["callback_url"], callback_url)
        
    def can_process_client_token(self):
        callback_url = "http://testapp2.com/"
        redirect_url, auth_data = self.social_center.start_authentication(self.TEST_SITE, callback_url)
        
        self.assertRaises(Exception, self.social_center.process_client_token, None)
        self.assertRaises(Exception, self.social_center.process_client_token, {})
        self.assertRaises(Exception, self.social_center.process_client_token, { "callback_url" : callback_url })
        
        invalid_result = self.social_center.process_client_token(self.TEST_SITE, "invalid_code", auth_data)
        
        self.assertNotIn("main_token", invalid_result)
        self.assertIn("error", invalid_result)
        
        modified_data = auth_data.copy()
        modified_data["session_token"] = "invalidSessionToken"
        modified_result = self.social_center.process_client_token(self.TEST_SITE, self.test_bot._valid_client_token, modified_data)
        
        self.assertNotIn("main_token", modified_result)
        self.assertIn("error", modified_result)
        
        valid_result = self.social_center.process_client_token(self.TEST_SITE, self.test_bot._valid_client_token, auth_data)
        
        self.assertIn("main_token", valid_result)
        self.assertNotIn("error", valid_result)
    
    def can_authenticate(self):
        self.social_center.authenticate(self.TEST_SITE, "test_main_token", "test_sub_token")
        
        self.assertEqual(self.test_bot.main_token, "test_main_token")
        self.assertEqual(self.test_bot.sub_token, "test_sub_token")
        self.assertTrue(self.social_center.is_logged_in(self.TEST_SITE))
Exemplo n.º 18
0
class SocialCenterAuthTestCase(TestCase):
    """Test Case 4: Social Center Must Authenticate Stub
    """
    TEST_SITE = "test"

    @classmethod
    def setUpClass(self):
        self.test_bot = TestBot()
        self.social_center = SocialCenter()
        self.social_center.add_site(self.TEST_SITE, self.test_bot)

    @classmethod
    def tearDownClass(self):
        self.social_center.logout(self.TEST_SITE)
        self.social_center.remove_site(self.TEST_SITE)
        del self.social_center
        del self.test_bot

    def test_case(self):
        self.is_initially_logged_out()
        self.can_start_auth()
        self.can_process_client_token()
        self.can_authenticate()

    def is_initially_logged_out(self):
        self.assertFalse(self.social_center.is_logged_in(self.TEST_SITE))

    def can_start_auth(self):
        callback_url = "http://testapp.com/"

        redirect_url, auth_data = self.social_center.start_authentication(
            self.TEST_SITE, callback_url)

        self.assertEqual(
            redirect_url, "http://test.com/auth?redirect_uri=%s" %
            urllib.quote_plus(callback_url))
        self.assertEqual(auth_data["session_token"],
                         self.test_bot._session_token)
        self.assertEqual(auth_data["callback_url"], callback_url)

    def can_process_client_token(self):
        callback_url = "http://testapp2.com/"
        redirect_url, auth_data = self.social_center.start_authentication(
            self.TEST_SITE, callback_url)

        self.assertRaises(Exception, self.social_center.process_client_token,
                          None)
        self.assertRaises(Exception, self.social_center.process_client_token,
                          {})
        self.assertRaises(Exception, self.social_center.process_client_token,
                          {"callback_url": callback_url})

        invalid_result = self.social_center.process_client_token(
            self.TEST_SITE, "invalid_code", auth_data)

        self.assertNotIn("main_token", invalid_result)
        self.assertIn("error", invalid_result)

        modified_data = auth_data.copy()
        modified_data["session_token"] = "invalidSessionToken"
        modified_result = self.social_center.process_client_token(
            self.TEST_SITE, self.test_bot._valid_client_token, modified_data)

        self.assertNotIn("main_token", modified_result)
        self.assertIn("error", modified_result)

        valid_result = self.social_center.process_client_token(
            self.TEST_SITE, self.test_bot._valid_client_token, auth_data)

        self.assertIn("main_token", valid_result)
        self.assertNotIn("error", valid_result)

    def can_authenticate(self):
        self.social_center.authenticate(self.TEST_SITE, "test_main_token",
                                        "test_sub_token")

        self.assertEqual(self.test_bot.main_token, "test_main_token")
        self.assertEqual(self.test_bot.sub_token, "test_sub_token")
        self.assertTrue(self.social_center.is_logged_in(self.TEST_SITE))
Exemplo n.º 19
0
 def setUpClass(self):
     self.test_bot = TestBot()
     self.social_center = SocialCenter()
Exemplo n.º 20
0
class SocialCenterPageTestCase(TestCase):
    """Test Case 5: Social Center Must Retrieve Pages
    """
    TEST_SITE = "test"
    TEST_PAGED_SITE = "test_paged"

    @classmethod
    def setUpClass(self):
        self.test_bot = TestBot()
        self.test_paged_bot = TestBotWithPages()
        self.social_center = SocialCenter()
        self.social_center.add_site(self.TEST_SITE, self.test_bot)
        self.social_center.add_site(self.TEST_PAGED_SITE, self.test_paged_bot)
        self.social_center.authenticate(self.TEST_SITE, "test_main_token",
                                        "test_sub_token")
        self.social_center.authenticate(self.TEST_PAGED_SITE,
                                        "test_main_token", "test_sub_token")

    @classmethod
    def tearDownClass(self):
        self.social_center.logout(self.TEST_SITE)
        self.social_center.logout(self.TEST_PAGED_SITE)

        self.social_center.remove_site(self.TEST_SITE)
        self.social_center.remove_site(self.TEST_PAGED_SITE)

        del self.social_center
        del self.test_bot
        del self.test_paged_bot

    def test_case(self):
        self.only_paged_site_has_pages()
        self.can_get_pages()

    def only_paged_site_has_pages(self):
        self.assertFalse(self.social_center.must_select_page(self.TEST_SITE))
        self.assertTrue(
            self.social_center.must_select_page(self.TEST_PAGED_SITE))

        self.assertRaises(NotImplementedError, self.social_center.get_pages,
                          self.TEST_SITE, "any_token")
        # Should not raise any errors:
        pages = self.social_center.get_pages(
            self.TEST_PAGED_SITE, self.test_paged_bot._valid_main_token)

    def can_get_pages(self):
        self.social_center.authenticate(self.TEST_PAGED_SITE,
                                        "test_main_token", "test_sub_token")

        invalid_pages = self.social_center.get_pages(self.TEST_PAGED_SITE,
                                                     "invalid_request_token")
        self.assertFalse(invalid_pages)

        valid_pages = self.social_center.get_pages(
            self.TEST_PAGED_SITE, self.test_paged_bot._valid_main_token)
        self.assertTrue(valid_pages)
        self.assertEqual(len(valid_pages), 10)
        for i in range(10):
            page = valid_pages[i]
            self.assertEqual(page["id"], str(i + 1))
            self.assertEqual(page["name"], "Page " + str(i + 1))
Exemplo n.º 21
0
class RequestTestCase(TestCase):
    """Test Case 9: Social Center Views Must Render
    """
    TEST_SITE = "test"
    TEST_PAGED_SITE = "test_paged"

    @staticmethod
    def generate_user_with_permissions():
        admin_group = Group.objects.create(name="CMSAdmin")
        user = User.objects.create_user(username='******')
        user.groups.add(admin_group)
        # user.user_permissions.add(Permission.objects.get(codename="add_socialtoken"))
        # user.user_permissions.add(Permission.objects.get(codename="change_socialtoken"))
        # user.user_permissions.add(Permission.objects.get(codename="delete_socialtoken"))
        # user = User.objects.get(pk=user.pk)
        return user

    @staticmethod
    def generate_get_request(user):
        get_request = HttpRequest()
        get_request.method = "GET"
        get_request.session = {}
        get_request.user = user
        get_request.META["SERVER_NAME"] = "testapp.com"
        get_request.META["SERVER_PORT"] = "12345"

        return get_request

    @classmethod
    def setUpClass(self):
        self.user = self.generate_user_with_permissions()
        self.test_bot = TestBot()
        self.test_paged_bot = TestBotWithPages()
        self.social_center = SocialCenter()
        self.default_site_info = self.social_center.get_sites()
        self.social_center.add_site(self.TEST_SITE, self.test_bot)
        self.social_center.add_site(self.TEST_PAGED_SITE, self.test_paged_bot)

    @classmethod
    def tearDownClass(self):
        self.user.delete()
        self.social_center.logout(self.TEST_SITE)
        self.social_center.logout(self.TEST_PAGED_SITE)

        self.social_center.remove_site(self.TEST_SITE)
        self.social_center.remove_site(self.TEST_PAGED_SITE)

        del self.social_center
        del self.test_bot
        del self.test_paged_bot

    def test_social_view_has_correct_elements(self):
        sites = self.social_center.get_sites()
        for site_id, site in sites.items():
            site["auth_uri"] = "/social/%s/auth" % site_id
        expected_html = render_to_string("socialnetwork-main.html",
                                         {"sites": sites})

        request = self.generate_get_request(self.user)
        request.method = "POST"
        invalid_response = sn_views.social(request)
        request.method = "GET"
        valid_response = sn_views.social(request)

        self.assertNotEqual(invalid_response.content.decode(), expected_html)
        self.assertEqual(valid_response.content.decode(), expected_html)

        # Test view changes after one site has been authenticated
        self.social_center.authenticate(self.TEST_SITE, "main_token",
                                        "sub_token")
        updated_sites = self.social_center.get_sites()
        for site_id, site in updated_sites.items():
            site["auth_uri"] = "/social/%s/auth" % site_id
        updated_sites[self.TEST_SITE][
            "logout_uri"] = "/social/%s/logout" % self.TEST_SITE

        request.method = "GET"
        updated_valid_response = sn_views.social(request)
        updated_expected_html = render_to_string("socialnetwork-main.html",
                                                 {"sites": updated_sites})
        self.social_center.logout(self.TEST_SITE)
        self.assertEqual(updated_valid_response.content.decode(),
                         updated_expected_html)

    def test_social_test_view_has_correct_elements(self):
        expected_html = render_to_string("socialnetwork-post-test.html",
                                         {"social_post_uri": "/social/post"})

        request = self.generate_get_request(self.user)
        request.method = "POST"
        invalid_response = sn_views.social_test(request)
        request.method = "GET"
        valid_response = sn_views.social_test(request)

        self.assertNotEqual(invalid_response.content.decode(), expected_html)
        self.assertEqual(valid_response.content.decode(), expected_html)

    def test_social_auth_view_has_correct_elements(self):
        get_request = self.generate_get_request(self.user)

        not_found_response = sn_views.social_auth(get_request,
                                                  "non_existant_site")
        self.assertEqual(not_found_response.status_code, 404)

        self.social_center.authenticate(self.TEST_SITE, "main_token",
                                        "sub_token")
        already_logged_in_response = sn_views.social_auth(
            get_request, self.TEST_SITE)
        self.assertEqual(already_logged_in_response.status_code, 302)
        self.assertEqual(already_logged_in_response.url, "/social/")

        self.social_center.logout(self.TEST_SITE)
        test_auth_data_key = "%s_auth_data" % self.TEST_SITE
        callback_url = get_request.build_absolute_uri("/social/%s/callback" %
                                                      self.TEST_SITE)
        oauth_url, auth_data = self.social_center.start_authentication(
            self.TEST_SITE, callback_url)

        auth_response = sn_views.social_auth(get_request, self.TEST_SITE)
        self.assertIn(test_auth_data_key, get_request.session)
        self.assertEqual(get_request.session[test_auth_data_key], auth_data)
        self.assertEqual(auth_response.status_code, 302)
        self.assertEqual(auth_response.url, oauth_url)

    def test_social_callback_view_has_correct_elements(self):
        get_request = self.generate_get_request(self.user)

        not_found_response = sn_views.social_callback(get_request,
                                                      "non_existant_site")
        self.assertEqual(not_found_response.status_code, 404)

        self.assertRaises(Exception, sn_views.social_callback, get_request,
                          self.TEST_SITE)
        self.assertRaises(Exception, sn_views.social_callback, get_request,
                          self.TEST_PAGED_SITE)

        test_client_token_name = self.test_bot.get_client_token_name()
        test_paged_client_token_name = self.test_paged_bot.get_client_token_name(
        )
        test_auth_data_key = "%s_auth_data" % self.TEST_SITE
        test_paged_auth_data_key = "%s_auth_data" % self.TEST_PAGED_SITE

        test_auth_data = self.test_bot.start_authentication(
            "http://testapp.com/")[1]
        test_paged_auth_data = self.test_paged_bot.start_authentication(
            "http://testapp.com/")[1]

        test_request = copy.deepcopy(get_request)

        test_request.session = {test_auth_data_key: test_auth_data}
        test_request.GET[test_client_token_name] = "invalid_code"
        test_invalid_code_response = sn_views.social_callback(
            test_request, self.TEST_SITE)
        self.assertEqual(test_invalid_code_response.status_code, 500)

        test_request.session = {test_auth_data_key: test_auth_data}
        test_request.GET[test_client_token_name] = "valid_code"
        test_valid_code_response = sn_views.social_callback(
            test_request, self.TEST_SITE)
        self.assertEqual(test_valid_code_response.status_code, 302)
        self.assertEqual(test_valid_code_response.url, "/social/")

        test_paged_request = copy.deepcopy(get_request)

        test_paged_request.session = {
            test_paged_auth_data_key: test_paged_auth_data
        }
        test_paged_request.GET[
            test_paged_client_token_name] = self.test_paged_bot._valid_client_token
        test_paged_valid_code_response = sn_views.social_callback(
            test_paged_request, self.TEST_PAGED_SITE)

        pages = self.social_center.get_pages(
            self.TEST_PAGED_SITE, self.test_paged_bot._valid_main_token)
        expected_html = render_to_string(
            "socialnetwork-select-page.html", {
                "pages": pages,
                "root_uri": "/social/",
                "process_uri": "/social/%s/page" % self.TEST_PAGED_SITE
            })

        self.assertEqual(test_paged_valid_code_response.content.decode(),
                         expected_html)
Exemplo n.º 22
0
 def setUpClass(self):
     self.test_bot = TestBot()
     self.social_center = SocialCenter()
     self.social_center.add_site(self.TEST_SITE, self.test_bot)
Exemplo n.º 23
0
class SocialCenterPublishStubTestCase(TestCase):
    """Test Case 6: Social Center Must Publish Posts on Stub
    """
    TEST_SITE = "test"

    @classmethod
    def setUpClass(self):
        self.test_bot = TestBot()
        self.social_center = SocialCenter()
        self.social_center.add_site(self.TEST_SITE, self.test_bot)

    @classmethod
    def tearDownClass(self):
        self.social_center.logout(self.TEST_SITE)
        self.social_center.remove_site(self.TEST_SITE)
        del self.social_center
        del self.test_bot

    def test_cannot_post_while_logged_out(self):
        title = "Test title"
        content = "Test content"
        link = "Test link"

        self.assertFalse(self.social_center.is_logged_in(self.TEST_SITE))

        logged_out_valid_results = self.social_center.publish(
            title, content, link)
        logged_out_invalid_results = self.social_center.publish(
            None, None, None)

        # Results are always returned
        self.assertTrue(logged_out_valid_results)
        self.assertTrue(logged_out_invalid_results)

        self.assertIn(self.TEST_SITE, logged_out_valid_results)
        valid_result = logged_out_valid_results[self.TEST_SITE]
        self.assertNotIn("title", valid_result)
        self.assertFalse(valid_result["logged_in"])

        self.assertIn(self.TEST_SITE, logged_out_invalid_results)
        invalid_result = logged_out_invalid_results[self.TEST_SITE]
        self.assertNotIn("title", valid_result)
        self.assertFalse(valid_result["logged_in"])

    def test_can_post_while_logged_in(self):
        title = "Test title"
        content = "Test content"
        link = "Test link"

        self.social_center.authenticate(self.TEST_SITE, "main_token",
                                        "sub_token")
        #import pdb; pdb.set_trace()
        logged_in_valid_results = self.social_center.publish(
            title, content, link)
        logged_in_invalid_results = self.social_center.publish(
            None, None, None)

        # Results are always returned
        self.assertTrue(logged_in_valid_results)
        self.assertTrue(logged_in_invalid_results)

        self.assertIn(self.TEST_SITE, logged_in_valid_results)
        valid_result = logged_in_valid_results[self.TEST_SITE]
        self.assertNotIn("error", valid_result)
        self.assertEqual(valid_result["title"], title)
        self.assertEqual(valid_result["content"], content)
        self.assertEqual(valid_result["link"], link)

        self.assertIn(self.TEST_SITE, logged_in_invalid_results)
        invalid_result = logged_in_invalid_results[self.TEST_SITE]
        self.assertIn("error", invalid_result)