Skip to content

Commit

Permalink
Merge pull request #84 from ElsonOtake/active_job
Browse files Browse the repository at this point in the history
API access data tracking
  • Loading branch information
ElsonOtake authored Dec 10, 2023
2 parents 5827fb9 + bf810a5 commit 1b47ee4
Show file tree
Hide file tree
Showing 20 changed files with 123 additions and 179 deletions.
2 changes: 1 addition & 1 deletion .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ Metrics/ClassLength:
Max: 150
Metrics/BlockLength:
AllowedMethods: ['describe']
Max: 30
Max: 36
Exclude:
- "spec/swagger_helper.rb"

Expand Down
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -159,8 +159,9 @@ Open `http://localhost:3000/` on your browser.
To run tests, run the following command:

```sh
rspec spec/features
rspec spec/models
bundle exec rspec spec/features
bundle exec rspec spec/models
bundle exec rspec spec/requests
rails test test/models
rails test test/controllers
rails test test/integration
Expand All @@ -169,7 +170,7 @@ To run tests, run the following command:

To generate the API documentation:
```sh
rake rswag
bundle exec rake rswag
```
[API documentation](http://localhost:3000/api-docs/index.html) address.

Expand Down
5 changes: 5 additions & 0 deletions app/controllers/api/v1/comments_controller.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
class Api::V1::CommentsController < ApplicationController
include TrackEvent
before_action :authorize_request
before_action :find_member_post
after_action :track_event, only: %i[create]

ALLOWED_DATA = %(text).freeze

Expand All @@ -22,6 +24,9 @@ def create

comment = @post.comments.new(data)
comment.author = current_user
session[:action] = 'create'
session[:post_author] = @post.author_id
session[:comment_length] = comment.text.size
if comment.save
render json: comment
else
Expand Down
2 changes: 1 addition & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
devise_for :members, controllers: { omniauth_callbacks: 'members/omniauth_callbacks',
sessions: "members/sessions",
registrations: "members/registrations" }
resources :members, only: %i[index show] do
resources :members, only: %i[index] do
resources :posts do
resources :comments
end
Expand Down
12 changes: 2 additions & 10 deletions spec/features/posts_index_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,14 @@
@bill = Member.create(name: 'Bill', bio: 'Teacher from Uganda.', email: '[email protected]',
password: 'password', confirmed_at: Time.now)
@hello = @lilly.posts.create(title: 'Hello', text: 'This is my first post')
# @hello = Post.create(author: @lilly, title: 'Hello', text: 'This is my first post')
# Post.create(author: @lilly, title: 'Hey', text: 'This is my second post')
@lilly.posts.create(title: 'Hey', text: 'This is my second post')

@hello.comments.create(author: @tom, text: 'First comment')
@hello.comments.create(author: @lilly, text: 'Second comment')
@hello.comments.create(author: @jim, text: 'Third comment')
@hello.comments.create(author: @bill, text: 'Fourth comment')
@hello.comments.create(author: @tom, text: 'Fifth comment')
# Comment.create(post: @hello, author: @tom, text: 'First comment')
# Comment.create(post: @hello, author: @lilly, text: 'Second comment')
# Comment.create(post: @hello, author: @jim, text: 'Third comment')
# Comment.create(post: @hello, author: @bill, text: 'Fourth comment')
# Comment.create(post: @hello, author: @tom, text: 'Fifth comment')
# Like.create(post: @hello, author: @bill)
# Like.create(post: @hello, author: @jim)
# Like.create(post: @hello, author: @tom)

@hello.likes.create(author: @bill)
@hello.likes.create(author: @jim)
@hello.likes.create(author: @tom)
Expand Down
2 changes: 1 addition & 1 deletion spec/integration/auth_login_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
end

response '401', 'Unauthorized' do
let(:id) { { error: 'unauthorized' } }
let(:id) { { error: 'Unauthorized member' } }
run_test!
end
end
Expand Down
15 changes: 15 additions & 0 deletions spec/integration/comments_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@
run_test!
end

response '401', 'Unauthorized' do
let(:error) { 'Unauthorized member' }
run_test!
end

response '404', 'Not found' do
let(:error) { 'Member and/or post not found' }
run_test!
Expand Down Expand Up @@ -53,6 +58,11 @@
run_test!
end

response '401', 'Unauthorized' do
let(:error) { 'Unauthorized member' }
run_test!
end

response '404', 'Not found' do
let(:error) { 'Member/Post/Comment not found' }
run_test!
Expand Down Expand Up @@ -87,6 +97,11 @@
run_test!
end

response '401', 'Unauthorized' do
let(:error) { 'Unauthorized member' }
run_test!
end

response '404', 'Not found' do
let(:error) { 'Member and/or post not found' }
run_test!
Expand Down
5 changes: 5 additions & 0 deletions spec/integration/members_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@
run_test!
end

response '401', 'Unauthorized' do
let(:id) { { error: 'Unauthorized member' } }
run_test!
end

response '404', 'Not found' do
let(:error) { 'Member not found' }
run_test!
Expand Down
10 changes: 10 additions & 0 deletions spec/integration/posts_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@
run_test!
end

response '401', 'Unauthorized' do
let(:id) { { error: 'Unauthorized member' } }
run_test!
end

response '404', 'Not found' do
let(:error) { 'Member/Post not found' }
run_test!
Expand Down Expand Up @@ -53,6 +58,11 @@
run_test!
end

response '401', 'Unauthorized' do
let(:id) { { error: 'Unauthorized member' } }
run_test!
end

response '404', 'Not found' do
let(:error) { 'Member/Post not found' }
run_test!
Expand Down
5 changes: 0 additions & 5 deletions spec/models/browser_analytic_spec.rb

This file was deleted.

5 changes: 0 additions & 5 deletions spec/models/counter_analytic_spec.rb

This file was deleted.

5 changes: 0 additions & 5 deletions spec/models/length_analytic_spec.rb

This file was deleted.

5 changes: 0 additions & 5 deletions spec/models/unique_analytic_spec.rb

This file was deleted.

5 changes: 5 additions & 0 deletions spec/requests/analytics_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
require 'rails_helper'

RSpec.describe 'Analytics', type: :request do
before(:each) do
@member = FactoryBot.create(:member)
sign_in @member
end

describe 'GET /index' do
it 'returns http success' do
get '/analytics/index'
Expand Down
25 changes: 22 additions & 3 deletions spec/requests/comments_spec.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,28 @@
require 'rails_helper'

RSpec.describe 'Comments', type: :request do
describe 'GET /create' do
it 'returns http success' do
get '/comments/create'
before(:each) do
@member = FactoryBot.create(:member)
@post = @member.posts.create(title: 'MyTitle', text: 'MyText')
sign_in @member
end
describe 'Test instance variables' do
it 'check that a member can be created' do
expect(@member).to be_valid
end
it 'check that a post can be created' do
expect(@post).to be_valid
end
end
describe 'GET /members/:@member/posts/:post/comments/new' do
it 'render template new' do
get new_member_post_comment_path(@member, @post)
expect(response).to render_template(:new)
end
end
describe 'POST /members/:@member/posts/:post/comments' do
it 'return http success' do
post member_post_comments_path(@member, @post), params: { comment: { text: 'My Text' } }
expect(response).to_not have_http_status(:success)
end
end
Expand Down
10 changes: 0 additions & 10 deletions spec/requests/likes_spec.rb

This file was deleted.

83 changes: 17 additions & 66 deletions spec/requests/members_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,95 +2,46 @@

RSpec.describe 'Members', type: :request do
before(:each) do
get root_path
end
describe 'GET /' do
it 'returns http success' do
expect(response).to have_http_status(:success)
end
it 'render template members/index' do
expect(response).to render_template('members/index')
end
it 'body include the placeholder "Here is a list of members"' do
expect(response.body).to_not include('Here is a list of members')
end
it 'body include the path app/views/members/index.html.erb' do
expect(response.body).to_not include('app/views/members/index.html.erb')
end
@member = FactoryBot.create(:member)
sign_in @member
end

describe 'GET /members' do
describe 'GET /' do
before(:each) do
get root_path
end
it 'returns http success' do
expect(response).to have_http_status(:success)
end
it 'render template members/index' do
it 'render index template' do
expect(response).to render_template('members/index')
end
it 'body include the placeholder "Here is a list of members"' do
expect(response.body).to_not include('Here is a list of members')
end
it 'body include the path app/views/members/index.html.erb' do
expect(response.body).to_not include('app/views/members/index.html.erb')
end
end

describe 'GET /members/1' do
before(:each) do
get member_path(1)
end
it 'returns http success' do
expect(response).to_not have_http_status(:success)
end
it 'render template members/show' do
expect(response).to_not render_template('members/show')
end
it 'body include the placeholder "Here is a detail for a given member"' do
expect(response.body).to_not include('Here is a detail for a given member')
end
it 'body include the path app/views/members/show.html.erb' do
expect(response.body).to_not include('app/views/members/show.html.erb')
end
end

describe 'GET /members/index' do
describe 'GET /members' do
before(:each) do
get root_path
get '/members'
end
it 'returns http success' do
expect(response).to have_http_status(:success)
end
it 'not render template members/index' do
it 'render index template' do
expect(response).to render_template('members/index')
end
it 'render template members/show' do
expect(response).to_not render_template('members/show')
end
it 'body include the path app/views/members/show.html.erb' do
expect(response.body).to_not include('app/views/members/show.html.erb')
end
end

describe 'GET /members/show' do
before(:each) do
get member_path(1)
end
it 'returns http success' do
expect(response).to_not have_http_status(:success)
it 'body include the API endpoints link' do
expect(response.body).to include('API endpoints')
end
it 'render template members/show' do
expect(response).to_not render_template('members/show')
it 'body include the Add post link' do
expect(response.body).to include('Add post')
end
it 'body include the placeholder "Here is a detail for a given member"' do
expect(response.body).to_not include('Here is a detail for a given member')
it 'body include the Analytics link' do
expect(response.body).to include('Analytics')
end
it "body doesn't include the placeholder 'Here is a list of members'" do
expect(response.body).to_not include('Here is a list of members')
it 'body include the Edit profile link' do
expect(response.body).to include('Edit profile')
end
it 'body include the path app/views/members/show.html.erb' do
expect(response.body).to_not include('app/views/members/show.html.erb')
it 'body include the Sign out link' do
expect(response.body).to include('Sign out')
end
end
end
Loading

0 comments on commit 1b47ee4

Please sign in to comment.