diff --git a/app/models/Player.model.js b/app/models/Player.model.js index c480f88..002d6ce 100644 --- a/app/models/Player.model.js +++ b/app/models/Player.model.js @@ -130,8 +130,11 @@ class Player extends Model { * @returns Promise */ async to_api(with_stats = false) { - const current_week = await this.sports_data.current_play_week() - const stat = with_stats ? await this.points_for_week(current_week) : undefined + let stat + try { + const current_week = await this.sports_data.current_play_week() + stat = with_stats ? await this.points_for_week(current_week) : undefined + } catch (e) {} return { id: this.id, diff --git a/app/routing/routers/api.routes.js b/app/routing/routers/api.routes.js index e0ecbaf..62a1f23 100644 --- a/app/routing/routers/api.routes.js +++ b/app/routing/routers/api.routes.js @@ -40,13 +40,6 @@ const index = { * or middleware that are applied in order. */ get: { - // handlers should be a list of either controller:: or middleware:: references - // e.g. middleware::HomeLogger - // e.g. controller::Home.welcome - '/': [ - 'controller::Home.welcome' - ], - // Get information about the user's team '/my-team': ['controller::Teams.get_my_team'], diff --git a/test/backend/routing_routers_api.spec.js b/test/backend/routing_routers_api.spec.js new file mode 100644 index 0000000..792ae91 --- /dev/null +++ b/test/backend/routing_routers_api.spec.js @@ -0,0 +1,26 @@ +const { expect } = require('chai') +const sinon = require('sinon') + +describe('the API routes', function() { + it('should define the correct public API', function() { + const config = require('../../app/routing/routers/api.routes') + expect(config).to.be.eql({ + prefix: '/api/v1', + middleware: [ 'auth:UserOnly', 'InjectUserTeam' ], + get: { + '/my-team': ['controller::Teams.get_my_team'], + '/my-team/players': ['controller::Teams.get_my_team_players'], + '/my-team/lineup': ['controller::Teams.get_my_team_current_lineup'], + '/draft-board/available': ['controller::DraftBoard.get_available_players'], + '/matchups': ['controller::Scores.get_weekly_scores'], + '/league-standings': ['controller::Scores.get_league_standings'], + '/status': ['controller::Home.get_status'], + }, + post: { + '/my-team': ['controller::Teams.save_my_team'], + '/my-team/lineup': ['controller::Teams.save_my_team_lineup'], + '/draft-board/draft-player': ['controller::DraftBoard.draft_player_to_team'], + }, + }) + }) +}) diff --git a/test/backend/services_sports_data.spec.js b/test/backend/services_sports_data.spec.js new file mode 100644 index 0000000..add5b2c --- /dev/null +++ b/test/backend/services_sports_data.spec.js @@ -0,0 +1,66 @@ +const { expect } = require('chai') +const sinon = require('sinon') +const SportsData = require('../../app/services/sports_data.service') + +const get_inst = () => { + const inst = new SportsData() + const configs = { + get(key) { + return ({ + 'server.sports_data.season': '2020REG', + 'server.sports_data.api_key': 'fakekey', + })[key] + } + } + + inst.configs = configs + return inst +} + +describe('the sports data service', function() { + it('should be a functional class instance', function() { + expect(get_inst()).to.be.an.instanceOf(SportsData) + }) + + it('should properly format API URLs', function() { + const inst = get_inst() + + let url = '' + + url = 'https://api.sportsdata.io/v3/nfl/scores/json/some-path/1?key=fakekey' + expect(inst.url('some-path/1')).to.be.equal(url) + + url = 'https://api.sportsdata.io/v3/nfl/scores/json/some-path/other-path?key=fakekey' + expect(inst.url('some-path/other-path')).to.be.equal(url) + + url = 'https://api.sportsdata.io/v3/nfl/fubar/json/some-path/another?key=fakekey' + expect(inst.url('some-path/another', 'fubar')).to.be.equal(url) + }) + + it('should call the correct sportsdata.io endpoints', async function() { + const inst = get_inst() + + const test_endpoint = async (method, endpoint, base, args = []) => { + inst.get_request = sinon.spy() + + await inst[method](...args) + + expect(inst.get_request.calledOnce).to.be.true + expect(inst.get_request.getCall(0).args[0]).to.be.equal(endpoint) + expect(inst.get_request.getCall(0).args[1]).to.be.equal(base) + } + + await test_endpoint('get_week_player_stats', 'PlayerGameProjectionStatsByWeek/2020REG/1', 'projections', [1]) + await test_endpoint('get_active_teams', 'Teams') + await test_endpoint('get_team_players', 'Players/MYTEAM', undefined, ['MYTEAM']) + }) + + it('should have methods for is_draft_stage and current_play_week', function() { + const inst = get_inst() + + expect(inst.is_draft_stage).to.be.a('function') + expect(inst.is_draft_stage.length).to.be.equal(0) + expect(inst.current_play_week).to.be.a('function') + expect(inst.current_play_week.length).to.be.equal(0) + }) +})