コード例 #1
0
class GraphDataTest(GraphBase):
    def setUp(self):
        self.user = MockUser()
        super(GraphDataTest, self).setUp()
        self.graph = self.create_graph(user_id=self.user.user_id(),
                                       is_public=False)
        self.url = reverse("graph_data", args=[
            self.graph.key.id(),
        ])

    @patch("oauth2client.appengine.StorageByKeyName.get",
           Mock(return_value=MockedCredentials()))
    @patch("google.appengine.api.users.get_current_user",
           Mock(return_value=MockUser()))
    def test_logged_user_should_be_able_to_access_page_even_if_the_graph_is_not_public(
            self):
        self.client.get(reverse("login"))
        response = self.client.get(self.url)
        self.assertEqual(response.status_code, 200)

    def test_anonymous_user_should_not_see_the_page_if_the_graph_is_not_public(
            self):
        response = self.client.get(self.url)
        self.assertEqual(response.status_code, 403)

    def user_should_see_the_page_if_the_graph_is_public(self):
        response = self.client.get(self.url)
        self.assertEqual(response.status_code, 200)

    def test_user_should_get_404_if_the_graph_does_not_exist(self):
        res = self.client.get(
            reverse('graph_standalone', kwargs={'graph_id': 9999999}))
        self.assertEqual(res.status_code, 404)
コード例 #2
0
 def setUp(self):
     self.user = MockUser()
     super(GraphDataTest, self).setUp()
     self.graph = self.create_graph(user_id=self.user.user_id(),
                                    is_public=False)
     self.url = reverse("graph_data", args=[
         self.graph.key.id(),
     ])
コード例 #3
0
class GraphCreateTest(GraphBase):

    def setUp(self):
        super(GraphCreateTest, self).setUp()
        self.url = reverse("graph_create")
        self.user = MockUser()

    @patch("google.appengine.api.users.get_current_user", Mock(return_value=MockUser()))
    @patch("oauth2client.appengine.StorageByKeyName.get", Mock(return_value=MockedCredentials()))
    def test_logged_user_should_be_able_to_see_the_page(self):
        self.client.get(reverse("login"))
        response = self.client.get(self.url)
        self.assertEqual(response.status_code, 200)

    def test_anonymous_user_should_be_redirected_to_login(self):
        response = self.client.get(self.url)
        self.assertEqual(response.status_code, 302)
        self.assertTrue(str(response).endswith('http://testserver/login\n\n'))

    def test_anonymous_user_should_not_create_a_graph(self):
        response = self.client.post(self.url, data={'name':'Anonymous', 'is_public': True})
        self.assertEqual(response.status_code, 302)
        self.assertTrue(str(response).endswith('http://testserver/login\n\n'))

    @patch("oauth2client.appengine.StorageByKeyName.get", Mock(return_value=MockedCredentials()))
    @patch("google.appengine.api.users.get_current_user")
    def test_logged_user_should_be_able_to_crate_graph_if_graph_already_exists(self, mocked_user):
        mocked_user.return_value = self.user
        self.create_graph(user_id=self.user.user_id())
        self.client.get(reverse("login"))
        response = self.client.get(reverse("graph_create"))
        self.assertEqual(response.status_code, 200)

    @patch("oauth2client.appengine.StorageByKeyName.get", Mock(return_value=MockedCredentials()))
    @patch("google.appengine.api.users.get_current_user")
    def test_logged_user_should_be_able_to_create_a_graph(self, mocked_user):
        mocked_user.return_value = self.user
        self.client.get(reverse("login"))
        self.assertFalse(Graph.query(Graph.user_id == self.user.user_id()).get())
        post_data = {'is_public': False, 'name': 'Test', 'spreadsheet_id': '123'}
        response = self.client.post(reverse("graph_create"), post_data)
        self.assertEqual(response.status_code, 302)
        new_graph = Graph.query(Graph.user_id == self.user.user_id()).get()
        self.assertTrue(new_graph)

    @patch("oauth2client.appengine.StorageByKeyName.get", Mock(return_value=MockedCredentials()))
    @patch("google.appengine.api.users.get_current_user")
    def test_logged_user_should_not_be_able_to_create_a_graph_with_wrong_data(self, mocked_user):
        mocked_user.return_value = self.user
        self.client.get(reverse("login"))
        post_data = {}
        response = self.client.post(self.url, post_data)
        self.assertEqual(response.status_code, 200)
        new_graph = Graph.query(Graph.user_id == self.user.user_id()).get()
        self.assertFalse(new_graph)
