def test_sync_consumer_basic_auth(self):
        # Create consumer_ref
        consumer_ref = ConsumerReferenceFactory(username=fake.consumer_name())

        # Mark for auto cleanup
        self._cleanup_afterwards(consumer_ref)

        # Publish
        logic.synchronize_consumer(self.client, consumer_ref)
        self.assertTrue(consumer_ref.synchronized)

        # Check kong
        amount = self.client.consumers.basic_auth(consumer_ref.kong_id).count()
        self.assertEqual(amount, 0)

        # Create auth
        auth_ref = BasicAuthReferenceFactory(consumer=consumer_ref)

        # Publish
        logic.synchronize_consumer(self.client, consumer_ref)
        self.assertTrue(consumer_ref.synchronized)

        # Reload
        auth_ref = models.BasicAuthReference.objects.get(id=auth_ref.id)
        self.assertIsNotNone(auth_ref.kong_id)

        # Make sure the remote username matches the locally known username
        result = self.client.consumers.basic_auth(consumer_ref.kong_id).retrieve(auth_ref.kong_id)
        self.assertIsNotNone(result)
        self.assertEqual(result['username'], auth_ref.username)
        self.assertIsNotNone(result['password'])
Example #2
0
    def test_sync_consumer_oauth2(self):
        # Create consumer_ref
        consumer_ref = ConsumerReferenceFactory(username=fake.consumer_name())

        # Mark for auto cleanup
        self._cleanup_afterwards(consumer_ref)

        # Publish
        logic.synchronize_consumer(self.client, consumer_ref)
        self.assertTrue(consumer_ref.synchronized)

        # Check kong
        amount = self.client.consumers.oauth2(consumer_ref.kong_id).count()
        self.assertEqual(amount, 0)

        # Create auth
        auth_ref = OAuth2ReferenceFactory(consumer=consumer_ref)

        # Publish
        logic.synchronize_consumer(self.client, consumer_ref)
        self.assertTrue(consumer_ref.synchronized)

        # Reload
        auth_ref = models.OAuth2Reference.objects.get(id=auth_ref.id)
        self.assertIsNotNone(auth_ref.kong_id)

        # Check kong
        result = self.client.consumers.oauth2(consumer_ref.kong_id).retrieve(auth_ref.kong_id)
        self.assertIsNotNone(result)
        self.assertEqual(result['client_id'], auth_ref.client_id)
Example #3
0
    def test_delete_consumer_oauth2(self):
        # Create consumer_ref
        consumer_ref = ConsumerReferenceFactory(username=fake.consumer_name())

        # Create auth
        auth_ref1 = OAuth2ReferenceFactory(consumer=consumer_ref)
        OAuth2ReferenceFactory(consumer=consumer_ref)

        # Publish
        logic.synchronize_consumer(self.client, consumer_ref)
        self.assertTrue(consumer_ref.synchronized)

        # Check
        self.assertEqual(
            self.client.consumers.oauth2(consumer_ref.kong_id).count(), 2)

        # Delete auth_ref1
        auth_ref1.delete()

        # Publish
        logic.synchronize_consumer(self.client, consumer_ref)
        self.assertTrue(consumer_ref.synchronized)

        # Check
        self.assertEqual(
            self.client.consumers.oauth2(consumer_ref.kong_id).count(), 1)

        # Delete consumer
        consumer_kong_id = consumer_ref.kong_id
        consumer_ref.delete()

        # Check
        with self.assertRaises(ValueError):
            self.client.consumers.oauth2(consumer_kong_id).count()
Example #4
0
    def test_incomplete_consumer(self):
        # Create incomplete consumer_ref
        consumer_ref = ConsumerReferenceFactory()

        # Mark for auto cleanup
        self._cleanup_afterwards(consumer_ref)

        # Try to sync, expect an error
        with self.assertRaises(ValueError):
            logic.synchronize_consumer(self.client, consumer_ref)

        self.assertFalse(consumer_ref.synchronized)

        # Fix consumer_ref
        consumer_ref.username = fake.consumer_name()
        consumer_ref.save()

        # Sync again
        logic.synchronize_consumer(self.client, consumer_ref)
        self.assertTrue(consumer_ref.synchronized)

        # Check kong
        result = self.client.consumers.retrieve(consumer_ref.kong_id)
        self.assertIsNotNone(result)
        self.assertEqual(result['username'], consumer_ref.username)
