-
-
-
-
-
diff --git a/server/config/routes.rb b/server/config/routes.rb
index d91ad920..3bb9069b 100644
--- a/server/config/routes.rb
+++ b/server/config/routes.rb
@@ -1,6 +1,12 @@
Rails.application.routes.draw do
get 'welcome/index'
+ namespace :api do
+ namespace :v1 do
+ resources :api_users
+ end
+ end
root 'welcome#index'
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
end
+
diff --git a/server/db/migrate/20190623093635_create_api_users.rb b/server/db/migrate/20190623093635_create_api_users.rb
new file mode 100644
index 00000000..ae065c6d
--- /dev/null
+++ b/server/db/migrate/20190623093635_create_api_users.rb
@@ -0,0 +1,16 @@
+class CreateApiUsers < ActiveRecord::Migration[5.2]
+ def change
+ create_table :api_users do |t|
+
+ t.string :first_name
+ t.string :last_name
+ t.string :city
+ t.string :email
+ t.string :password_digest
+
+ t.boolean :mentor
+ t.boolean :mentee
+ t.timestamps
+ end
+ end
+end
diff --git a/server/db/schema.rb b/server/db/schema.rb
new file mode 100644
index 00000000..13876ee2
--- /dev/null
+++ b/server/db/schema.rb
@@ -0,0 +1,27 @@
+# This file is auto-generated from the current state of the database. Instead
+# of editing this file, please use the migrations feature of Active Record to
+# incrementally modify your database, and then regenerate this schema definition.
+#
+# Note that this schema.rb definition is the authoritative source for your
+# database schema. If you need to create the application database on another
+# system, you should be using db:schema:load, not running all the migrations
+# from scratch. The latter is a flawed and unsustainable approach (the more migrations
+# you'll amass, the slower it'll run and the greater likelihood for issues).
+#
+# It's strongly recommended that you check this file into your version control system.
+
+ActiveRecord::Schema.define(version: 2019_06_23_093635) do
+
+ create_table "api_users", force: :cascade do |t|
+ t.string "first_name"
+ t.string "last_name"
+ t.string "city"
+ t.string "email"
+ t.string "password_digest"
+ t.boolean "mentor"
+ t.boolean "mentee"
+ t.datetime "created_at", null: false
+ t.datetime "updated_at", null: false
+ end
+
+end
diff --git a/server/db/seeds.rb b/server/db/seeds.rb
deleted file mode 100644
index 1beea2ac..00000000
--- a/server/db/seeds.rb
+++ /dev/null
@@ -1,7 +0,0 @@
-# This file should contain all the record creation needed to seed the database with its default values.
-# The data can then be loaded with the rails db:seed command (or created alongside the database with db:setup).
-#
-# Examples:
-#
-# movies = Movie.create([{ name: 'Star Wars' }, { name: 'Lord of the Rings' }])
-# Character.create(name: 'Luke', movie: movies.first)
diff --git a/server/spec/controllers/api_users_controller_spec.rb b/server/spec/controllers/api_users_controller_spec.rb
new file mode 100644
index 00000000..de265e86
--- /dev/null
+++ b/server/spec/controllers/api_users_controller_spec.rb
@@ -0,0 +1,5 @@
+require 'rails_helper'
+
+RSpec.describe Api::V1::ApiUsersController, type: :controller do
+
+end
diff --git a/server/spec/factories/api_users.rb b/server/spec/factories/api_users.rb
new file mode 100644
index 00000000..fe613662
--- /dev/null
+++ b/server/spec/factories/api_users.rb
@@ -0,0 +1,11 @@
+FactoryBot.define do
+ factory :api_user do
+ email {Faker::Internet.safe_email}
+ password_digest {Faker::Lorem.word} #Misc doesn't seem to work {Faker::Misc.password}
+ first_name {Faker::Name.first_name}
+ last_name {Faker::Name.last_name}
+ city {Faker::Address.city}
+ mentor {Faker::Boolean.boolean(0.5)}
+ mentee {Faker::Boolean.boolean(0.5)}
+ end
+end
diff --git a/server/spec/helpers/api_users_helper_spec.rb b/server/spec/helpers/api_users_helper_spec.rb
new file mode 100644
index 00000000..2eb820c9
--- /dev/null
+++ b/server/spec/helpers/api_users_helper_spec.rb
@@ -0,0 +1,15 @@
+require 'rails_helper'
+
+# Specs in this file have access to a helper object that includes
+# the ApiUsersHelper. For example:
+#
+# describe ApiUsersHelper do
+# describe "string concat" do
+# it "concats two strings with spaces" do
+# expect(helper.concat_strings("this","that")).to eq("this that")
+# end
+# end
+# end
+RSpec.describe ApiUsersHelper, type: :helper do
+ pending "Have not used this helper file function yet #{__FILE__}"
+end
diff --git a/server/spec/models/api_user_spec.rb b/server/spec/models/api_user_spec.rb
new file mode 100644
index 00000000..09a307d9
--- /dev/null
+++ b/server/spec/models/api_user_spec.rb
@@ -0,0 +1,16 @@
+require 'rails_helper'
+
+RSpec.describe ApiUser, type: :model do
+ # Association test
+ # ensure an item record has 1:m relationship with the *** model
+
+ # it { should have_many(:***).dependent(destroy)}
+
+ # Validation test
+ # ensure columns 'email' and 'password' are present before
+
+ it {should validate_presence_of(:email)}
+ it {should validate_presence_of(:password_digest)}
+
+ pending "At some stage add more contraints and invoke more resource relationships in this file #{__FILE__}"
+end
diff --git a/server/spec/rails_helper.rb b/server/spec/rails_helper.rb
new file mode 100644
index 00000000..230d682c
--- /dev/null
+++ b/server/spec/rails_helper.rb
@@ -0,0 +1,86 @@
+# This file is copied to spec/ when you run 'rails generate rspec:install'
+require 'spec_helper'
+ENV['RAILS_ENV'] ||= 'test'
+require File.expand_path('../../config/environment', __FILE__)
+# Prevent database truncation if the environment is production
+abort("The Rails environment is running in production mode!") if Rails.env.production?
+require 'rspec/rails'
+
+# Add additional requires below this line. Rails is not loaded until this point!
+require 'database_cleaner'
+Shoulda::Matchers.configure do |config|
+ config.integrate do |with|
+ with.test_framework :rspec
+ with.library :rails
+ end
+end
+
+Dir[Rails.root.join('spec/support/**/*.rb')].each { |f| require f }
+# Requires supporting ruby files with custom matchers and macros, etc, in
+# spec/support/ and its subdirectories. Files matching `spec/**/*_spec.rb` are
+# run as spec files by default. This means that files in spec/support that end
+# in _spec.rb will both be required and run as specs, causing the specs to be
+# run twice. It is recommended that you do not name files matching this glob to
+# end with _spec.rb. You can configure this pattern with the --pattern
+# option on the command line or in ~/.rspec, .rspec or `.rspec-local`.
+#
+# The following line is provided for convenience purposes. It has the downside
+# of increasing the boot-up time by auto-requiring all files in the support
+# directory. Alternatively, in the individual `*_spec.rb` files, manually
+# require only the support files necessary.
+#
+# Dir[Rails.root.join('spec', 'support', '**', '*.rb')].each { |f| require f }
+
+# Checks for pending migrations and applies them before tests are run.
+# If you are not using ActiveRecord, you can remove these lines.
+begin
+ ActiveRecord::Migration.maintain_test_schema!
+rescue ActiveRecord::PendingMigrationError => e
+ puts e.to_s.strip
+ exit 1
+end
+RSpec.configure do |config|
+ # Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
+ config.fixture_path = "#{::Rails.root}/spec/fixtures"
+
+ # If you're not using ActiveRecord, or you'd prefer not to run each of your
+ # examples within a transaction, remove the following line or assign false
+ # instead of true.
+ config.use_transactional_fixtures = true
+
+ # RSpec Rails can automatically mix in different behaviours to your tests
+ # based on their file location, for example enabling you to call `get` and
+ # `post` in specs under `spec/controllers`.
+ #
+ # You can disable this behaviour by removing the line below, and instead
+ # explicitly tag your specs with their type, e.g.:
+ #
+ # RSpec.describe UsersController, :type => :controller do
+ # # ...
+ # end
+ #
+ # The different available types are documented in the features, such as in
+ # https://relishapp.com/rspec/rspec-rails/docs
+ config.infer_spec_type_from_file_location!
+
+ # Filter lines from Rails gems in backtraces.
+ config.filter_rails_from_backtrace!
+ # arbitrary gems may also be filtered via:
+ # config.filter_gems_from_backtrace("gem name")
+
+ config.include RequestSpecHelper
+ config.include ControllerSpecHelper
+ # Add FactoryBot methods
+ config.include FactoryBot::Syntax::Methods
+
+ config.before(:suite) do
+ DatabaseCleaner.clean_with(:truncation)
+ DatabaseCleaner.strategy = :transaction
+ end
+
+ config.around(:each) do |example|
+ DatabaseCleaner.cleaning do
+ example.run
+ end
+ end
+end
diff --git a/server/spec/requests/api/v1/api_users_spec.rb b/server/spec/requests/api/v1/api_users_spec.rb
new file mode 100644
index 00000000..28687465
--- /dev/null
+++ b/server/spec/requests/api/v1/api_users_spec.rb
@@ -0,0 +1,170 @@
+require 'rails_helper'
+
+RSpec.describe Api::V1::ApiUsersController, type: :request do
+#RSpec.describe 'api_users API', type: :request do
+
+ # initialize test data
+ let!(:api_users){create_list(:api_user, 10)}
+ let(:api_user_id) {api_users.first.id}
+
+ # Test suite for GET /api/v1/api_users
+ describe 'GET /api/v1/api_users' do
+ before {get '/api/v1/api_users'}
+
+ it 'returns api_users' do
+ expect(json).not_to be_empty
+ expect(json.size).to eq(10)
+ end
+
+ it 'returns status code 200' do
+ expect(response).to have_http_status(200)
+ end
+ end
+
+ # Test suite for GET /api/v1/api_userss/:id
+ describe 'GET /api/v1/api_users/:id' do
+ before { get "/api/v1/api_users/#{api_user_id}/" }
+
+ context 'when api_user exists' do
+ it 'returns status code 200' do
+ expect(response).to have_http_status(200)
+ end
+
+ it 'returns the item' do
+ expect(json['id']).to eq(api_user_id)
+ end
+ end
+
+ context 'when api_user does not exist' do
+ let(:api_user_id) {0}
+ before { get "/api/v1/api_users/#{api_user_id}/" }
+
+ it 'returns status code 404' do
+ expect(response).to have_http_status(404)
+ end
+
+ it 'returns message informing no user with that id' do
+ expect(json["message"]).to match(/Couldn't find ApiUser with 'id'=#{api_user_id}/)
+ end
+ end
+
+ end
+
+ # Test suite POST /api/v1/api_user
+ describe 'POST /api/v1/api_users' do
+ let(:valid_attributes) do
+ # send json payload
+ { "email": "Test_email@email.com", "password_digest": "password1"}.to_json
+
+ context 'when request is valid' do
+ before { post '/api/v1/api_users', params: valid_attributes}
+
+ it 'returns status code 201' do
+ expect(response).to have_http_status(201)
+ end
+
+ it 'returns same params as entered' do
+ expect(json['email'], json['password_digest']).to eq("Test_email@email.com","password1")
+ end
+ end
+ end
+
+ context 'when the request is invalid as no params' do
+ let(:invalid_attributes) { { api_user: { email: nil } }.to_json }
+ before { post '/api/v1/api_users', params: invalid_attributes }
+
+ it 'returns status code 422' do
+ expect(response).to have_http_status(422)
+ end
+
+ it 'returns a validation failure message' do
+ expect(json['message'])
+ .to match(/param is missing or the value is empty: api_user/)
+ end
+ end
+
+ context 'when the request is invalid as only some requird params' do
+ let(:invalid_attributes) { { "api_user": { "email": "email@email.com" } }.to_json }
+ before { post '/api/v1/api_users', params: invalid_attributes }
+
+ it 'returns status code 422' do
+ expect(response).to have_http_status(422)
+ end
+ it 'returns a validation failure message' do
+ expect(json['message'])
+ .to match(/param is missing or the value is empty: api_user/)
+ end
+ end
+
+ end
+
+ # Test suite for Patch /api/v1/api_userss/:id
+ describe 'PATCH /api/v1/api_users/:id' do
+ let(:valid_attributes) do
+ # send json payload
+ { "first_name": "Bobby","last_name": "Dylan", "city": "Mexico", "email": "Test_email@email.com", "password_digest": "password1", "mentor": True, "mentee": False}.to_json
+ let(:api_user_id) {api_users.first.id}
+
+ context 'when request is valid' do
+ before { patch "/api/v1/api_users/#{api_user_id}/", params: valid_attributes}
+
+ it 'returns status code 204' do
+ expect(response).to have_http_status(204)
+ end
+ end
+
+ context 'check that parameters have updated correctly' do
+ before { get "/api/v1/api_users/#{api_user_id}/", params: valid_attributes}
+ end
+
+ it 'returns same params as entered' do
+ expect(json['first_name'],json['last_name'],json['city'], json['email'], json['password_digest'],json['mentor'],json['mentee']).to eq("Bobby", "Dylan","Mexico", "Test_email@email.com","password1", True, False)
+ end
+
+ end
+
+ context 'when api_user does not exist' do
+ let(:api_user_id) {0}
+ let(:valid_attributes) do
+ # send json payload
+ { "email": "Test_email@email.com", "password_digest": "password1"}.to_json
+ before { patch "/api/v1/api_users/#{api_user_id}/", params: valid_attributes}
+
+ it 'returns status code 404' do
+ expect(response).to have_http_status(404)
+ end
+
+ it 'returns message informing no user with that id' do
+ expect(json["message"]).to match(/Couldn't find ApiUser with 'id'=#{api_user_id}/)
+ end
+ end
+ end
+ end
+
+
+ # Test suite for Delete /api/v1/api_userss/:id
+ describe 'DELETE /api/v1/api_users/:id' do
+
+ context 'when request made to delete a user' do
+ let(:api_user_id) {api_users.first.id}
+ before { delete "/api/v1/api_users/#{api_user_id}/" }
+ it 'returns status code 204' do
+ expect(response).to have_http_status(204)
+ end
+ end
+
+ context 'when api_user does not exist' do
+ let(:api_user_id) {0}
+ before { delete "/api/v1/api_users/#{api_user_id}/" }
+
+ it 'returns status code 404' do
+ expect(response).to have_http_status(404)
+ end
+
+ it 'returns message informing no user with that id' do
+ expect(json["message"]).to match(/Couldn't find ApiUser with 'id'=#{api_user_id}/)
+ end
+ end
+ end
+
+end
diff --git a/server/spec/spec_helper.rb b/server/spec/spec_helper.rb
new file mode 100644
index 00000000..ce33d66d
--- /dev/null
+++ b/server/spec/spec_helper.rb
@@ -0,0 +1,96 @@
+# This file was generated by the `rails generate rspec:install` command. Conventionally, all
+# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
+# The generated `.rspec` file contains `--require spec_helper` which will cause
+# this file to always be loaded, without a need to explicitly require it in any
+# files.
+#
+# Given that it is always loaded, you are encouraged to keep this file as
+# light-weight as possible. Requiring heavyweight dependencies from this file
+# will add to the boot time of your test suite on EVERY test run, even for an
+# individual file that may not need all of that loaded. Instead, consider making
+# a separate helper file that requires the additional dependencies and performs
+# the additional setup, and require it from the spec files that actually need
+# it.
+#
+# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
+RSpec.configure do |config|
+ # rspec-expectations config goes here. You can use an alternate
+ # assertion/expectation library such as wrong or the stdlib/minitest
+ # assertions if you prefer.
+ config.expect_with :rspec do |expectations|
+ # This option will default to `true` in RSpec 4. It makes the `description`
+ # and `failure_message` of custom matchers include text for helper methods
+ # defined using `chain`, e.g.:
+ # be_bigger_than(2).and_smaller_than(4).description
+ # # => "be bigger than 2 and smaller than 4"
+ # ...rather than:
+ # # => "be bigger than 2"
+ expectations.include_chain_clauses_in_custom_matcher_descriptions = true
+ end
+
+ # rspec-mocks config goes here. You can use an alternate test double
+ # library (such as bogus or mocha) by changing the `mock_with` option here.
+ config.mock_with :rspec do |mocks|
+ # Prevents you from mocking or stubbing a method that does not exist on
+ # a real object. This is generally recommended, and will default to
+ # `true` in RSpec 4.
+ mocks.verify_partial_doubles = true
+ end
+
+ # This option will default to `:apply_to_host_groups` in RSpec 4 (and will
+ # have no way to turn it off -- the option exists only for backwards
+ # compatibility in RSpec 3). It causes shared context metadata to be
+ # inherited by the metadata hash of host groups and examples, rather than
+ # triggering implicit auto-inclusion in groups with matching metadata.
+ config.shared_context_metadata_behavior = :apply_to_host_groups
+
+# The settings below are suggested to provide a good initial experience
+# with RSpec, but feel free to customize to your heart's content.
+=begin
+ # This allows you to limit a spec run to individual examples or groups
+ # you care about by tagging them with `:focus` metadata. When nothing
+ # is tagged with `:focus`, all examples get run. RSpec also provides
+ # aliases for `it`, `describe`, and `context` that include `:focus`
+ # metadata: `fit`, `fdescribe` and `fcontext`, respectively.
+ config.filter_run_when_matching :focus
+
+ # Allows RSpec to persist some state between runs in order to support
+ # the `--only-failures` and `--next-failure` CLI options. We recommend
+ # you configure your source control system to ignore this file.
+ config.example_status_persistence_file_path = "spec/examples.txt"
+
+ # Limits the available syntax to the non-monkey patched syntax that is
+ # recommended. For more details, see:
+ # - http://rspec.info/blog/2012/06/rspecs-new-expectation-syntax/
+ # - http://www.teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
+ # - http://rspec.info/blog/2014/05/notable-changes-in-rspec-3/#zero-monkey-patching-mode
+ config.disable_monkey_patching!
+
+ # Many RSpec users commonly either run the entire suite or an individual
+ # file, and it's useful to allow more verbose output when running an
+ # individual spec file.
+ if config.files_to_run.one?
+ # Use the documentation formatter for detailed output,
+ # unless a formatter has already been configured
+ # (e.g. via a command-line flag).
+ config.default_formatter = "doc"
+ end
+
+ # Print the 10 slowest examples and example groups at the
+ # end of the spec run, to help surface which specs are running
+ # particularly slow.
+ config.profile_examples = 10
+
+ # Run specs in random order to surface order dependencies. If you find an
+ # order dependency and want to debug it, you can fix the order by providing
+ # the seed, which is printed after each run.
+ # --seed 1234
+ config.order = :random
+
+ # Seed global randomization in this process using the `--seed` CLI option.
+ # Setting this allows you to use `--seed` to deterministically reproduce
+ # test failures related to randomization by passing the same `--seed` value
+ # as the one that triggered the failure.
+ Kernel.srand config.seed
+=end
+end
diff --git a/server/spec/support/controller_spec_helper.rb b/server/spec/support/controller_spec_helper.rb
new file mode 100644
index 00000000..9af5f7ce
--- /dev/null
+++ b/server/spec/support/controller_spec_helper.rb
@@ -0,0 +1,27 @@
+module ControllerSpecHelper
+ # generate tokens from user id
+ def token_generator(api_user_id)
+ JsonWebToken.encode(api_user_id: api_user_id)
+ end
+
+ # generate expired tokens from user id
+ def expired_token_generator(api_user_id)
+ JsonWebToken.encode({ api_user_id: api_user_id }, (Time.now.to_i - 10))
+ end
+
+ # return valid headers
+ def valid_headers
+ {
+ "Authorization" => token_generator(api_user.id),
+ "Content-Type" => "application/json"
+ }
+ end
+
+ # return invalid headers
+ def invalid_headers
+ {
+ "Authorization" => nil,
+ "Content-Type" => "application/json"
+ }
+ end
+end
diff --git a/server/spec/support/request_spec_helper.rb b/server/spec/support/request_spec_helper.rb
new file mode 100644
index 00000000..06a2d3ea
--- /dev/null
+++ b/server/spec/support/request_spec_helper.rb
@@ -0,0 +1,6 @@
+module RequestSpecHelper
+ # Parse JSON reponse to ruby hash
+ def json
+ JSON.parse(response.body)
+ end
+end