コード例 #1
0
    def test_validate_related_language(self):
        assertion = {
            'type': 'Assertion',
            'id': 'http://example.com/assertion',
            'verification': {
                'type': 'HostedBadge'
            },
            'badge': 'http://example.com/badgeclass'
        }
        badgeclass = {
            'id': 'http://example.com/badgeclass',
            'type': 'BadgeClass',
            '@language': 'es',
            'issuer': 'http://example.com/issuer',
            'related': {
                'id': 'http://example.com/other_badgeclass',
                '@language': 'en-US'
            },
            'name': 'Insignia Pronto'
        }
        state = {'graph': [assertion, badgeclass]}
        task_meta = add_task(DETECT_AND_VALIDATE_NODE_CLASS,
                             node_id=badgeclass['id'])
        result, message, actions = run_task(state, task_meta)
        self.assertTrue(result)

        language_task = [
            t for t in actions if t.get('prop_name') == '@language'
        ][0]
        r, _, __ = run_task(state, language_task)
        self.assertTrue(r, "The BadgeClass's language property is valid.")

        related_task = [t for t in actions
                        if t.get('prop_name') == 'related'][0]
        result, message, actions = run_task(state, related_task)
        self.assertTrue(
            result,
            "The related property is valid and queues up task discovery for embedded node"
        )
        result, message, actions = run_task(state, actions[0])
        self.assertTrue(
            result, "Some tasks are discovered to validate the related node.")
        self.assertEqual(len(actions), 2,
                         "There are only tasks for 'id' and '@language'.")

        for a in actions:
            r, _, __ = run_task(state, a)
            self.assertTrue(r,
                            "Related node property validation is successful.")
コード例 #2
0
    def test_can_detect_and_upgrade_v_0_5(self):
        assertion_data = self.assertion_data
        state = INITIAL_STATE
        task = add_task(INTAKE_JSON, data=json.dumps(assertion_data), node_id='http://example.org/assertion')

        result, message, actions = run_task(state, task)
        self.assertTrue(result)
        version_action = [a for a in actions if a.get('type') == SET_OPENBADGES_VERSION][0]
        self.assertEqual(version_action['version'], '0.5')
        upgrade_action = [a for a in actions if a.get('name') == UPGRADE_0_5_NODE][0]

        result, message, actions = run_task(state, upgrade_action)
        self.assertTrue(result)

        self.assertEqual(len(actions), 2)
        self.assertEqual(actions[0]['name'], JSONLD_COMPACT_DATA)
コード例 #3
0
    def validate_language_prop_basic(self):
        badgeclass = {
            'id': 'http://example.org/badgeclass',
            '@language': 'en-US'
        }
        state = {'graph': [badgeclass]}
        task = add_task(VALIDATE_EXPECTED_NODE_CLASS, node_id=badgeclass['id'],
                        expected_class=OBClasses.BadgeClass)
        result, message, actions = run_task(state, task)
        self.assertTrue(result)

        l_actions = [a for a in actions if a.get('prop_name') == '@language']
        self.assertEqual(len(l_actions), 1)

        result, message, actions = run_task(state, l_actions[0])
        self.assertTrue(result)
コード例 #4
0
    def test_json_0_5_assertion_input(self):
        assertion_data = json.dumps(self.assertion_data)
        state = INITIAL_STATE
        action = store_input(assertion_data)
        state = main_reducer(state, action)
        self.assertEqual(state['input']['value'], assertion_data)

        task_meta = add_task(DETECT_INPUT_TYPE)
        result, message, actions = run_task(state, task_meta)
        self.assertTrue(result, "Task completes successfully")
        self.assertIn(REPORT_MESSAGE, [a.get('type') for a in actions])
コード例 #5
0
    def test_claim_property_validation(self):
        self.set_up_resources()

        state = {'graph': [self.endorsement]}
        task_meta = add_task(VALIDATE_EXPECTED_NODE_CLASS,
                             node_id=self.endorsement['id'],
                             prop_name='claim',
                             expected_class=OBClasses.Endorsement)

        result, message, actions = run_task(state, task_meta)
        self.assertTrue(result)
        claim_action = [a for a in actions if a.get('prop_name') == 'claim'][0]

        result, message, actions = run_task(state, claim_action)
        self.assertTrue(result)
        self.assertEqual(len(actions), 1)

        result, message, actions = run_task(state, actions[0])
        self.assertTrue(result)
        self.assertEqual(len(actions), 3)
コード例 #6
0
    def test_json_1_0_assertion_input(self):
        assertion_data = test_components['1_0_basic_assertion']
        state = INITIAL_STATE
        action = store_input(assertion_data)
        state = main_reducer(state, action)
        self.assertEqual(state['input']['value'], assertion_data)

        task_meta = add_task(DETECT_INPUT_TYPE)
        result, message, actions = run_task(state, task_meta)
        self.assertTrue(result)
        self.assertIn(SET_VALIDATION_SUBJECT, [a.get('type') for a in actions])
        self.assertIn(FETCH_HTTP_NODE, [a.get('name') for a in actions])