Example #5
0
    def test_incomplete_consumer(self):
        # Create incomplete consumer_ref
        consumer_ref = ConsumerReferenceFactory()

        # Mark for auto cleanup
        self._cleanup_afterwards(consumer_ref)

        # Try to sync, expect an error
        with self.assertRaises(ValueError):
            logic.synchronize_consumer(self.client, consumer_ref)

        self.assertFalse(consumer_ref.synchronized)

        # Fix consumer_ref
        consumer_ref.username = fake.consumer_name()
        consumer_ref.save()

        # Sync again
        logic.synchronize_consumer(self.client, consumer_ref)
        self.assertTrue(consumer_ref.synchronized)

        # Check kong
        result = self.client.consumers.retrieve(consumer_ref.kong_id)
        self.assertIsNotNone(result)
        self.assertEqual(result['username'], consumer_ref.username)
Example #6
0
    def test_sync_updated_consumer(self):
        # Create consumer_ref
        consumer_ref = ConsumerReferenceFactory(username=fake.consumer_name())

        # Mark for auto cleanup
        self._cleanup_afterwards(consumer_ref)

        # Publish
        logic.synchronize_consumer(self.client, consumer_ref)
        self.assertTrue(consumer_ref.synchronized)

        # Check kong
        result = self.client.consumers.retrieve(consumer_ref.kong_id)
        self.assertIsNotNone(result)
        self.assertEqual(result['username'], consumer_ref.username)

        # Update
        new_name = fake.consumer_name()
        self.assertNotEqual(new_name, consumer_ref.username)
        consumer_ref.username = new_name
        consumer_ref.save()

        # Publish
        logic.synchronize_consumer(self.client, consumer_ref)
        self.assertTrue(consumer_ref.synchronized)

        # Check kong
        result = self.client.consumers.retrieve(consumer_ref.kong_id)
        self.assertIsNotNone(result)
        self.assertEqual(result['username'], new_name)
Example #7
0
    def test_sync_consumer_basic_auth(self):
        # Create consumer_ref
        consumer_ref = ConsumerReferenceFactory(username=fake.consumer_name())

        # Mark for auto cleanup
        self._cleanup_afterwards(consumer_ref)

        # Publish
        logic.synchronize_consumer(self.client, consumer_ref)
        self.assertTrue(consumer_ref.synchronized)

        # Check kong
        amount = self.client.consumers.basic_auth(consumer_ref.kong_id).count()
        self.assertEqual(amount, 0)

        # Create auth
        auth_ref = BasicAuthReferenceFactory(consumer=consumer_ref)

        # Publish
        logic.synchronize_consumer(self.client, consumer_ref)
        self.assertTrue(consumer_ref.synchronized)

        # Reload
        auth_ref = models.BasicAuthReference.objects.get(id=auth_ref.id)
        self.assertIsNotNone(auth_ref.kong_id)

        # Make sure the remote username matches the locally known username
        result = self.client.consumers.basic_auth(
            consumer_ref.kong_id).retrieve(auth_ref.kong_id)
        self.assertIsNotNone(result)
        self.assertEqual(result['username'], auth_ref.username)
        self.assertIsNotNone(result['password'])
Example #8
0
    def test_sync_updated_consumer(self):
        # Create consumer_ref
        consumer_ref = ConsumerReferenceFactory(username=fake.consumer_name())

        # Mark for auto cleanup
        self._cleanup_afterwards(consumer_ref)

        # Publish
        logic.synchronize_consumer(self.client, consumer_ref)
        self.assertTrue(consumer_ref.synchronized)

        # Check kong
        result = self.client.consumers.retrieve(consumer_ref.kong_id)
        self.assertIsNotNone(result)
        self.assertEqual(result['username'], consumer_ref.username)

        # Update
        new_name = fake.consumer_name()
        self.assertNotEqual(new_name, consumer_ref.username)
        consumer_ref.username = new_name
        consumer_ref.save()

        # Publish
        logic.synchronize_consumer(self.client, consumer_ref)
        self.assertTrue(consumer_ref.synchronized)

        # Check kong
        result = self.client.consumers.retrieve(consumer_ref.kong_id)
        self.assertIsNotNone(result)
        self.assertEqual(result['username'], new_name)
