def test_define_scope_agent(self):
        url = 'http://testserver/XAPI/statements'
        guid = str(uuid.uuid1())
        stmt_data = {"id":guid,"actor":{"objectType": "Agent",
            "mbox":"mailto:bob@bob.com", "name":"bob"},"verb":{"id": "http://example.com/verbs/helped",
            "display": {"en-US":"helped"}},"object": {"objectType":"Agent", "mbox":"mailto:tim@tim.com",
            "name":"tim"}}
        stmt_post = self.client.post(reverse('lrs:statements'), json.dumps(stmt_data), content_type="application/json",
            Authorization=self.jane_auth, X_Experience_API_Version=settings.XAPI_VERSION)
        self.assertEqual(stmt_post.status_code, 200)


        # build stmt data and path
        put_guid = str(uuid.uuid1())
        stmt = json.dumps({"actor":{"objectType": "Agent", "mbox":"mailto:bill@bill.com", "name":"bill"},
            "verb":{"id": "http://example.com/verbs/talked","display": {"en-US":"talked"}},
            "object": {"objectType":"Agent", "mbox":"mailto:tim@tim.com","name":"tim timson"}})

        param = {"statementId":put_guid}
        path = "%s?%s" % (url, urllib.urlencode(param))
        
        # START PUT STMT
        oauth_header_resource_params, access_token = self.oauth_handshake(scope_type='statements/write statements/read')
        
        # from_token_and_callback takes a dictionary        
        param_list = oauth_header_resource_params.split(",")
        oauth_header_resource_params_dict = {}
        for p in param_list:
            item = p.split("=")
            oauth_header_resource_params_dict[str(item[0]).strip()] = str(item[1]).strip('"')
        # from_request ignores realm, must remove so not input to from_token_and_callback
        del oauth_header_resource_params_dict['OAuth realm']

        # Create oauth request and add signature
        oauth_request = oauth.Request.from_token_and_callback(access_token, http_method='PUT',
            http_url=path, parameters=oauth_header_resource_params_dict)
        signature_method = oauth.SignatureMethod_HMAC_SHA1()
        signature = signature_method.sign(oauth_request, self.consumer, access_token)
        oauth_header_resource_params += ',oauth_signature="%s"' % signature

        # Put statements
        resp = self.client.put(path, data=stmt, content_type="application/json",
            Authorization=oauth_header_resource_params, X_Experience_API_Version=settings.XAPI_VERSION)
        self.assertEqual(resp.status_code, 204)
        agents = Agent.objects.all().values_list('name', flat=True)
        # Jane, Bill, Tim, Bob, Dick, Anonymous Group and Account
        self.assertEqual(len(agents), 7)
        self.assertIn('tim', agents)
        self.assertNotIn('tim timson', agents)
        # =================================================

        # START GET STMT
        get_params = {"agent":{"objectType": "Agent", "mbox":"mailto:tim@tim.com"}, "related_agents":True}
        path = "%s?%s" % (url, urllib.urlencode(get_params)) 

        # Use same oauth headers, replace nonce
        oauth_header_resource_params_dict['oauth_nonce'] = 'get_differ_nonce'

        # Create oauth request and add signature
        oauth_request2 = oauth.Request.from_token_and_callback(access_token, http_method='GET',
            http_url=path, parameters=oauth_header_resource_params_dict)
        signature_method2 = oauth.SignatureMethod_HMAC_SHA1()
        signature2 = signature_method2.sign(oauth_request2, self.consumer, access_token)
        sig = oauth_header_resource_params.replace('"%s"' % signature, '"%s"' % signature2)
        new_oauth_headers = sig.replace('oauth_nonce="resource_nonce"','oauth_nonce="get_differ_nonce"')        

        get_resp = self.client.get(path, X_Experience_API_Version=settings.XAPI_VERSION,
            Authorization=new_oauth_headers)
        self.assertEqual(get_resp.status_code, 200)
        content = json.loads(get_resp.content)
        # Should be two since querying by tim email.
        self.assertEqual(len(content['statements']), 2)
        self.client.logout()
        # ==================================================

        # START OF POST WITH ANOTHER HANDSHAKE
        # Anonymous group that will make 2 canonical agents
        ot = "Group"
        members = [{"name":"john doe","mbox":"mailto:jd@example.com"},
                    {"name":"jan doe","mbox":"mailto:jandoe@example.com"}]
        kwargs = {"objectType":ot, "member": members, "name": "doe group"}
        global_group, created = Agent.objects.retrieve_or_create(**kwargs)

        # Anonymous group that will retrieve two agents and create one more canonical agents
        members = [{"name":"john doe","mbox":"mailto:jd@example.com"},
                    {"name":"jan doe","mbox":"mailto:jandoe@example.com"},
                    {"name":"dave doe", "mbox":"mailto:dd@example.com"}]
        kwargs1 = {"objectType":ot, "member": members, "name": "doe group"}

        post_stmt = {"actor":{"objectType": "Agent", "mbox":"mailto:dom@dom.com", "name":"dom"},
            "verb":{"id": "http://example.com/verbs/assisted","display": {"en-US":"assisted"}},
            "object": kwargs1}
        stmt_json = json.dumps(post_stmt)

        post_oauth_header_resource_params, post_access_token = self.oauth_handshake2(scope_type='statements/write statements/read')

        # from_token_and_callback takes a dictionary        
        post_param_list = post_oauth_header_resource_params.split(",")
        post_oauth_header_resource_params_dict = {}
        for p in post_param_list:
            item = p.split("=")
            post_oauth_header_resource_params_dict[str(item[0]).strip()] = str(item[1]).strip('"')
        
        # from_request ignores realm, must remove so not input to from_token_and_callback
        del post_oauth_header_resource_params_dict['OAuth realm']
        
        # Create oauth request and add signature
        post_oauth_request = oauth.Request.from_token_and_callback(post_access_token, http_method='POST',
            http_url='http://testserver/XAPI/statements/',
            parameters=post_oauth_header_resource_params_dict)
        post_signature_method = oauth.SignatureMethod_HMAC_SHA1()
        post_signature = post_signature_method.sign(post_oauth_request, self.consumer2,
            post_access_token)
        post_oauth_header_resource_params += ',oauth_signature="%s"' % post_signature  
        
        post = self.client.post('/XAPI/statements/', data=stmt_json, content_type="application/json",
            Authorization=post_oauth_header_resource_params, X_Experience_API_Version=settings.XAPI_VERSION)
        self.assertEqual(post.status_code, 200)
        agents = Agent.objects.all().values_list('name', flat=True)
        # 2 oauth group objects and 2 anon account agents for oauth, all of these agents since created with member or manually, 2 agents for
        # jand and dick users, 2 doe groups since can't update anymore
        self.assertEqual(len(agents), 15)
        self.assertIn('bill', agents)
        self.assertNotIn('tim timson', agents)
        self.assertIn('dom', agents)
        self.assertIn('bob', agents)
        self.assertIn('tim', agents)
        self.assertIn('jan doe', agents)
        self.assertIn('john doe', agents)
        self.assertIn('dave doe', agents)        
        self.assertIn('jane', agents)
        self.assertIn('dick', agents)
        self.assertIn('doe group', agents)
dataset/ETHPy150Open adlnet/ADL_LRS/lrs/tests/test_OAuth.py/OAuthTests.test_define_scope_agent/original