Skip to content

Commit

Permalink
merging stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
Otto Andelin committed Aug 25, 2024
2 parents 3134896 + 5c30192 commit 87fe3cc
Show file tree
Hide file tree
Showing 24 changed files with 447 additions and 561 deletions.
3 changes: 2 additions & 1 deletion Backend/game_history/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ The `GameStat` model represents the statistics of a game, linked to a `GameHisto
- `game_id`: OneToOneField, primary key linked to `GameHistory`.
- `player1_score`: Integer, score of the first player.
- `player2_score`: Integer, score of the second player.
- `total_hits`: Integer, total number of hits in the game.
- `player1_hits`: Integer, number of hits by the first player.
- `player2_hits`: Integer, number of hits by the second player.
- `longest_rally`: Integer, the longest rally in the game.

### Serializers
Expand Down
6 changes: 4 additions & 2 deletions Backend/game_history/game_history/game_data/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@ def __str__(self):
class GameStat(models.Model):
game_id = models.OneToOneField(GameHistory, on_delete=models.CASCADE, primary_key=True) # this field is a foreign key to the GameHistory model
player1_score = models.IntegerField()
player1_hits = models.IntegerField()
player2_score = models.IntegerField()
total_hits = models.IntegerField()
player2_hits = models.IntegerField()
longest_rally = models.IntegerField()

def __str__(self):
return f"Stats for Game {self.game_id.game_id}: {self.player1_score} vs {self.player2_score} - Total Hits: {self.total_hits}, Longest Rally: {self.longest_rally}"
return (f"Stats for Game {self.game_id.game_id}: {self.player1_score} vs {self.player2_score} - Longest Rally: {self.longest_rally}")
111 changes: 47 additions & 64 deletions Backend/game_history/game_history/game_history/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,53 +19,53 @@
PGSQL_HOST = os.environ.get('PGSQL_HOST')
DB_USER = os.environ.get('DB_USER')
DB_PASS = os.environ.get('DB_PASS')
LOG_DIR = Path('/var/log/')

LOGGING = {
'version': 1,
'disable_existing_loggers': False,
'formatters': {
'verbose': {
'format': '{levelname} {asctime} {module} {message}',
'style': '{',
},
'simple': {
'format': '{levelname} {message}',
'style': '{',
},
},
'handlers': {
'file': {
'level': 'DEBUG',
'class': 'logging.FileHandler',
'filename': os.path.join(LOG_DIR, 'django_debug.log'),
'formatter': 'verbose',
},
'error_file': {
'level': 'ERROR',
'class': 'logging.FileHandler',
'filename': os.path.join(LOG_DIR, 'django_error.log'),
'formatter': 'verbose',
},
'console': {
'level': 'DEBUG',
'class': 'logging.StreamHandler',
'formatter': 'simple',
},
},
'loggers': {
'django': {
'handlers': ['file', 'console'],
'level': 'DEBUG',
'propagate': True,
},
'django.request': {
'handlers': ['error_file'],
'level': 'ERROR',
'propagate': False,
},
},
}
# LOG_DIR = Path('/var/log/')

# LOGGING = {
# 'version': 1,
# 'disable_existing_loggers': False,
# 'formatters': {
# 'verbose': {
# 'format': '{levelname} {asctime} {module} {message}',
# 'style': '{',
# },
# 'simple': {
# 'format': '{levelname} {message}',
# 'style': '{',
# },
# },
# 'handlers': {
# 'file': {
# 'level': 'DEBUG',
# 'class': 'logging.FileHandler',
# 'filename': os.path.join(LOG_DIR, 'django_debug.log'),
# 'formatter': 'verbose',
# },
# 'error_file': {
# 'level': 'ERROR',
# 'class': 'logging.FileHandler',
# 'filename': os.path.join(LOG_DIR, 'django_error.log'),
# 'formatter': 'verbose',
# },
# 'console': {
# 'level': 'DEBUG',
# 'class': 'logging.StreamHandler',
# 'formatter': 'simple',
# },
# },
# 'loggers': {
# 'django': {
# 'handlers': ['file', 'console'],
# 'level': 'DEBUG',
# 'propagate': True,
# },
# 'django.request': {
# 'handlers': ['error_file'],
# 'level': 'ERROR',
# 'propagate': False,
# },
# },
# }

# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent
Expand Down Expand Up @@ -99,26 +99,9 @@
'django.contrib.staticfiles',
'game_data',
'rest_framework',
'rest_framework_simplejwt',
'corsheaders',
]

SIMPLE_JWT = {
"ACCESS_TOKEN_LIFETIME": timedelta(minutes=60),
"REFRESH_TOKEN_LIFETIME": timedelta(days=1),
"ROTATE_REFRESH_TOKENS": False, # If True, refresh tokens will rotate, meaning that a new token is returned with each request to the refresh endpoint. for example, if a user is logged in on multiple devices, rotating refresh tokens will cause all devices to be logged out when the user logs out on one device.
"BLACKLIST_AFTER_ROTATION": True, # If True, the refresh token will be blacklisted after it is used to obtain a new access token. This means that if a refresh token is stolen, it can only be used once to obtain a new access token. This is useful if rotating refresh tokens is enabled, but can cause problems if a refresh token is shared between multiple clients.
"AUTH_HEADER_TYPES": ("Bearer",),
"AUTH_TOKEN_CLASSES": ("rest_framework_simplejwt.tokens.AccessToken",),
}

# Add REST framework settings for JWT authentication
REST_FRAMEWORK = {
'DEFAULT_AUTHENTICATION_CLASSES': (
'rest_framework_simplejwt.authentication.JWTAuthentication',
),
}

MIDDLEWARE = [
"corsheaders.middleware.CorsMiddleware",
"django.middleware.security.SecurityMiddleware",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,8 @@ def test_create_game_stat(api_client):
'game_id': game.game_id,
'player1_score': 10,
'player2_score': 5,
'total_hits': 15,
'player1_hits': 0,
'player2_hits': 2,
'longest_rally': 4
}
response = api_client.post(url, data, format='json')
Expand All @@ -151,68 +152,74 @@ def test_create_game_stat(api_client):
game_stat = GameStat.objects.first()
assert game_stat.player1_score == 10
assert game_stat.player2_score == 5
assert game_stat.total_hits == 15
assert game_stat.player1_hits == 0
assert game_stat.player2_hits == 2
assert game_stat.longest_rally == 4

@pytest.mark.django_db
def test_list_game_stat(api_client):
game1 = GameHistory.objects.create(player1_id=1, player2_id=2, winner_id=1, start_time=now())
GameStat.objects.create(game_id=game1, player1_score=10, player2_score=5, total_hits=15, longest_rally=4)
GameStat.objects.create(game_id=game1, player1_score=10, player2_score=5, player1_hits=0, player2_hits=2, longest_rally=4)

game2 = GameHistory.objects.create(player1_id=3, player2_id=4, winner_id=4, start_time=now())
GameStat.objects.create(game_id=game2, player1_score=8, player2_score=7, total_hits=20, longest_rally=5)
GameStat.objects.create(game_id=game2, player1_score=8, player2_score=7, player1_hits=20, player2_hits=0, longest_rally=5)

url = reverse('gamestat-list')
response = api_client.get(url, format='json')
assert response.status_code == status.HTTP_200_OK
assert len(response.data) == 2
assert response.data[0]['player1_score'] == 10
assert response.data[0]['player2_score'] == 5
assert response.data[0]['total_hits'] == 15
assert response.data[0]['player1_hits'] == 0
assert response.data[0]['player2_hits'] == 2
assert response.data[0]['longest_rally'] == 4
assert response.data[1]['player1_score'] == 8
assert response.data[1]['player2_score'] == 7
assert response.data[1]['total_hits'] == 20
assert response.data[1]['player1_hits'] == 20
assert response.data[1]['player2_hits'] == 0
assert response.data[1]['longest_rally'] == 5

@pytest.mark.django_db
def test_retrieve_game_stat(api_client):
game = GameHistory.objects.create(player1_id=1, player2_id=2, winner_id=1, start_time=now())
game_stat = GameStat.objects.create(game_id=game, player1_score=10, player2_score=5, total_hits=15, longest_rally=4)
game_stat = GameStat.objects.create(game_id=game, player1_score=10, player2_score=5, player1_hits=15, player2_hits=2, longest_rally=4)

url = reverse('gamestat-detail', args=[game_stat.pk])
response = api_client.get(url, format='json')
assert response.status_code == status.HTTP_200_OK
assert response.data['player1_score'] == game_stat.player1_score
assert response.data['player2_score'] == game_stat.player2_score
assert response.data['total_hits'] == game_stat.total_hits
assert response.data['player1_hits'] == game_stat.player1_hits
assert response.data['player2_hits'] == game_stat.player2_hits
assert response.data['longest_rally'] == game_stat.longest_rally

@pytest.mark.django_db
def test_update_game_stat(api_client):
game = GameHistory.objects.create(player1_id=1, player2_id=2, winner_id=1, start_time=now())
game_stat = GameStat.objects.create(game_id=game, player1_score=10, player2_score=5, total_hits=15, longest_rally=4)
game_stat = GameStat.objects.create(game_id=game, player1_score=10, player2_score=5, player1_hits=15, player2_hits=2, longest_rally=4)

url = reverse('gamestat-detail', args=[game_stat.pk])
data = {
'game_id': game.game_id,
'player1_score': 12,
'player2_score': 6,
'total_hits': 18,
'player1_hits': 18,
'player2_hits': 0,
'longest_rally': 5
}
response = api_client.put(url, data, format='json')
assert response.status_code == status.HTTP_200_OK
game_stat.refresh_from_db()
assert game_stat.player1_score == 12
assert game_stat.player2_score == 6
assert game_stat.total_hits == 18
assert game_stat.player1_hits == 18
assert game_stat.player2_hits == 0
assert game_stat.longest_rally == 5

@pytest.mark.django_db
def test_delete_game_stat(api_client):
game = GameHistory.objects.create(player1_id=1, player2_id=2, winner_id=1, start_time=now())
game_stat = GameStat.objects.create(game_id=game, player1_score=10, player2_score=5, total_hits=15, longest_rally=4)
game_stat = GameStat.objects.create(game_id=game, player1_score=10, player2_score=5, player1_hits=15, player2_hits=2, longest_rally=4)

url = reverse('gamestat-detail', args=[game_stat.pk])
response = api_client.delete(url)
Expand Down
11 changes: 9 additions & 2 deletions Backend/token_service/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,18 @@ The API runs on port 8000 and exposed to 8001.
## Tutorial to use the token_service

There are three endpoints in the token_service. The endpoints are:
- `auth/token/refresh/` - This endpoint is used to refresh the access token. to refresh the access token you need to send a request to this endpoint with the refresh token in the request body. The request will be like this:
- `auth/token/refresh/` - This endpoint is used to refresh the access token. to refresh the access token you need to send a request to this endpoint with the refresh token in the request body.
You should send the "user refresh token" in the header as brearer token and The request will be like this:

```json
{
"id": "user_id",
"refresh": "your refresh token"
}
```
It will return the new access token as a response.
```json
{
"access": "new access token"
}
```

Expand Down
Loading

0 comments on commit 87fe3cc

Please sign in to comment.