Example #9
0
    def test_sync_consumer_oauth2(self):
        # Create consumer_ref
        consumer_ref = ConsumerReferenceFactory(username=fake.consumer_name())

        # Mark for auto cleanup
        self._cleanup_afterwards(consumer_ref)

        # Publish
        logic.synchronize_consumer(self.client, consumer_ref)
        self.assertTrue(consumer_ref.synchronized)

        # Check kong
        amount = self.client.consumers.oauth2(consumer_ref.kong_id).count()
        self.assertEqual(amount, 0)

        # Create auth
        auth_ref = OAuth2ReferenceFactory(consumer=consumer_ref)

        # Publish
        logic.synchronize_consumer(self.client, consumer_ref)
        self.assertTrue(consumer_ref.synchronized)

        # Reload
        auth_ref = models.OAuth2Reference.objects.get(id=auth_ref.id)
        self.assertIsNotNone(auth_ref.kong_id)

        # Check kong
        result = self.client.consumers.oauth2(consumer_ref.kong_id).retrieve(
            auth_ref.kong_id)
        self.assertIsNotNone(result)
        self.assertEqual(result['client_id'], auth_ref.client_id)
Example #10
0
    def test_delete_consumer_oauth2(self):
        # Create consumer_ref
        consumer_ref = ConsumerReferenceFactory(username=fake.consumer_name())

        # Create auth
        auth_ref1 = OAuth2ReferenceFactory(consumer=consumer_ref)
        OAuth2ReferenceFactory(consumer=consumer_ref)

        # Publish
        logic.synchronize_consumer(self.client, consumer_ref)
        self.assertTrue(consumer_ref.synchronized)

        # Check
        self.assertEqual(self.client.consumers.oauth2(consumer_ref.kong_id).count(), 2)

        # Delete auth_ref1
        auth_ref1.delete()

        # Publish
        logic.synchronize_consumer(self.client, consumer_ref)
        self.assertTrue(consumer_ref.synchronized)

        # Check
        self.assertEqual(self.client.consumers.oauth2(consumer_ref.kong_id).count(), 1)

        # Delete consumer
        consumer_kong_id = consumer_ref.kong_id
        consumer_ref.delete()

        # Check
        with self.assertRaises(ValueError):
            self.client.consumers.oauth2(consumer_kong_id).count()
Example #11
0
    def test_sync_consumer_multiple_oauth2(self):
        amount = 3

        # Create consumer_ref
        consumer_ref = ConsumerReferenceFactory(username=fake.consumer_name())

        # Mark for auto cleanup
        self._cleanup_afterwards(consumer_ref)

        # Create auths
        auths = []
        for i in range(amount):
            auths.append(OAuth2ReferenceFactory(consumer=consumer_ref))

        # Publish
        logic.synchronize_consumer(self.client, consumer_ref)
        self.assertTrue(consumer_ref.synchronized)

        # Check
        self.assertEqual(self.client.consumers.oauth2(consumer_ref.kong_id).count(), amount)

        # Reload
        for i in range(len(auths)):
            auths[i] = models.OAuth2Reference.objects.get(id=auths[i].id)
            self.assertIsNotNone(auths[i].kong_id)

        # Check kong
        result = self.client.consumers.oauth2(consumer_ref.kong_id).list()
        self.assertIsNotNone(result)
        self.assertEqual(
            sorted([(uuid.UUID(r['id']), r['client_id']) for r in result['data']], key=lambda x: x[0]),
            sorted([(obj.kong_id, obj.client_id) for obj in auths], key=lambda x: x[0]))
Example #12
0
File: views.py Project: BUPT902/API
def ConfigConsumer(username):
    consumer = ConsumerReference(username=username)
    consumer.save()
    KeyAuth = KeyAuthReference(consumer=consumer)
    KeyAuth.save()
    client = factory.get_kong_client()
    obj = ConsumerReference.objects.get(id=consumer.id)
    logic.synchronize_consumer(client, obj, toggle=False)
    client.close()
