Skip to content

Commit

Permalink
Store trusta labs scores to Event table (#354)
Browse files Browse the repository at this point in the history
* feat(api): adds trusta labs stamp weight

* feat(api): saving trusta labs scores to Event table instead of its own table

* feat(api): added more trusta labs tests and removed unused model

---------

Co-authored-by: Aminah Burch <[email protected]>
Co-authored-by: Lucian Hymer <[email protected]>
  • Loading branch information
3 people authored Aug 15, 2023
1 parent 44fc36d commit 24d8e25
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 73 deletions.
1 change: 1 addition & 0 deletions api/registry/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
3 changes: 0 additions & 3 deletions api/scorer/settings/gitcoin_passport_weights.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
12 changes: 0 additions & 12 deletions api/trusta_labs/admin.py

This file was deleted.

8 changes: 5 additions & 3 deletions api/trusta_labs/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down Expand Up @@ -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},
)
36 changes: 0 additions & 36 deletions api/trusta_labs/migrations/0001_initial.py

This file was deleted.

12 changes: 0 additions & 12 deletions api/trusta_labs/models.py

This file was deleted.

45 changes: 38 additions & 7 deletions api/trusta_labs/test/test_trusta_labs_score.py
Original file line number Diff line number Diff line change
@@ -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",
Expand All @@ -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",
Expand All @@ -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)

0 comments on commit 24d8e25

Please sign in to comment.