コード例 #4
0
class GraphUpdateTest(GraphBase):

    def setUp(self):
        self.user = MockUser()
        super(GraphUpdateTest, self).setUp()
        self.graph = self.create_graph(user_id=self.user.user_id(), is_public=False)
        self.url = reverse("graph_update", args=[self.graph.key.id(), ])

    @patch("oauth2client.appengine.StorageByKeyName.get", Mock(return_value=MockedCredentials()))
    @patch("google.appengine.api.users.get_current_user", Mock(return_value=MockUser()))
    def test_logged_user_should_be_able_to_see_the_page(self):
        self.client.get(reverse("login"))
        response = self.client.get(self.url)
        self.assertEqual(response.status_code, 200)

    def test_anonymous_user_should_be_redirected_to_login(self):
        response = self.client.get(self.url)
        self.assertEqual(response.status_code, 302)
        self.assertTrue(str(response).endswith('http://testserver/login\n\n'))

    @patch("oauth2client.appengine.StorageByKeyName.get", Mock(return_value=MockedCredentials()))
    @patch("google.appengine.api.users.get_current_user", Mock(return_value=MockUser()))
    def test_logged_user_should_not_be_able_to_see_other_user_graph_forms(self):
        self.client.get(reverse("login"))
        response = self.client.get(reverse('graph_update', args=[self.other_public_graph.key.id()]))
        self.assertEqual(response.status_code, 403)

    @patch("oauth2client.appengine.StorageByKeyName.get", Mock(return_value=MockedCredentials()))
    @patch("google.appengine.api.users.get_current_user", Mock(return_value=MockUser()))
    def test_logged_user_should_not_be_able_to_update_other_user_graphs(self):
        self.client.get(reverse("login"))
        response = self.client.post(reverse('graph_update', args=[self.other_public_graph.key.id()]), post_data={})
        self.assertEqual(response.status_code, 403)

    @patch("oauth2client.appengine.StorageByKeyName.get", Mock(return_value=MockedCredentials()))
    @patch("google.appengine.api.users.get_current_user")
    def test_logged_user_should_not_be_able_to_update_a_graph_with_wrong_data(self, mocked_user):
        mocked_user.return_value = self.user
        self.client.get(reverse("login"))
        is_public_value = self.graph.is_public
        post_data = {'is_public': not is_public_value}
        response = self.client.post(self.url, post_data)
        self.assertEqual(response.status_code, 200)
        graph_not_modified = Graph.query(Graph.user_id == self.user.user_id()).get()
        self.assertEqual(graph_not_modified.is_public, is_public_value)

    @patch("oauth2client.appengine.StorageByKeyName.get", Mock(return_value=MockedCredentials()))
    @patch("google.appengine.api.users.get_current_user", Mock(return_value=MockUser()))
    def test_logged_user_should_get_404_if_the_graph_does_not_exist(self):
        self.client.get(reverse("login"))
        res = self.client.get(reverse('graph_update', kwargs={'graph_id': 9999999}))
        self.assertEqual(res.status_code, 404)
コード例 #5
0
class GraphDataTest(GraphBase):

    def setUp(self):
        self.user = MockUser()
        super(GraphDataTest, self).setUp()
        self.graph = self.create_graph(user_id=self.user.user_id(), is_public=False)
        self.url = reverse("graph_data", args=[self.graph.key.id(), ])

    @patch("oauth2client.appengine.StorageByKeyName.get", Mock(return_value=MockedCredentials()))
    @patch("google.appengine.api.users.get_current_user", Mock(return_value=MockUser()))
    def test_logged_user_should_be_able_to_access_page_even_if_the_graph_is_not_public(self):
        self.client.get(reverse("login"))
        response = self.client.get(self.url)
        self.assertEqual(response.status_code, 200)

    def test_anonymous_user_should_not_see_the_page_if_the_graph_is_not_public(self):
        response = self.client.get(self.url)
        self.assertEqual(response.status_code, 403)

    def user_should_see_the_page_if_the_graph_is_public(self):
        response = self.client.get(self.url)
        self.assertEqual(response.status_code, 200)

    def test_user_should_get_404_if_the_graph_does_not_exist(self):
        res = self.client.get(reverse('graph_standalone', kwargs={'graph_id': 9999999}))
        self.assertEqual(res.status_code, 404)