Example #13
0
File: views.py Project: BUPT902/API
def apiDetail(request, param1):
    #http://10.103.240.194:8080/accontroller/apiAccessControl?apiName=weather&userId=15
    api = APIReference.objects.get(name = param1)
    username = get_username(request)
    person = ConsumerReference.objects.get(username = username)
    status = 0
    if request.method == "POST":
        p = Userinfo.objects.get(username=username)
        payload = {'apiName': param1, 'userId': p.userid}
        try:
            req_content = get("http://10.103.240.194:8080/accontroller/apiAccessControl", params=payload, timeout=2)
            json_content = json.loads(req_content.content)
            if not(json_content['result']):
                return HttpResponse('3')
        except:
            return HttpResponse("2")
        try:
            new_buy = BuyReference(api=api,consumer=person)
            new_buy.save()
            Acl = AclReference(consumer=person, group=param1)
            Acl.save()
            #print(person.id)
            client = factory.get_kong_client()
            obj = ConsumerReference.objects.get(id=person.id)
            logic.synchronize_consumer(client, obj, toggle=False)
            client.close()
            return HttpResponse("1")
        except:
            return HttpResponse("2")
    api_key = person.keyauthreference_related.all()[:1]
    api_key = api_key[0]
    gateway_url = 'http://10.33.6.199:8000'
    url_Parameters = ParameterReference.objects.filter(api__name__exact = param1)
    Headers = HeaderReference.objects.filter(api__name__exact = param1)
    Errors  = ErrorReference.objects.filter(api__name__exact = param1)
    url_example = "?"
    for para in url_Parameters:
        url_example += para.name + '=' + para.defaultValue + '&'
    context = {
        'api' : api,
        'gateway_url' : gateway_url,
        'url_Parameters' : url_Parameters,
        'url_example' : url_example,
        'Headers' : Headers,
        'Errors' : Errors,
        'api_key': api_key,
        'status' : status,
    }
    return render_to_response('apiDetail.html', context)
    def test_sync_consumer(self):
        # Create consumer_ref
        consumer_ref = ConsumerReferenceFactory(username=fake.consumer_name())

        # Mark for auto cleanup
        self._cleanup_afterwards(consumer_ref)

        # Sync
        logic.synchronize_consumer(self.client, consumer_ref)
        self.assertTrue(consumer_ref.synchronized)

        # Make sure the remote username matches the locally known username
        result = self.client.consumers.retrieve(consumer_ref.kong_id)
        self.assertIsNotNone(result)
        self.assertEqual(result['username'], consumer_ref.username)
Example #15
0
    def test_sync_consumer(self):
        # Create consumer_ref
        consumer_ref = ConsumerReferenceFactory(username=fake.consumer_name())

        # Mark for auto cleanup
        self._cleanup_afterwards(consumer_ref)

        # Sync
        logic.synchronize_consumer(self.client, consumer_ref)
        self.assertTrue(consumer_ref.synchronized)

        # Make sure the remote username matches the locally known username
        result = self.client.consumers.retrieve(consumer_ref.kong_id)
        self.assertIsNotNone(result)
        self.assertEqual(result['username'], consumer_ref.username)
Example #16
0
    def test_delete_consumer(self):
        # Create consumer_ref
        consumer_ref = ConsumerReferenceFactory(username=fake.consumer_name())

        # Publish
        logic.synchronize_consumer(self.client, consumer_ref)
        self.assertTrue(consumer_ref.synchronized)

        # Check kong
        result = self.client.consumers.retrieve(consumer_ref.kong_id)
        self.assertIsNotNone(result)
        self.assertEqual(result['username'], consumer_ref.username)

        # You can delete afterwards
        consumer_kong_id = consumer_ref.kong_id
        consumer_ref.delete()

        # Check kong
        with self.assertRaises(ValueError):
            _ = self.client.consumers.retrieve(consumer_kong_id)
Example #17
0
    def test_delete_consumer(self):
        # Create consumer_ref
        consumer_ref = ConsumerReferenceFactory(username=fake.consumer_name())

        # Publish
        logic.synchronize_consumer(self.client, consumer_ref)
        self.assertTrue(consumer_ref.synchronized)

        # Check kong
        result = self.client.consumers.retrieve(consumer_ref.kong_id)
        self.assertIsNotNone(result)
        self.assertEqual(result['username'], consumer_ref.username)

        # You can delete afterwards
        consumer_kong_id = consumer_ref.kong_id
        consumer_ref.delete()

        # Check kong
        with self.assertRaises(ValueError):
            _ = self.client.consumers.retrieve(consumer_kong_id)