コード例 #7
0
    def test_can_detect_and_upgrade_v_0_5(self):
        assertion_data = {
            "recipient":
            "sha256$a4a934a0bfc882a34a3e71650e40789453b2db9799a51a2d084a64caadd72397",
            "salt": "2e2bad0df9e11272ffbcee86e4c7edd4",
            "issued_on": "2017-01-01",
            "badge": {
                "name": "Test Badge for [email protected]",
                "image": "http://example.org/image",
                "description": "Awarded using the php example codebase",
                "criteria": "http://example.org/criteria",
                "issuer": {
                    "origin": "example.org",
                    "name": "Test Issuer",
                    "org": None,
                    "contact": None
                }
            }
        }
        state = INITIAL_STATE
        task = add_task(INTAKE_JSON,
                        data=json.dumps(assertion_data),
                        node_id='http://example.org/assertion')

        result, message, actions = run_task(state, task)
        self.assertTrue(result)
        version_action = [
            a for a in actions if a.get('type') == SET_OPENBADGES_VERSION
        ][0]
        self.assertEqual(version_action['version'], '0.5')
        upgrade_action = [
            a for a in actions if a.get('name') == UPGRADE_0_5_NODE
        ][0]

        result, message, actions = run_task(state, upgrade_action)
        self.assertTrue(result)

        self.assertEqual(len(actions), 2)
        self.assertEqual(actions[0]['name'], JSONLD_COMPACT_DATA)
コード例 #8
0
    def test_fetch_task_handles_potential_baked_input(self):
        set_up_context_mock()
        assertion_url = 'http://example.org/assertion/1'
        image_url = 'http://example.org/image'

        with open(
                os.path.join(os.path.dirname(__file__), 'testfiles',
                             'public_domain_heart.png'), 'rb') as f:
            baked_file = bake(f, assertion_url)

        responses.add(responses.GET,
                      image_url,
                      body=baked_file.read(),
                      status=200,
                      content_type='image/png')

        task = add_task(FETCH_HTTP_NODE,
                        url=image_url,
                        is_potential_baked_input=True)
        result, message, actions = run_task({}, task)

        self.assertTrue(result)
        store_resource_action = [
            a for a in actions if a.get('type') == STORE_ORIGINAL_RESOURCE
        ][0]
        process_baked_input_action = [
            a for a in actions if a.get('name') == PROCESS_BAKED_RESOURCE
        ][0]

        self.assertEqual(store_resource_action.get('node_id'), image_url)
        self.assertEqual(process_baked_input_action.get('node_id'), image_url)

        task = add_task(FETCH_HTTP_NODE,
                        url=image_url,
                        is_potential_baked_input=False)
        result, message, actions = run_task({}, task)
        self.assertTrue(result)
コード例 #9
0
    def test_node_has_id_different_from_fetch_url(self):
        set_up_context_mock()

        first_url = 'http://example.org/url1'
        second_url = 'http://example.org/url2'
        node_data = {
            '@context': OPENBADGES_CONTEXT_V2_URI,
            'id': first_url,
            'name': 'Some Badge'
        }

        responses.add(responses.GET, first_url, json=node_data)
        responses.add(responses.GET, second_url, json=node_data)

        task = add_task(FETCH_HTTP_NODE, url=second_url)
        state = {'graph': []}

        result, message, actions = run_task(state, task)
        self.assertTrue(result)
        self.assertEqual(len(actions), 2)
        self.assertEqual(actions[1]['name'], INTAKE_JSON)

        result, message, actions = run_task(state, actions[1])  # INTAKE_JSON
        self.assertTrue(result)
        self.assertEqual(len(actions), 2)
        self.assertEqual(actions[1]['name'], JSONLD_COMPACT_DATA)

        result, message, actions = run_task(state,
                                            actions[1])  # JSONLD_COMPACT_DATA
        self.assertTrue(result)
        self.assertEqual(len(actions), 3)
        self.assertEqual(actions[2]['name'], FETCH_HTTP_NODE)
        self.assertEqual(actions[2]['url'], first_url)
        self.assertEqual(actions[0]['messageLevel'], MESSAGE_LEVEL_WARNING)

        # Pass 2: re-run FETCH_HTTP_NODE
        result, message, actions = run_task(state, actions[2])
        self.assertTrue(result)

        result, message, actions = run_task(state, actions[1])  # INTAKE_JSON
        self.assertTrue(result)

        result, message, actions = run_task(state,
                                            actions[1])  # JSONLD_COMPACT_DATA
        self.assertTrue(result)
        self.assertEqual(len(actions), 2)
        self.assertEqual(actions[0]['type'], ADD_NODE)
        self.assertEqual(actions[1]['name'], DETECT_AND_VALIDATE_NODE_CLASS)