コード例 #6
0
class SpreadsheetsLoadingOptionsFormTestCase(BaseAppengineTestCase):
    def setUp(self):
        super(SpreadsheetsLoadingOptionsFormTestCase, self).setUp()
        self.client = Client()

    @patch("google.appengine.api.users.get_current_user",
           Mock(return_value=MockUser()))
    @patch("oauth2client.appengine.StorageByKeyName.get",
           Mock(return_value=MockedCredentials()))
    def test_fetch_options(self):
        mock = Mock()
        mock.configure_mock(
            **{
                "files.return_value.list.return_value.execute.return_value":
                GDRIVE_LIST_RESPONSE
            })

        with patch("clients.drive.build", return_value=mock):
            response = self.client.get(reverse("spreadsheet_list"))
            self.assertTrue(response.status_code, 200)

            # Only 3 inputs
            self.assertTrue(
                len([
                    m.start() for m in re.finditer(
                        '<input type="radio" name="spreadsheet_id"',
                        response.content)
                ]), 3)
            # Only 3 anchors
            self.assertTrue(
                len([
                    m.start() for m in re.finditer(
                        '<a href="http://example.com/spreadsheet/00[123]" target="_blank">Link</a>',
                        response.content)
                ]), 3)

            self.assertIn("Spreadsheet 001", response.content)
            self.assertIn("Spreadsheet 002", response.content)
            self.assertIn("Spreadsheet 003", response.content)
コード例 #7
0
 def setUp(self):
     self.user = MockUser()
     super(GraphDataTest, self).setUp()
     self.graph = self.create_graph(user_id=self.user.user_id(), is_public=False)
     self.url = reverse("graph_data", args=[self.graph.key.id(), ])
コード例 #8
0
 def setUp(self):
     super(TestGenerateNodesThroughSpreadsheet, self).setUp()
     self.user = MockUser()
     self.graph = self.create_graph(user_id=self.user.user_id(),
                                    is_public=True)
