'Verify correct parameter parsing and validation for token responses.'

def test_json_token_response(self):

    self.assertEqual(parse_token_response(self.json_response), self.json_dict)
    self.assertRaises(AccessDeniedError, parse_token_response, self.json_error)
    self.assertRaises(MissingTokenError, parse_token_response, self.json_notoken)
    self.assertEqual(parse_token_response(self.json_response_noscope, scope=[u'all', u'the', u'scopes']), self.json_noscope_dict)
    scope_changes_recorded = []
    def record_scope_change(sender, message, old, new):
        scope_changes_recorded.append((message, old, new))
    os.environ[u'OAUTHLIB_RELAX_TOKEN_SCOPE'] = u'1'
    signals.scope_changed.connect(record_scope_change)
    try:
        parse_token_response(self.json_response, scope=u'aaa')
        self.assertEqual(len(scope_changes_recorded), 1)
        (message, old, new) = scope_changes_recorded[0]
        for scope in (new + old):
            self.assertIn(scope, message)
        self.assertEqual(old, [u'aaa'])
        self.assertEqual(set(new), set([u'abc', u'def']))
    finally:
        signals.scope_changed.disconnect(record_scope_change)
    del os.environ[u'OAUTHLIB_RELAX_TOKEN_SCOPE']
