From b39d1a8f2d9f7af9b39b54164e999786d839b922 Mon Sep 17 00:00:00 2001 From: Aminah Burch Date: Mon, 14 Aug 2023 18:39:31 +0300 Subject: [PATCH 1/3] feat(api): adds trusta labs stamp weight --- api/scorer/settings/gitcoin_passport_weights.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/api/scorer/settings/gitcoin_passport_weights.py b/api/scorer/settings/gitcoin_passport_weights.py index 19cbe9949..40821ada3 100644 --- a/api/scorer/settings/gitcoin_passport_weights.py +++ b/api/scorer/settings/gitcoin_passport_weights.py @@ -72,9 +72,6 @@ "twitterAccountAgeGte#180": "1.21", "twitterAccountAgeGte#365": "1.21", "twitterAccountAgeGte#730": "1.21", - "twitterTweetDaysGte#30": "1.21", - "twitterTweetDaysGte#60": "1.21", - "twitterTweetDaysGte#120": "1.21", "ZkSync": "0.400", "ZkSyncEra": "0.400", "CyberProfilePremium": "1.21", From 4f0db6d1bca22bad7ae3f3b93588dcdc3591e8f8 Mon Sep 17 00:00:00 2001 From: Gerald Iakobinyi-Pich Date: Tue, 15 Aug 2023 15:28:48 +0300 Subject: [PATCH 2/3] feat(api): saving trusta labs scores to Event table instead of its own table --- api/registry/models.py | 1 + api/trusta_labs/api.py | 8 +++++--- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/api/registry/models.py b/api/registry/models.py index 771001f46..8e5c1a54e 100644 --- a/api/registry/models.py +++ b/api/registry/models.py @@ -77,6 +77,7 @@ class Event(models.Model): class Action(models.TextChoices): FIFO_DEDUPLICATION = "FDP" LIFO_DEDUPLICATION = "LDP" + TRUSTALAB_SCORE = "TLS" action = models.CharField( max_length=3, diff --git a/api/trusta_labs/api.py b/api/trusta_labs/api.py index b45fe210d..076922108 100644 --- a/api/trusta_labs/api.py +++ b/api/trusta_labs/api.py @@ -3,7 +3,7 @@ from ninja.security import APIKeyHeader from ninja_extra import NinjaExtraAPI, status from ninja_extra.exceptions import APIException -from trusta_labs.models import TrustaLabsScore +from registry.models import Event api = NinjaExtraAPI(urls_namespace="trusta_labs") @@ -55,6 +55,8 @@ def create_trusta_labs_score_db(request, payload: TrustaLabsScorePayload): if payload.score == None: raise TrustaLabsScoreHasNoScore() - TrustaLabsScore.objects.update_or_create( - address=payload.address, sybil_risk_score=payload.score + Event.objects.create( + action=Event.Action.TRUSTALAB_SCORE, + address=payload.address.lower(), + data={"score": payload.score}, ) From 5159facb1459c33e2cde3baa9a227cbc353b766f Mon Sep 17 00:00:00 2001 From: Lucian Hymer Date: Tue, 15 Aug 2023 09:04:19 -0700 Subject: [PATCH 3/3] feat(api): added more trusta labs tests and removed unused model --- api/trusta_labs/admin.py | 12 ----- api/trusta_labs/migrations/0001_initial.py | 36 --------------- api/trusta_labs/models.py | 12 ----- .../test/test_trusta_labs_score.py | 45 ++++++++++++++++--- 4 files changed, 38 insertions(+), 67 deletions(-) delete mode 100644 api/trusta_labs/admin.py delete mode 100644 api/trusta_labs/migrations/0001_initial.py delete mode 100644 api/trusta_labs/models.py diff --git a/api/trusta_labs/admin.py b/api/trusta_labs/admin.py deleted file mode 100644 index b5bc4f6e4..000000000 --- a/api/trusta_labs/admin.py +++ /dev/null @@ -1,12 +0,0 @@ -from django.contrib import admin -from trusta_labs.models import TrustaLabsScore - - -class TrustaLabsScoreAdmin(admin.ModelAdmin): - list_display = ["address", "sybil_risk_score"] - search_fields = ["address"] - search_help_text = "This will perform an exact case insensitive search by 'address'" - show_full_result_count = False - - -admin.site.register(TrustaLabsScore, TrustaLabsScoreAdmin) diff --git a/api/trusta_labs/migrations/0001_initial.py b/api/trusta_labs/migrations/0001_initial.py deleted file mode 100644 index e0b8b06de..000000000 --- a/api/trusta_labs/migrations/0001_initial.py +++ /dev/null @@ -1,36 +0,0 @@ -# Generated by Django 4.2.3 on 2023-08-14 17:52 - -import account.models -from django.db import migrations, models - - -class Migration(migrations.Migration): - initial = True - - dependencies = [] - - operations = [ - migrations.CreateModel( - name="TrustaLabsScore", - fields=[ - ( - "id", - models.BigAutoField( - auto_created=True, - primary_key=True, - serialize=False, - verbose_name="ID", - ), - ), - ( - "address", - account.models.EthAddressField( - db_index=True, max_length=100, null=True - ), - ), - ("sybil_risk_score", models.IntegerField(blank=True, null=True)), - ("created_at", models.DateTimeField(auto_now_add=True)), - ("updated_at", models.DateTimeField(auto_now=True)), - ], - ), - ] diff --git a/api/trusta_labs/models.py b/api/trusta_labs/models.py deleted file mode 100644 index 6104ac941..000000000 --- a/api/trusta_labs/models.py +++ /dev/null @@ -1,12 +0,0 @@ -from account.models import EthAddressField -from django.db import models - -# WHEN a Trusta Labs API call is made for verification, -# THEN the result of the call, including timestamps, passport addresses, and Trusta Labs scores, should be logged in a dedicated table, separate from the credential issuance. - - -class TrustaLabsScore(models.Model): - address = EthAddressField(null=True, blank=False, max_length=100, db_index=True) - sybil_risk_score = models.IntegerField(null=True, blank=True) - created_at = models.DateTimeField(auto_now_add=True) - updated_at = models.DateTimeField(auto_now=True) diff --git a/api/trusta_labs/test/test_trusta_labs_score.py b/api/trusta_labs/test/test_trusta_labs_score.py index e014a0916..bd6298768 100644 --- a/api/trusta_labs/test/test_trusta_labs_score.py +++ b/api/trusta_labs/test/test_trusta_labs_score.py @@ -1,10 +1,8 @@ import json -from datetime import datetime, timezone -from typing import cast from django.conf import settings from django.test import Client, TestCase -from trusta_labs.models import TrustaLabsScore +from registry.models import Event mock_trusta_labs_score_body = { "address": "0x8u3eu3ydh3rydh3irydhu", @@ -14,8 +12,8 @@ class TrustaLabsScoreTestCase(TestCase): def test_create_trusta_labs_score(self): - self.headers = {"HTTP_AUTHORIZATION": settings.CGRANTS_API_TOKEN} """Test that creation of a trusta lab score works and saved correctly""" + self.headers = {"HTTP_AUTHORIZATION": settings.CGRANTS_API_TOKEN} client = Client() trusta_labs_response = client.post( "/trusta_labs/trusta-labs-score", @@ -26,11 +24,44 @@ def test_create_trusta_labs_score(self): self.assertEqual(trusta_labs_response.status_code, 200) # Check that the trusta lab score was created - all_trusta_labs_scores = list(TrustaLabsScore.objects.all()) + all_trusta_labs_scores = list( + Event.objects.filter(action=Event.Action.TRUSTALAB_SCORE) + ) self.assertEqual(len(all_trusta_labs_scores), 1) score = all_trusta_labs_scores[0] self.assertEqual(score.address, mock_trusta_labs_score_body["address"]) - self.assertEqual(score.sybil_risk_score, mock_trusta_labs_score_body["score"]) + self.assertEqual(score.data["score"], mock_trusta_labs_score_body["score"]) def test_error_creating_trusta_lab_score(self): - pass + self.headers = {"HTTP_AUTHORIZATION": settings.CGRANTS_API_TOKEN} + client = Client() + trusta_labs_response = client.post( + "/trusta_labs/trusta-labs-score", + "{}", + content_type="application/json", + **self.headers + ) + self.assertEqual(trusta_labs_response.status_code, 422) + + # Check that the trusta lab score was not created + all_trusta_labs_scores = list( + Event.objects.filter(action=Event.Action.TRUSTALAB_SCORE) + ) + self.assertEqual(len(all_trusta_labs_scores), 0) + + def test_bad_auth(self): + self.headers = {"HTTP_AUTHORIZATION": "bad_auth"} + client = Client() + trusta_labs_response = client.post( + "/trusta_labs/trusta-labs-score", + "{}", + content_type="application/json", + **self.headers + ) + self.assertEqual(trusta_labs_response.status_code, 401) + + # Check that the trusta lab score was not created + all_trusta_labs_scores = list( + Event.objects.filter(action=Event.Action.TRUSTALAB_SCORE) + ) + self.assertEqual(len(all_trusta_labs_scores), 0)