コード例 #9
0
class TestGenerateNodesThroughSpreadsheet(GraphBase):
    def setUp(self):
        super(TestGenerateNodesThroughSpreadsheet, self).setUp()
        self.user = MockUser()
        self.graph = self.create_graph(user_id=self.user.user_id(),
                                       is_public=True)

    @patch("clients.spreadsheets.SimpleSpreadsheetsClient.get_nodes",
           Mock(return_value=GOOD_NODES))
    @patch("clients.spreadsheets.SimpleSpreadsheetsClient.GetCategories",
           Mock(return_value=FETCHED_CATEGORIES))
    @patch("clients.spreadsheets.SimpleSpreadsheetsClient.get_styles",
           Mock(return_value=FETCHED_STYLES))
    def test_generate_nodes_with_good_data(self):
        self.assertEqual(0, ErrorLog.query().count())
        self.assertEqual(0, Node.query().count())
        self.assertEqual(0, Style.query().count())
        generate_nodes_through_spreadsheet(graph=self.graph)
        self.assertEqual(len(GOOD_NODES),
                         Node.query(Node.is_category == True).count())
        self.assertEqual(len(FETCHED_CATEGORIES),
                         Node.query(Node.is_category == False).count())
        self.assertEqual(1, Style.query(Style.graph == self.graph.key).count())
        self.assertEqual(0, ErrorLog.query().count())

        # now let's check ALL THE DATA
        node_1 = Node.query(Node.name == "Bruce Dickinson").get()
        self.assertEqual(node_1.name, "Bruce Dickinson")
        self.assertEqual(
            ','.join([category.get().name for category in node_1.categories]),
            "Metal")
        self.assertEqual(node_1.importance, 5)
        self.assertEqual(node_1.short_description, None)
        self.assertEqual(node_1.long_description, None)
        self.assertEqual(node_1.context_url, None)
        self.assertEqual(node_1.credit, None)
        self.assertEqual(node_1.node_style, None)
        self.assertEqual(node_1.label_style, None)

        node_2 = Node.query(Node.name == "Rob Halford").get()
        self.assertEqual(node_2.name, "Rob Halford")
        self.assertEqual(
            ','.join([category.get().name for category in node_2.categories]),
            "Metal")
        self.assertEqual(node_2.importance, 6)
        self.assertEqual(node_2.short_description, "Judas Priest's singer")
        self.assertEqual(node_2.long_description, None)
        self.assertEqual(node_2.context_url, None)
        self.assertEqual(node_2.credit, None)
        self.assertEqual(node_2.node_style, None)
        self.assertEqual(node_2.label_style, None)

        node_3 = Node.query(Node.name == "Freddie Mercury").get()
        self.assertEqual(node_3.name, "Freddie Mercury")
        self.assertEqual(
            ','.join([category.get().name for category in node_3.categories]),
            "Pop,Rock")
        self.assertEqual(node_3.importance, 10)
        self.assertEqual(node_3.short_description, None)
        self.assertEqual(node_3.long_description, None)
        self.assertEqual(node_3.context_url, "http://www.freddiemercury.com/")
        self.assertEqual(node_3.credit, "Foo")
        self.assertEqual(node_3.node_style, "a-style")
        self.assertEqual(node_3.label_style, "b-style")

        style = Style.query(Style.graph == self.graph.key).get()
        self.assertEqual(
            style.styles,
            '.a-class { font-size: 10px; font-family: verdana }\n.b-class { border: 1px solid green; }'
        )

    @patch("clients.spreadsheets.SimpleSpreadsheetsClient.get_nodes",
           Mock(return_value=ALL_NODES))
    @patch("clients.spreadsheets.SimpleSpreadsheetsClient.GetCategories",
           Mock(return_value=FETCHED_CATEGORIES))
    @patch("clients.spreadsheets.SimpleSpreadsheetsClient.get_styles",
           Mock(return_value=FETCHED_STYLES))
    def test_generate_nodes_with_good_and_bad_data(self):
        self.assertEqual(0, ErrorLog.query().count())
        self.assertEqual(0, Node.query().count())
        generate_nodes_through_spreadsheet(graph=self.graph)
        self.assertEqual(len(GOOD_NODES),
                         Node.query(Node.is_category == True).count())
        self.assertEqual(len(FETCHED_CATEGORIES),
                         Node.query(Node.is_category == False).count())
        self.assertEqual(1, Style.query(Style.graph == self.graph.key).count())
        self.assertEqual(1, ErrorLog.query().count())

    @patch("clients.spreadsheets.SimpleSpreadsheetsClient.get_nodes",
           Mock(return_value=GOOD_NODES))
    @patch("clients.spreadsheets.SimpleSpreadsheetsClient.GetCategories",
           Mock(return_value=[]))
    @patch("clients.spreadsheets.SimpleSpreadsheetsClient.get_styles",
           Mock(return_value=FETCHED_STYLES))
    def test_try_to_generate_nodes_without_any_category(self):
        self.assertEqual(0, ErrorLog.query().count())
        self.assertEqual(0, Node.query().count())
        generate_nodes_through_spreadsheet(graph=self.graph)
        self.assertEqual(0, Node.query().count())
        self.assertEqual(1, ErrorLog.query().count())
        self.assertEqual(1, Style.query(Style.graph == self.graph.key).count())

    @patch("clients.spreadsheets.SimpleSpreadsheetsClient.get_nodes",
           Mock(return_value=GOOD_NODES))
    @patch("clients.spreadsheets.SimpleSpreadsheetsClient.GetCategories",
           Mock(return_value=FETCHED_CATEGORIES))
    @patch("clients.spreadsheets.SimpleSpreadsheetsClient.get_styles",
           Mock(return_value=FETCHED_STYLES))
    def test_all_graph_related_entities_should_be_deleted_before_updating_the_graph(
            self):
        self.assertEqual(0, ErrorLog.query().count())
        self.assertEqual(0, Node.query().count())
        self.assertEqual(0, Style.query().count())
        generate_nodes_through_spreadsheet(graph=self.graph)
        categories_before = Node.query(Node.is_category == True).count()
        nodes_before = Node.query(Node.is_category == False).count()
        style_before = Style.query(Style.graph == self.graph.key).count()
        logs_before = ErrorLog.query().count()
        generate_nodes_through_spreadsheet(graph=self.graph)
        self.assertEqual(categories_before,
                         Node.query(Node.is_category == True).count())
        self.assertEqual(nodes_before,
                         Node.query(Node.is_category == False).count())
        self.assertEqual(style_before,
                         Style.query(Style.graph == self.graph.key).count())
        self.assertEqual(logs_before, ErrorLog.query().count())
コード例 #10
0
FETCHED_STYLES = {
    'a-class': [
        'font-size: 10px;',
        'font-family: verdana',
    ],
    'b-class': ['border: 1px solid green;']
}

