Skip to content

Commit

Permalink
Merge pull request #31 from jrsmth/feature/improvements/main
Browse files Browse the repository at this point in the history
[patch] UMA-55-56: test suite extension
  • Loading branch information
jrsmth authored Dec 8, 2023
2 parents eaec1cc + 275cc93 commit c040bf4
Show file tree
Hide file tree
Showing 15 changed files with 96 additions and 28 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/main.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
- name: Install 🔧
run: pip3 install -r src/app/requirements.txt --no-cache-dir
- name: Test 📋
run: python3 -m unittest discover .
run: python3 -m pytest src
release:
runs-on: ubuntu-latest
steps:
Expand Down
7 changes: 6 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
# Releases
<!-- @LatestFirst -->

## [0.7.1] - 08/12/23
- UMA-55: Fix the test step in the GitHub Actions workflow
- UMA-56: Add tests for admin and login blueprint

## [0.7.0] - 06/12/23
- UMA-32: [BUG] If winning move is placed on the last square, the game ends in draw (ultimate only)
- UMA-49: Add delay in single player mode for the computer thinking time (randomised)
Expand Down Expand Up @@ -112,4 +116,5 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
[0.5.3]: https://github.com/jrsmth/ultima/compare/0.5.2...0.5.3
[0.6.0]: https://github.com/jrsmth/ultima/compare/0.5.3...0.6.0
[0.6.1]: https://github.com/jrsmth/ultima/compare/0.6.0...0.6.1
[0.7.0]: https://github.com/jrsmth/ultima/compare/0.6.1...0.7.0
[0.7.0]: https://github.com/jrsmth/ultima/compare/0.6.1...0.7.0
[0.7.1]: https://github.com/jrsmth/ultima/compare/0.7.0...0.7.1
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ make start:
export FLASK_ENV=$(env) && make build && gunicorn -b 0.0.0.0:8080 -k gevent -w 1 --chdir src/app app:app

make test:
export FLASK_ENV="prod" && python3 -m unittest discover ./src/app
export FLASK_ENV="dev" && python3 -m pytest src

make redis:
docker run --name ultima-redis -p 6379:6379 -d redis
Expand Down
24 changes: 24 additions & 0 deletions src/app/admin/admin_spec.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import base64
import pytest
from flask import current_app
from src.app.app import app


class AdminSpec:
client = app.test_client()

def should_not_allow_destroy_all_games_when_invalid_creds(self):
response = self.client.get('/flood')
assert response.status_code == 401

# TODO :: mock out redis and/or add redis to GH Action
@pytest.mark.skipif('current_app.config["ENV"] == "prod"')
def should_allow_destroy_all_games_when_valid_creds(self):
byte_string = "{0}:{1}".format(
current_app.config["USERNAME"],
current_app.config["PASSWORD"]
).encode()

creds = base64.b64encode(byte_string).decode("utf-8")
response = self.client.get('/flood', headers={"Authorization": "Basic " + creds})
assert response.status_code == 204
8 changes: 5 additions & 3 deletions src/app/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
from flask import Flask
from flask_httpauth import HTTPBasicAuth
from flask_socketio import SocketIO
from from_root import from_root

from src.app.admin import admin
from src.app.config.config import Config, DevConfig, ProductionConfig
from src.app.game import game
Expand All @@ -12,8 +14,8 @@


# Initialise app
templates = os.path.abspath("../resources/templates")
statics = "../resources/static"
templates = from_root("src", "resources", "templates")
statics = from_root("src", "resources", "static")
app = Flask(__name__, template_folder=templates, static_folder=statics)
socketio = SocketIO(app)
auth = HTTPBasicAuth()
Expand All @@ -39,7 +41,7 @@


# Initialise message bundle
messages = Messages('../resources/messages.properties')
messages = Messages(from_root('src', 'resources', 'messages.properties'))


# Register routes
Expand Down
21 changes: 0 additions & 21 deletions src/app/app.test.py

This file was deleted.

3 changes: 3 additions & 0 deletions src/app/config/config.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
class Config(object):
ENV = "local"
APP_URL = "http://localhost:8080"
REDIS_URL = "redis://@localhost:6379/0"
USERNAME = "noah"
Expand All @@ -7,10 +8,12 @@ class Config(object):


class DevConfig(Config):
ENV = "dev"
APP_URL = "http://localhost:8080"
REDIS_URL = "rediss://red-cklv99jj89us738u33i0:[email protected]:6379"


class ProductionConfig(Config):
ENV = "prod"
APP_URL = "https://www.ultima.jrsmth.io/"
REDIS_URL = 'redis://red-cklv99jj89us738u33i0:6379'
Empty file removed src/app/game/game.test.py
Empty file.
3 changes: 3 additions & 0 deletions src/app/game/game_spec.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# parsed_html = PyQuery(response.data.decode('utf-8'))
# player_two = parsed_html('span#player-two-name')
# assert player_two.text() == ""
Empty file removed src/app/login/login.test.py
Empty file.
41 changes: 41 additions & 0 deletions src/app/login/login_spec.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import pytest
from src.app.app import app, redis
from flask import current_app


class LoginSpec:
client = app.test_client()

# TODO :: mock out redis and/or add redis to GH Action
@pytest.mark.skipif('current_app.config["ENV"] == "prod"')
def should_load_login_page_without_error(self):
response = self.client.get("/", content_type="html/text")
assert response.status_code == 200
assert "Player Name" in response.data.decode('utf-8')

# TODO :: mock out redis and/or add redis to GH Action
@pytest.mark.skipif('current_app.config["ENV"] == "prod"')
@pytest.mark.parametrize("game_mode,player_mode", [
("standard", "single"),
("standard", "double"),
("ultimate", "single"),
("ultimate", "double")
])
def should_create_game_with_game_and_player_modes(self, game_mode, player_mode):
data = {
"name": "test",
"gameId": "",
"gameMode": game_mode,
"playerMode": player_mode
}

response = self.client.post("/", data=data)
location = response.headers["Location"]

game_id = location.split("/")[2]
game_state = redis.get_complex(game_id)
assert game_state["game_mode"] == game_mode
assert game_state["player_mode"] == player_mode

assert response.status_code == 302
assert "/game" in location
4 changes: 3 additions & 1 deletion src/app/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,6 @@ gevent
flask_socketio
shortuuid
jsons
Flask-HTTPAuth
Flask-HTTPAuth
from-root
pyquery
Empty file removed src/app/socket/socket.test.py
Empty file.
File renamed without changes.
9 changes: 9 additions & 0 deletions src/pytest.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[pytest]
python_files = *_spec.py
python_functions = should_*
python_classes = *Spec
testpaths =
app
app/admin
app/login
app/game

0 comments on commit c040bf4

Please sign in to comment.