Skip to content

Commit

Permalink
merger
Browse files Browse the repository at this point in the history
  • Loading branch information
buzuloiu committed Jun 4, 2019
1 parent ebd92d9 commit 4a938eb
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 33 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,13 @@ Welcome to my basic online store!
The API reference for this app is located here:
https://shopify-intern-api.herokuapp.com/api/v1/docs

All the endpoints have specs written for them with RSpec. see `/spec/acceptance`.
### Testing the Endpoints
All the endpoints have specs written for them with RSpec (and some 🎩). see `/spec/acceptance`.
The docs for the API are generated by running the specs. If any specs don't pass, the documentation for that endpoint will not be generated. This way a broken feature won't be put into the API reference.

### Unit Testing
Unit tests are written using the built in rails testing, FactoryBot, and Faker

## Getting Started

#### Making requests
Expand Down
3 changes: 2 additions & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
put '/carts/:id/add', to: 'carts#add_to_cart'
get '/line_items/:id/available', to: 'line_items#available'
put '/line_items/:id/purchase', to: 'line_items#purchase'
resources :products, only: [:index, :show, :purchase]
resources :products, only: [:index, :purchase]
get '/products/:id', to: 'products#show'
resources :carts, only: [:create, :show, :index]
resources :line_items
end
Expand Down
19 changes: 14 additions & 5 deletions test/controllers/carts_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,33 @@

class CartsControllerTest < ActionDispatch::IntegrationTest
setup do
@cart = carts(:one)
@cart = FactoryBot.create(:cart)
@user = FactoryBot.create(:user)
@api_key = JsonWebToken.encode(user_id: @user.id)
end

test "should get index" do
get api_v1_carts_url, as: :json
test "should list carts" do
get api_v1_carts_url,
params: { authentication: @api_key },
as: :json
assert_response :success
end

test "should create cart" do
assert_difference('Cart.count') do
post api_v1_carts_url, params: { cart: { completed_at: @cart.completed_at, total_cents: @cart.total_cents } }, as: :json
put api_v1_carts_url,
params: { cart: { completed_at: @cart.completed_at, total_cents: @cart.total_cents },
authentication: @api_key },
as: :json
end

assert_response 201
end

test "should show cart" do
get api_v1_cart_url(@cart), as: :json
get api_v1_cart_url(@cart),
params: { authentication: @api_key },
as: :json
assert_response :success
end

Expand Down
23 changes: 15 additions & 8 deletions test/controllers/line_items_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,8 @@
class LineItemsControllerTest < ActionDispatch::IntegrationTest
setup do
@line_item = line_items(:one)
end

test "should get index" do
get api_v1_line_items_url, as: :json
assert_response :success
@user = FactoryBot.create(:user)
@api_key = JsonWebToken.encode(user_id: @user.id)
end

test "should create line_item" do
Expand All @@ -19,18 +16,28 @@ class LineItemsControllerTest < ActionDispatch::IntegrationTest
end

test "should show line_item" do
get api_v1_line_item_url(@line_item), as: :json
get api_v1_line_item_url(@line_item),
params: { authentication: @api_key },
as: :json
assert_response :success
end

test "should update line_item" do
patch api_v1_line_item_url(@line_item), params: { line_item: { cart_id: @line_item.cart_id, product_id: @line_item.product_id, quantity: @line_item.quantity, total_price: @line_item.total_price_cents } }, as: :json
patch api_v1_line_item_url(@line_item),
params: { line_item: { cart_id: @line_item.cart_id,
product_id: @line_item.product_id,
quantity: @line_item.quantity,
total_price: @line_item.total_price_cents },
authentication: @api_key},
as: :json
assert_response 200
end

test "should destroy line_item" do
assert_difference('LineItem.count', -1) do
delete api_v1_line_item_url(@line_item), as: :json
delete api_v1_line_item_url(@line_item),
params: { authentication: @api_key },
as: :json
end

assert_response 204
Expand Down
33 changes: 15 additions & 18 deletions test/controllers/products_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,32 @@

class ProductsControllerTest < ActionDispatch::IntegrationTest
setup do
@product = products(:one)
@product = FactoryBot.create(:product, :in_stock)
@product_no_stock = FactoryBot.create(:product, :out_of_stock)
@user = FactoryBot.create(:user)
@api_key = JsonWebToken.encode(user_id: @user.id)
end

test "should get index" do
get api_v1_products_url, as: :json
get api_v1_products_url,
params: { authentication: @api_key },
as: :json
assert_response :success
end

test "should create product" do
assert_difference('Product.count') do
post api_v1_products_url, params: { product: { inventory_count: @product.inventory_count, price: @product.price_cents, title: @product.title } }, as: :json
end

assert_response 201
end

test "should show product" do
get api_v1_product_url(@product), as: :json
get api_v1_product_url(@product.id),
params: { authentication: @api_key },
as: :json
assert_response :success
end

test "should update product" do
put api_v1_product_url(@product), params: { product: { inventory_count: @product.inventory_count, price: @product.price_cents, title: @product.title } }, as: :json
assert_response 200
end


test "should purchase product" do
put api_v1_product_purchase_url(@product), params: { product: { inventory_count: @product.inventory_count, price: @product.price_cents, title: @product.title } }, as: :json
put api_v1_url(@product),
params: { product: { inventory_count: @product.inventory_count,
price: @product.price_cents,
title: @product.title } },
as: :json
assert_response 200
end

Expand Down
4 changes: 4 additions & 0 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,8 @@ class ActiveSupport::TestCase
fixtures :all

# Add more helper methods to be used by all tests here...
def log_in_as(user)
session[:user_id] = user.id
end

end

0 comments on commit 4a938eb

Please sign in to comment.