__all__ = [
    'TestGenerateNodesThroughSpreadsheet',
]


@patch("google.appengine.api.users.get_current_user",
       Mock(return_value=MockUser()))
@patch("oauth2client.appengine.StorageByKeyName.get",
       Mock(return_value=MockedCredentials()))
class TestGenerateNodesThroughSpreadsheet(GraphBase):
    def setUp(self):
        super(TestGenerateNodesThroughSpreadsheet, self).setUp()
        self.user = MockUser()
        self.graph = self.create_graph(user_id=self.user.user_id(),
                                       is_public=True)

    @patch("clients.spreadsheets.SimpleSpreadsheetsClient.get_nodes",
           Mock(return_value=GOOD_NODES))
    @patch("clients.spreadsheets.SimpleSpreadsheetsClient.GetCategories",
           Mock(return_value=FETCHED_CATEGORIES))
    @patch("clients.spreadsheets.SimpleSpreadsheetsClient.get_styles",
           Mock(return_value=FETCHED_STYLES))
コード例 #11
0
 def setUp(self):
     super(TestGenerateNodesThroughSpreadsheet, self).setUp()
     self.user = MockUser()
     self.graph = self.create_graph(user_id=self.user.user_id(), is_public=True)
コード例 #12
0
class GraphCreateTest(GraphBase):
    def setUp(self):
        super(GraphCreateTest, self).setUp()
        self.url = reverse("graph_create")
        self.user = MockUser()

    @patch("google.appengine.api.users.get_current_user",
           Mock(return_value=MockUser()))
    @patch("oauth2client.appengine.StorageByKeyName.get",
           Mock(return_value=MockedCredentials()))
    def test_logged_user_should_be_able_to_see_the_page(self):
        self.client.get(reverse("login"))
        response = self.client.get(self.url)
        self.assertEqual(response.status_code, 200)

    def test_anonymous_user_should_be_redirected_to_login(self):
        response = self.client.get(self.url)
        self.assertEqual(response.status_code, 302)
        self.assertTrue(str(response).endswith('http://testserver/login\n\n'))

    def test_anonymous_user_should_not_create_a_graph(self):
        response = self.client.post(self.url,
                                    data={
                                        'name': 'Anonymous',
                                        'is_public': True
                                    })
        self.assertEqual(response.status_code, 302)
        self.assertTrue(str(response).endswith('http://testserver/login\n\n'))

    @patch("oauth2client.appengine.StorageByKeyName.get",
           Mock(return_value=MockedCredentials()))
    @patch("google.appengine.api.users.get_current_user")
    def test_logged_user_should_be_able_to_crate_graph_if_graph_already_exists(
            self, mocked_user):
        mocked_user.return_value = self.user
        self.create_graph(user_id=self.user.user_id())
        self.client.get(reverse("login"))
        response = self.client.get(reverse("graph_create"))
        self.assertEqual(response.status_code, 200)

    @patch("oauth2client.appengine.StorageByKeyName.get",
           Mock(return_value=MockedCredentials()))
    @patch("google.appengine.api.users.get_current_user")
    def test_logged_user_should_be_able_to_create_a_graph(self, mocked_user):
        mocked_user.return_value = self.user
        self.client.get(reverse("login"))
        self.assertFalse(
            Graph.query(Graph.user_id == self.user.user_id()).get())
        post_data = {
            'is_public': False,
            'name': 'Test',
            'spreadsheet_id': '123'
        }
        response = self.client.post(reverse("graph_create"), post_data)
        self.assertEqual(response.status_code, 302)
        new_graph = Graph.query(Graph.user_id == self.user.user_id()).get()
        self.assertTrue(new_graph)

    @patch("oauth2client.appengine.StorageByKeyName.get",
           Mock(return_value=MockedCredentials()))
    @patch("google.appengine.api.users.get_current_user")
    def test_logged_user_should_not_be_able_to_create_a_graph_with_wrong_data(
            self, mocked_user):
        mocked_user.return_value = self.user
        self.client.get(reverse("login"))
        post_data = {}
        response = self.client.post(self.url, post_data)
        self.assertEqual(response.status_code, 200)
        new_graph = Graph.query(Graph.user_id == self.user.user_id()).get()
        self.assertFalse(new_graph)