Example #18
0
    def test_withdraw_consumer(self):
        # Create consumer_ref
        consumer_ref = ConsumerReferenceFactory(username=fake.consumer_name())

        # Publish
        logic.synchronize_consumer(self.client, consumer_ref)
        self.assertTrue(consumer_ref.synchronized)

        # Make sure the remote username matches the locally known username
        result = self.client.consumers.retrieve(consumer_ref.kong_id)
        self.assertIsNotNone(result)
        self.assertEqual(result['username'], consumer_ref.username)

        # Store kong_id
        kong_id = consumer_ref.kong_id

        # You can delete afterwards
        logic.withdraw_consumer(self.client, consumer_ref)
        self.assertFalse(consumer_ref.synchronized)

        # Check kong
        with self.assertRaises(ValueError):
            _ = self.client.consumers.retrieve(kong_id)
    def test_withdraw_consumer(self):
        # Create consumer_ref
        consumer_ref = ConsumerReferenceFactory(username=fake.consumer_name())

        # Publish
        logic.synchronize_consumer(self.client, consumer_ref)
        self.assertTrue(consumer_ref.synchronized)

        # Make sure the remote username matches the locally known username
        result = self.client.consumers.retrieve(consumer_ref.kong_id)
        self.assertIsNotNone(result)
        self.assertEqual(result['username'], consumer_ref.username)

        # Store kong_id
        kong_id = consumer_ref.kong_id

        # You can delete afterwards
        logic.withdraw_consumer(self.client, consumer_ref)
        self.assertFalse(consumer_ref.synchronized)

        # Check kong
        with self.assertRaises(ValueError):
            _ = self.client.consumers.retrieve(kong_id)
Example #20
0
    def test_sync_consumer_multiple_basic_auth(self):
        amount = 3

        # Create consumer_ref
        consumer_ref = ConsumerReferenceFactory(username=fake.consumer_name())

        # Mark for auto cleanup
        self._cleanup_afterwards(consumer_ref)

        # Create auths
        auths = []
        for i in range(amount):
            auths.append(BasicAuthReferenceFactory(consumer=consumer_ref))

        # Publish
        logic.synchronize_consumer(self.client, consumer_ref)
        self.assertTrue(consumer_ref.synchronized)

        # Check
        self.assertEqual(
            self.client.consumers.basic_auth(consumer_ref.kong_id).count(),
            amount)

        # Reload
        for i in range(len(auths)):
            auths[i] = models.BasicAuthReference.objects.get(id=auths[i].id)
            self.assertIsNotNone(auths[i].kong_id)

        # Check kong
        result = self.client.consumers.basic_auth(consumer_ref.kong_id).list()
        self.assertIsNotNone(result)
        self.assertEqual(
            sorted([(uuid.UUID(r['id']), r['username'])
                    for r in result['data']],
                   key=lambda x: x[0]),
            sorted([(obj.kong_id, obj.username) for obj in auths],
                   key=lambda x: x[0]))
Example #21
0
    def test_withdraw_consumer_oauth2(self):
        # Create consumer_ref
        consumer_ref = ConsumerReferenceFactory(username=fake.consumer_name())

        # Create auth
        auth_ref = OAuth2ReferenceFactory(consumer=consumer_ref)

        # Publish
        logic.synchronize_consumer(self.client, consumer_ref)
        self.assertTrue(consumer_ref.synchronized)

        # Reload
        auth_ref = models.OAuth2Reference.objects.get(id=auth_ref.id)
        self.assertIsNotNone(auth_ref.kong_id)
        self.assertTrue(auth_ref.synchronized)

        # Withdraw
        logic.withdraw_consumer(self.client, consumer_ref)
        self.assertFalse(consumer_ref.synchronized)

        # Reload
        auth_ref = models.OAuth2Reference.objects.get(id=auth_ref.id)
        self.assertIsNone(auth_ref.kong_id)
        self.assertFalse(auth_ref.synchronized)
Example #22
0
    def test_withdraw_consumer_oauth2(self):
        # Create consumer_ref
        consumer_ref = ConsumerReferenceFactory(username=fake.consumer_name())

        # Create auth
        auth_ref = OAuth2ReferenceFactory(consumer=consumer_ref)

        # Publish
        logic.synchronize_consumer(self.client, consumer_ref)
        self.assertTrue(consumer_ref.synchronized)

        # Reload
        auth_ref = models.OAuth2Reference.objects.get(id=auth_ref.id)
        self.assertIsNotNone(auth_ref.kong_id)
        self.assertTrue(auth_ref.synchronized)

        # Withdraw
        logic.withdraw_consumer(self.client, consumer_ref)
        self.assertFalse(consumer_ref.synchronized)

        # Reload
        auth_ref = models.OAuth2Reference.objects.get(id=auth_ref.id)
        self.assertIsNone(auth_ref.kong_id)
        self.assertFalse(auth_ref.synchronized)