Beispiel #1
0
    def test_input_json_bad_0_5(self):
        input_data = {
            "recipient":
            "sha256$ef1253755797c2a3dfd3ab455a7a2080cb9160f2f1fbbf99c475347af1ecc598",
            "issued_on": "2012-12-28",
            "badge": {
                "name": "Completed Rails for Zombies Redux",
                "image":
                "https://d1ffx7ull4987f.cloudfront.net/images/achievements/large_badge/133/completed-rails-for-zombies-redux-0f73c361c3d5070ca2fa7951e65cbf39.png",
                "description":
                "Awarded for the completion of Rails for Zombies Redux",
                "version": "0.5.0",
                "criteria":
                "https://www.codeschool.com/users/mrmickca/badges/133",
                "issuer": {
                    "origin": "http://www.codeschool.com",
                    "org": None,
                    "contact": None,
                    "name": "Code School"
                }
            },
            "salt": "6abf7e9504d73363bdcf9336056f5235",
        }
        input_string = json.dumps(input_data)
        state = INITIAL_STATE
        state['input']['value'] = input_string
        task_meta = add_task(DETECT_INPUT_TYPE)
        result, message, actions = detect_input_type(state, task_meta)
        self.assertTrue(result)
        self.assertEqual(actions[0]['input_type'], 'json')
        self.assertEqual(actions[1]['messageLevel'], MESSAGE_LEVEL_ERROR)

        # 1.0 style hosted JSON input
        input_data['verify'] = {
            "url": "http://example.org/assertion/1",
            "type": "hosted"
        }
        input_data['badge'] = 'http://example.org/badge/1'
        input_string = json.dumps(input_data)
        state['input']['value'] = input_string

        result, message, actions = detect_input_type(state, task_meta)
        self.assertTrue(result)
        self.assertEqual(actions[0]['type'], STORE_INPUT)
        self.assertEqual(actions[0]['input'], input_data['verify']['url'])
        self.assertEqual(actions[1]['input_type'], 'url')
        self.assertEqual(actions[2]['url'],
                         input_data['verify']['url'])  # FETCH_HTTP_NODE
        self.assertEqual(actions[3]['node_id'],
                         input_data['verify']['url'])  # SET_VALIDATION_SUBJECT
Beispiel #2
0
    def test_input_url_type_detection(self):
        """
        The detect_input_type task should successfully detect
        """
        url = 'http://example.com/assertionmaybe'
        state = INITIAL_STATE.copy()
        state['input']['value'] = url

        success, message, actions = detect_input_type(state)

        self.assertTrue(success)
        self.assertEqual(len(actions), 3)
        self.assertEqual(actions[0]['type'], 'SET_INPUT_TYPE')
        self.assertEqual(actions[1]['url'], url)
Beispiel #3
0
    def test_detect_jws_signed_input_type(self):
        set_up_context_mock()
        # responses.add(responses.GET, badgeclass_data['id'], json=badgeclass_data, status=200)
        # responses.add(responses.GET, issuer_data['id'], json=issuer_data, status=200)
        # responses.add(responses.GET, signing_key['id'], json=signing_key, status=200)

        state = INITIAL_STATE.copy()
        state['input']['value'] = self.signed_assertion

        success, message, actions = detect_input_type(state)

        self.assertTrue(success)
        self.assertEqual(len(actions), 2)
        self.assertEqual(actions[0]['input_type'], 'jws')
Beispiel #4
0
    def test_input_type_detection(self):
        state = INITIAL_STATE.copy()
        url = 'http://example.org/assertion/1'
        state['input'] = {'value': url}
        task_meta = add_task(DETECT_INPUT_TYPE)
        result, message, actions = detect_input_type(state, task_meta)

        fetch_action = [
            a for a in actions if a.get('name') == FETCH_HTTP_NODE
        ][0]
        set_action = [
            a for a in actions if a.get('type') == SET_VALIDATION_SUBJECT
        ][0]

        self.assertTrue(fetch_action.get('is_potential_baked_input'))
        self.assertEqual(set_action.get('node_id'), url)

        task_meta = add_task(DETECT_INPUT_TYPE, is_potential_baked_input=False)
        result, message, actions = detect_input_type(state, task_meta)
        self.assertTrue(result)
        fetch_action = [
            a for a in actions if a.get('name') == FETCH_HTTP_NODE
        ][0]
        self.assertFalse(fetch_action.get('is_potential_baked_input'))
Beispiel #5
0
    def test_input_jsonld_type_detection_preserves_json(self):
        """
        If the detect_input_type_task can't find an id field as a URL, it preserves
        the input as json
        """
        assertion_dict = json.loads(test_components['2_0_basic_assertion'])
        assertion_dict['id'] = assertion_dict[
            'badge'] = 'urn:org:example:badges:robotics:beth'
        json_input = json.dumps(assertion_dict)
        state = INITIAL_STATE.copy()
        state['input']['value'] = json_input

        success, message, actions = detect_input_type(state)

        self.assertTrue(success)
        self.assertEqual(len(actions), 1)
        self.assertEqual(actions[0]['type'], 'SET_INPUT_TYPE')
        self.assertEqual(actions[0]['input_type'], 'json')
Beispiel #6
0
    def test_input_jsonld_type_detection_replaces_with_url(self):
        """
        The detect_input_type task should successfully detect JSONLD with an id URL
        as input and switch to using an 'id' as URL value if possible
        """
        json_input = test_components['2_0_basic_assertion']
        state = INITIAL_STATE.copy()
        state['input']['value'] = json_input

        success, message, actions = detect_input_type(state)

        self.assertTrue(success)
        self.assertEqual(len(actions), 4)
        self.assertEqual(actions[0]['type'], 'STORE_INPUT')
        self.assertEqual(actions[1]['type'], 'SET_INPUT_TYPE')
        self.assertEqual(actions[2]['type'], 'ADD_TASK')
        self.assertEqual(actions[2]['name'], 'FETCH_HTTP_NODE')

        self.assertEqual(actions[0]['input'], actions[2]['url'])
        self.assertEqual(json.loads(json_input)['id'], actions[2]['url'])