コード例 #13
0
class GraphUpdateTest(GraphBase):
    def setUp(self):
        self.user = MockUser()
        super(GraphUpdateTest, self).setUp()
        self.graph = self.create_graph(user_id=self.user.user_id(),
                                       is_public=False)
        self.url = reverse("graph_update", args=[
            self.graph.key.id(),
        ])

    @patch("oauth2client.appengine.StorageByKeyName.get",
           Mock(return_value=MockedCredentials()))
    @patch("google.appengine.api.users.get_current_user",
           Mock(return_value=MockUser()))
    def test_logged_user_should_be_able_to_see_the_page(self):
        self.client.get(reverse("login"))
        response = self.client.get(self.url)
        self.assertEqual(response.status_code, 200)

    def test_anonymous_user_should_be_redirected_to_login(self):
        response = self.client.get(self.url)
        self.assertEqual(response.status_code, 302)
        self.assertTrue(str(response).endswith('http://testserver/login\n\n'))

    @patch("oauth2client.appengine.StorageByKeyName.get",
           Mock(return_value=MockedCredentials()))
    @patch("google.appengine.api.users.get_current_user",
           Mock(return_value=MockUser()))
    def test_logged_user_should_not_be_able_to_see_other_user_graph_forms(
            self):
        self.client.get(reverse("login"))
        response = self.client.get(
            reverse('graph_update', args=[self.other_public_graph.key.id()]))
        self.assertEqual(response.status_code, 403)

    @patch("oauth2client.appengine.StorageByKeyName.get",
           Mock(return_value=MockedCredentials()))
    @patch("google.appengine.api.users.get_current_user",
           Mock(return_value=MockUser()))
    def test_logged_user_should_not_be_able_to_update_other_user_graphs(self):
        self.client.get(reverse("login"))
        response = self.client.post(reverse(
            'graph_update', args=[self.other_public_graph.key.id()]),
                                    post_data={})
        self.assertEqual(response.status_code, 403)

    @patch("oauth2client.appengine.StorageByKeyName.get",
           Mock(return_value=MockedCredentials()))
    @patch("google.appengine.api.users.get_current_user")
    def test_logged_user_should_not_be_able_to_update_a_graph_with_wrong_data(
            self, mocked_user):
        mocked_user.return_value = self.user
        self.client.get(reverse("login"))
        is_public_value = self.graph.is_public
        post_data = {'is_public': not is_public_value}
        response = self.client.post(self.url, post_data)
        self.assertEqual(response.status_code, 200)
        graph_not_modified = Graph.query(
            Graph.user_id == self.user.user_id()).get()
        self.assertEqual(graph_not_modified.is_public, is_public_value)

    @patch("oauth2client.appengine.StorageByKeyName.get",
           Mock(return_value=MockedCredentials()))
    @patch("google.appengine.api.users.get_current_user",
           Mock(return_value=MockUser()))
    def test_logged_user_should_get_404_if_the_graph_does_not_exist(self):
        self.client.get(reverse("login"))
        res = self.client.get(
            reverse('graph_update', kwargs={'graph_id': 9999999}))
        self.assertEqual(res.status_code, 404)
