From 3427cd0360f87df75d891ce4b5d74bfa04176797 Mon Sep 17 00:00:00 2001 From: wvengen Date: Mon, 15 Oct 2018 16:51:33 +0200 Subject: [PATCH] API v1 specs for user, config and navigation endpoints --- doc/swagger.v1.yml | 86 +++++++++++++++++++++++++++++++++++++ spec/api/v1/swagger_spec.rb | 34 ++++++++++++++- spec/factories/user.rb | 6 +++ 3 files changed, 124 insertions(+), 2 deletions(-) diff --git a/doc/swagger.v1.yml b/doc/swagger.v1.yml index f948268b69..eeee5f35cd 100644 --- a/doc/swagger.v1.yml +++ b/doc/swagger.v1.yml @@ -29,8 +29,94 @@ produces: - 'application/json' paths: + /user: + get: + summary: info about the currently logged-in user + tags: + - 1. User + responses: + 200: + description: success + schema: + type: object + properties: + user: + $ref: '#/definitions/User' + 401: + description: not logged-in + schema: + $ref: '#/definitions/Error401' + security: + - foodsoft_auth: ['all'] + /config: + get: + summary: configuration variables + tags: + - 7. General + responses: + 200: + description: success + schema: + type: object + 401: + description: not logged-in + schema: + $ref: '#/definitions/Error401' + security: + - foodsoft_auth: ['all'] + /navigation: + get: + summary: navigation + tags: + - 7. General + responses: + 200: + description: success + schema: + type: object + properties: + navigation: + $ref: '#/definitions/Navigation' + 401: + description: not logged-in + schema: + $ref: '#/definitions/Error401' + security: + - foodsoft_auth: ['all'] definitions: + # models + User: + type: object + properties: + id: + type: integer + name: + type: string + description: full name + email: + type: string + description: email address + locale: + type: string + description: language code + required: ['id', 'name', 'email'] + Navigation: + type: array + items: + type: object + properties: + name: + type: string + description: title + url: + type: string + description: link + items: + $ref: '#/definitions/Navigation' + required: ['name'] + minProperties: 2 # name+url or name+items + Error: type: object properties: diff --git a/spec/api/v1/swagger_spec.rb b/spec/api/v1/swagger_spec.rb index 487ed2b6f6..2ed30f41b9 100644 --- a/spec/api/v1/swagger_spec.rb +++ b/spec/api/v1/swagger_spec.rb @@ -13,7 +13,37 @@ def fetch_swagger! subject { SwaggerCheckerFile.instance_for Rails.root.join('doc', 'swagger.v1.yml') } - it 'tests all documented routes' do - is_expected.to validate_all_paths + context 'has valid paths' do + context 'user' do + # create multiple users to make sure we're getting the authenticated user, not just any + let!(:other_user_1) { create :user } + let!(:user) { create :user } + let!(:other_user_2) { create :user } + + it { is_expected.to validate(:get, '/user', 200, auth) } + it { is_expected.to validate(:get, '/user', 401) } + + context 'with invalid access token' do + let(:access_token) { 'abc' } + it { is_expected.to validate(:get, '/user', 401, auth) } + end + end + + context 'config' do + it { is_expected.to validate(:get, '/config', 200, auth) } + it { is_expected.to validate(:get, '/config', 401) } + end + + context 'navigation' do + it { is_expected.to validate(:get, '/navigation', 200, auth) } + it { is_expected.to validate(:get, '/navigation', 401) } + end + end + + # needs to be last context so it is always run at the end + context 'and finally' do + it 'tests all documented routes' do + is_expected.to validate_all_paths + end end end diff --git a/spec/factories/user.rb b/spec/factories/user.rb index bf21a9e00c..51b35809d7 100644 --- a/spec/factories/user.rb +++ b/spec/factories/user.rb @@ -15,6 +15,12 @@ create :workgroup, role_admin: true, user_ids: [user.id] end end + + trait :ordergroup do + after :create do |user, evaluator| + create :ordergroup, user_ids: [user.id] + end + end end factory :group do