コード例 #14
0
class AuthTestCase(BaseAppengineTestCase):
    def setUp(self):
        super(AuthTestCase, self).setUp()
        self.client = Client()

    def test_login_without_google_user(self):
        response = self.client.get(reverse("login"))
        self.assertEqual(response.status_code, 302)
        self.assertIn('http://testserver/_ah/login?continue=', str(response))

    @patch("google.appengine.api.users.get_current_user",
           Mock(return_value=MockUser()))
    def test_user_without_credentials(self):
        response = self.client.get(reverse("login"))
        self.assertEqual(response.status_code, 302)
        self.assertIn('http://testserver/oauth2redirect', str(response))

    @patch("google.appengine.api.users.get_current_user",
           Mock(return_value=MockUser()))
    @patch("oauth2client.appengine.StorageByKeyName.get",
           Mock(return_value=MockedCredentials()))
    def test_user_with_credentials(self):
        response = self.client.get(reverse("login"))
        self.assertEqual(response.status_code, 302)
        self.assertTrue(str(response).endswith('http://testserver/\n\n'))
        self.assertIsInstance(self.client.session["credentials"],
                              MockedCredentials)

    @patch("google.appengine.api.users.get_current_user",
           Mock(return_value=MockUser()))
    @patch("oauth2client.appengine.StorageByKeyName.get",
           Mock(return_value=MockedCredentials(True)))
    @patch("oauth2client.appengine.StorageByKeyName.put",
           Mock(return_value=lambda x: None))
    def test_user_with_expired_credentials(self):
        response = self.client.get(reverse("login"))
        self.assertEqual(response.status_code, 302)
        self.assertTrue(str(response).endswith('http://testserver/\n\n'))
        self.assertIsInstance(self.client.session["credentials"],
                              MockedCredentials)

    @patch("google.appengine.api.users.get_current_user",
           Mock(return_value=MockUser()))
    def test_user_starts_oauth2_flow(self):
        response = self.client.get(reverse("oauth2redirect"))
        self.assertEqual(response.status_code, 302)
        self.assertIn(
            'https://accounts.google.com/o/oauth2/auth?redirect_uri=',
            str(response))
        self.assertEqual(self.client.session["redirect"], "/")

    @patch("google.appengine.api.users.get_current_user",
           Mock(return_value=MockUser()))
    @patch("oauth2client.client.OAuth2WebServerFlow.step2_exchange",
           Mock(return_value=MockedCredentials()))
    @patch("oauth2client.appengine.StorageByKeyName.put",
           Mock(return_value=lambda x: None))
    def test_user_oauth2_callback(self):
        response = self.client.get(reverse("oauth2redirect"))
        response = self.client.get(reverse("oauth2callback"), {"code": "1234"})
        self.assertEqual(response.status_code, 302)
        self.assertIsInstance(self.client.session["credentials"],
                              MockedCredentials)
        self.assertTrue(str(response).endswith('http://testserver/\n\n'))

    @patch("google.appengine.api.users.get_current_user",
           Mock(return_value=MockUser()))
    def test_user_oauth2_callback_failed(self):
        response = self.client.get(reverse("oauth2redirect"))
        response = self.client.get(reverse("oauth2callback"), {"code": "1234"})
        self.assertEqual(response.status_code, 302)
        self.assertIn('http://testserver/auth-failed', str(response))
コード例 #15
0
 def setUp(self):
     super(GraphCreateTest, self).setUp()
     self.url = reverse("graph_create")
     self.user = MockUser()
コード例 #16
0
 def setUp(self):
     super(GraphCreateTest, self).setUp()
     self.url = reverse("graph_create")
     self.user = MockUser()
コード例 #17
0
class TestGenerateNodesThroughSpreadsheet(GraphBase):

    def setUp(self):
        super(TestGenerateNodesThroughSpreadsheet, self).setUp()
        self.user = MockUser()
        self.graph = self.create_graph(user_id=self.user.user_id(), is_public=True)

    @patch("clients.spreadsheets.SimpleSpreadsheetsClient.get_nodes", Mock(return_value=GOOD_NODES))
    @patch("clients.spreadsheets.SimpleSpreadsheetsClient.GetCategories", Mock(return_value=FETCHED_CATEGORIES))
    @patch("clients.spreadsheets.SimpleSpreadsheetsClient.get_styles", Mock(return_value=FETCHED_STYLES))
    def test_generate_nodes_with_good_data(self):
        self.assertEqual(0, ErrorLog.query().count())
        self.assertEqual(0, Node.query().count())
        self.assertEqual(0, Style.query().count())
        generate_nodes_through_spreadsheet(graph=self.graph)
        self.assertEqual(len(GOOD_NODES), Node.query(Node.is_category == True).count())
        self.assertEqual(len(FETCHED_CATEGORIES), Node.query(Node.is_category == False).count())
        self.assertEqual(1, Style.query(Style.graph == self.graph.key).count())
        self.assertEqual(0, ErrorLog.query().count())

        # now let's check ALL THE DATA
        node_1 = Node.query(Node.name == "Bruce Dickinson").get()
        self.assertEqual(node_1.name, "Bruce Dickinson")
        self.assertEqual(','.join([category.get().name for category in node_1.categories]), "Metal")
        self.assertEqual(node_1.importance, 5)
        self.assertEqual(node_1.short_description, None)
        self.assertEqual(node_1.long_description, None)
        self.assertEqual(node_1.context_url, None)
        self.assertEqual(node_1.credit, None)
        self.assertEqual(node_1.node_style, None)
        self.assertEqual(node_1.label_style, None)

        node_2 = Node.query(Node.name == "Rob Halford").get()
        self.assertEqual(node_2.name, "Rob Halford")
        self.assertEqual(','.join([category.get().name for category in node_2.categories]), "Metal")
        self.assertEqual(node_2.importance, 6)
        self.assertEqual(node_2.short_description, "Judas Priest's singer")
        self.assertEqual(node_2.long_description, None)
        self.assertEqual(node_2.context_url, None)
        self.assertEqual(node_2.credit, None)
        self.assertEqual(node_2.node_style, None)
        self.assertEqual(node_2.label_style, None)

        node_3 = Node.query(Node.name == "Freddie Mercury").get()
        self.assertEqual(node_3.name, "Freddie Mercury")
        self.assertEqual(','.join([category.get().name for category in node_3.categories]), "Pop,Rock")
        self.assertEqual(node_3.importance, 10)
        self.assertEqual(node_3.short_description, None)
        self.assertEqual(node_3.long_description, None)
        self.assertEqual(node_3.context_url, "http://www.freddiemercury.com/")
        self.assertEqual(node_3.credit, "Foo")
        self.assertEqual(node_3.node_style, "a-style")
        self.assertEqual(node_3.label_style, "b-style")

        style = Style.query(Style.graph == self.graph.key).get()
        self.assertEqual(
            style.styles,
            '.a-class { font-size: 10px; font-family: verdana }\n.b-class { border: 1px solid green; }'
        )

    @patch("clients.spreadsheets.SimpleSpreadsheetsClient.get_nodes", Mock(return_value=ALL_NODES))
    @patch("clients.spreadsheets.SimpleSpreadsheetsClient.GetCategories", Mock(return_value=FETCHED_CATEGORIES))
    @patch("clients.spreadsheets.SimpleSpreadsheetsClient.get_styles", Mock(return_value=FETCHED_STYLES))
    def test_generate_nodes_with_good_and_bad_data(self):
        self.assertEqual(0, ErrorLog.query().count())
        self.assertEqual(0, Node.query().count())
        generate_nodes_through_spreadsheet(graph=self.graph)
        self.assertEqual(len(GOOD_NODES), Node.query(Node.is_category == True).count())
        self.assertEqual(len(FETCHED_CATEGORIES), Node.query(Node.is_category == False).count())
        self.assertEqual(1, Style.query(Style.graph == self.graph.key).count())
        self.assertEqual(1, ErrorLog.query().count())

    @patch("clients.spreadsheets.SimpleSpreadsheetsClient.get_nodes", Mock(return_value=GOOD_NODES))
    @patch("clients.spreadsheets.SimpleSpreadsheetsClient.GetCategories", Mock(return_value=[]))
    @patch("clients.spreadsheets.SimpleSpreadsheetsClient.get_styles", Mock(return_value=FETCHED_STYLES))
    def test_try_to_generate_nodes_without_any_category(self):
        self.assertEqual(0, ErrorLog.query().count())
        self.assertEqual(0, Node.query().count())
        generate_nodes_through_spreadsheet(graph=self.graph)
        self.assertEqual(0, Node.query().count())
        self.assertEqual(1, ErrorLog.query().count())
        self.assertEqual(1, Style.query(Style.graph == self.graph.key).count())

    @patch("clients.spreadsheets.SimpleSpreadsheetsClient.get_nodes", Mock(return_value=GOOD_NODES))
    @patch("clients.spreadsheets.SimpleSpreadsheetsClient.GetCategories", Mock(return_value=FETCHED_CATEGORIES))
    @patch("clients.spreadsheets.SimpleSpreadsheetsClient.get_styles", Mock(return_value=FETCHED_STYLES))
    def test_all_graph_related_entities_should_be_deleted_before_updating_the_graph(self):
        self.assertEqual(0, ErrorLog.query().count())
        self.assertEqual(0, Node.query().count())
        self.assertEqual(0, Style.query().count())
        generate_nodes_through_spreadsheet(graph=self.graph)
        categories_before = Node.query(Node.is_category == True).count()
        nodes_before = Node.query(Node.is_category == False).count()
        style_before = Style.query(Style.graph == self.graph.key).count()
        logs_before = ErrorLog.query().count()
        generate_nodes_through_spreadsheet(graph=self.graph)
        self.assertEqual(categories_before, Node.query(Node.is_category == True).count())
        self.assertEqual(nodes_before, Node.query(Node.is_category == False).count())
        self.assertEqual(style_before, Style.query(Style.graph == self.graph.key).count())
        self.assertEqual(logs_before, ErrorLog.query().count())