You have not asked the beast for anything. Please search for new friends!
-<% end %>
+<%= render "shared/usersearch" %>
diff --git a/config/initializers/omniauth.rb b/config/initializers/omniauth.rb
index 32bb0da..c0ff3f5 100644
--- a/config/initializers/omniauth.rb
+++ b/config/initializers/omniauth.rb
@@ -1,6 +1,5 @@
# config/initializers/omniauth.rb
Rails.application.config.middleware.use OmniAuth::Builder do
- provider :developer unless Rails.env.production?
- # provider :twitter, ENV["TWITTER_CLIENT_ID"], ENV["TWITTER_CLIENT_SECRET"]
+ provider :developer unless Rails.env.production?
provider :instagram, ENV["INSTAGRAM_CLIENT_ID"], ENV["INSTAGRAM_CLIENT_SECRET"]
end
diff --git a/config/routes.rb b/config/routes.rb
index 24492ec..2987390 100644
--- a/config/routes.rb
+++ b/config/routes.rb
@@ -7,6 +7,9 @@
get '/search', to: 'home#search'
+ get '/refresh_ig', to: 'ig_subscriptions#refresh_ig'
+ get '/refresh_twi', to: 'twi_subscriptions#refresh_twi'
+
post "/auth/:provider/callback", to: "sessions#create"
get '/auth/:provider/callback', to: 'sessions#create'
@@ -16,11 +19,14 @@
resources :ig_subscriptions, path: "instagram_results"
- # Example of regular route:
- # get 'products/:id' => 'catalog#view'
+ get "posts/:id", to: "posts#show", as: "posts"
+
+ get 'home/subscriptions', to: 'home#subscriptions', as: "subscriptions"
+
+ delete 'home/subscription', to: 'home#unfollow', as: 'unfollow'
+
+
- # Example of named route that can be invoked with purchase_url(id: product.id)
- # get 'products/:id/purchase' => 'catalog#purchase', as: :purchase
# Example resource route (maps HTTP verbs to controller actions automatically):
# resources :products
diff --git a/public/favicon.ico b/public/favicon.ico
deleted file mode 100644
index e69de29..0000000
diff --git a/spec/controllers/home_controller_spec.rb b/spec/controllers/home_controller_spec.rb
index b6e5876..2ce03b4 100644
--- a/spec/controllers/home_controller_spec.rb
+++ b/spec/controllers/home_controller_spec.rb
@@ -2,11 +2,97 @@
require 'support/vcr_setup'
RSpec.describe HomeController, type: :controller do
+ let(:log_in) {
+ @logged_user = create :user
+ session[:user_id] = @logged_user.id
+ }
- # will need to refactor using this setup for VCR use
- # only on tests that would make an API call
- # VCR.use_cassette('whatever cassette name you want') do
- # # the body of the test would go here...
- # end
+ describe "GET #index" do
+ it "renders the index page" do
+ get :index
+ expect(subject).to render_template :index
+ expect(response).to have_http_status(200)
+ end
+
+ context "when logged in" do
+ it "user has access to their posts" do
+ log_in
+ igsub = create(:ig_sub)
+ post = create(:post)
+ @logged_user.subscriptions << igsub
+ get :index
+
+ expect(@logged_user.posts).to include post
+ end
+ end
+ end
+
+ describe "search form" do
+ context "twitter search" do
+ it "redirects to twitter results index" do
+ get :search, { website: "twitter", search: "tinder" }
+
+ expect(response).to redirect_to twi_subscriptions_path(params: {twitter_search: "tinder"})
+ end
+ end
+
+ context "instagram search" do
+ it "redirects to instragram results index" do
+ get :search, { website: "instagram", search: "kittens" }
+
+ expect(response).to redirect_to ig_subscriptions_path(params: {instagram_search: "kittens"})
+ end
+ end
+
+ context "empty search input" do
+ it "redirects to home page if search field empty" do
+ get :search, { website: "twitter", search: "" }
+
+ expect(response).to redirect_to root_path
+ expect(flash[:error]).to be_present
+ end
+ end
+
+ context "no search input" do
+ it "redirects to home page if search from url" do
+ get :search
+
+ expect(response).to redirect_to root_path
+ expect(flash[:error]).to be_present
+ end
+ end
+ end
+
+ describe "subscriptions" do
+ it "renders subscriptions view" do
+ log_in
+ get :subscriptions
+
+ expect(response).to render_template :subscriptions
+ end
+
+ it "returns an array of subscription_id, profile_pic, and username"do
+ log_in
+ sub = create(:ig_sub, id: 1, profile_pic: "pic")
+ post = create(:post)
+ @logged_user.subscriptions << sub
+ array = [1, "beastmaster", "pic"]
+ get :subscriptions
+
+ expect(assigns(:sub_array).first).to eq array
+ end
+ end
+
+ describe "unfollow" do
+ it "redirects to the subscriptions_path" do
+ log_in
+ sub = create(:ig_sub)
+ @logged_user.subscriptions << sub
+
+ get :unfollow, subscription_id: 1
+
+ expect(response).to redirect_to subscriptions_path
+ end
+ end
end
diff --git a/spec/controllers/ig_subscriptions_controller_spec.rb b/spec/controllers/ig_subscriptions_controller_spec.rb
index 9f1fa66..b32c0da 100644
--- a/spec/controllers/ig_subscriptions_controller_spec.rb
+++ b/spec/controllers/ig_subscriptions_controller_spec.rb
@@ -5,7 +5,7 @@
let(:log_in) {
logged_user = create :user
session[:user_id] = logged_user.id
- session[:access_token] = "1234454433"
+ session[:access_token] = ENV["INSTAGRAM_ACCESS_TOKEN"]
}
describe "#index" do
@@ -24,22 +24,25 @@
end
#how to test this without using actual access token????
- it "assigns @results if logged in" do
- log_in
-
- get :index, instagram_search: "lilagrc"
+ it "assigns @response if logged in" do
+ VCR.use_cassette('instagram assigns response') do
+ log_in
+ get :index, instagram_search: "lilagrc"
- expect(assigns(:results)).to_not be_nil
+ expect(assigns(:response)).to_not be_nil
+ end
end
end
describe "#create" do
it "redirects to the home page" do
- log_in
- post :create, instagram_id: "777"
+ VCR.use_cassette('instagram #create redirects home') do
+ log_in
+ post :create, instagram_id: "777"
- expect(subject).to redirect_to root_path
- expect(response).to have_http_status(302)
+ expect(subject).to redirect_to root_path
+ expect(response).to have_http_status(302)
+ end
end
it "redirects to the home page if not logged in" do
@@ -49,17 +52,71 @@
end
#associations method is adding the id to instragram, not twitter
- it "associates the twitter subscription with user" do
- log_in
- post :create, instagram_id: "777"
+ it "associates the instagram subscription with user" do
+ VCR.use_cassette('instagram #create associates sub with user') do
+ log_in
+ post :create, instagram_id: "777"
+
+ expect(assigns(:user).subscriptions).to include(Subscription.find_by(instagram_id: "777"))
+ end
+ end
+
+ it "saves the 15 most recent posts for the subscription" do
+ VCR.use_cassette('instagram create method creates posts') do
+ log_in
+ expect(Post.count).to eq 0
+
+ post :create, instagram_id: "215892539"
+ expect(Post.count).to eq 15
+ end
+ end
+
+ it "does not save any posts if not logged in" do
+ post :create, instagram_id: "215892539"
- expect(assigns(:user).subscriptions).to include(Subscription.find_by(instagram_id: "777"))
+ expect(Post.count).to eq 0
+ end
+
+ it "user can't subscribe to private IG user they don't follow IRL" do
+ VCR.use_cassette('instagram cannot follow private user') do
+ log_in
+ post :create, instagram_id: "19274450"
+
+ expect(response).to redirect_to root_path
+ expect(flash[:error]).to be_present
+ end
end
end
- # will need to refactor using this setup for VCR use- only
- # for tests that will hit the API
- # VCR.use_cassette('whatever cassette name you want') do
- # # the body of the test would go here...
- # end
+ describe "#refresh_ig" do
+ it "should create 15 posts when a user has a single subscription & refreshes" do
+ VCR.use_cassette('instagram refresh 1') do
+ log_in
+ user = User.first
+ user.subscriptions << (create :ig_sub)
+ get :refresh_ig
+
+ expect(Post.count).to eq 15
+ expect(user.posts.count).to eq 15
+ end
+ end
+
+ it "should create 0 posts when a user has not subscriptions & refreshes" do
+ VCR.use_cassette('instagram refresh 2') do
+ log_in
+ user = User.first
+ get :refresh_ig
+
+ expect(Post.count).to eq 0
+ expect(user.posts.count).to eq 0
+ end
+ end
+
+ it "redirect to twitter refresh action" do
+ log_in
+ get :refresh_ig
+
+ expect(response).to redirect_to refresh_twi_path
+ end
+ end
end
diff --git a/spec/controllers/posts_controller_spec.rb b/spec/controllers/posts_controller_spec.rb
new file mode 100644
index 0000000..2893b27
--- /dev/null
+++ b/spec/controllers/posts_controller_spec.rb
@@ -0,0 +1,26 @@
+require 'rails_helper'
+
+RSpec.describe PostsController, type: :controller do
+ describe "posts#show" do
+ let (:params) do
+ {
+ id: "beastmaster",
+ username: "beastmaster"
+ }
+ end
+
+ it "returns http success" do
+ get :show, :id => params[:username]
+
+ expect(response).to have_http_status(:success)
+ end
+
+ it "saves all posts that match the given username" do
+ @posts = Post.where(username: "beastmaster").sorted_order
+ create :post
+ get :show, {:id => params[:id], username: params[:username]}
+
+ expect(assigns(:posts)).to eq(@posts)
+ end
+ end
+end
diff --git a/spec/controllers/sessions_controller_spec.rb b/spec/controllers/sessions_controller_spec.rb
index a921a72..0a887b9 100644
--- a/spec/controllers/sessions_controller_spec.rb
+++ b/spec/controllers/sessions_controller_spec.rb
@@ -1,17 +1,16 @@
require 'rails_helper'
-#do we get rid of or par down tests for developer log in? since it's not production?
+# do we get rid of or par down tests for developer log in? since it's not production?
RSpec.describe SessionsController, type: :controller do
describe "GET #create" do
context "when using omniauth developer authorization" do
context "when successful" do
-
before { request.env["omniauth.auth"] = OmniAuth.config.mock_auth[:developer] }
it "redirects to home page" do
get :create, provider: :developer
- expect(response).to redirect_to root_path
+ expect(response).to redirect_to refresh_ig_path
end
it "creates a user" do
@@ -27,13 +26,12 @@
context "when using instagram authorization" do
context "when successful" do
-
before { request.env["omniauth.auth"] = OmniAuth.config.mock_auth[:instagram] }
it "redirects to home page" do
get :create, provider: :instagram
- expect(response).to redirect_to root_path
+ expect(response).to redirect_to refresh_ig_path
end
it "creates a user" do
@@ -49,9 +47,7 @@
end
context "when the user has already signed in through developer" do
-
before { request.env["omniauth.auth"] = OmniAuth.config.mock_auth[:developer] }
-
let(:user) { User.find_or_create_user(OmniAuth.config.mock_auth[:developer]) }
it "doesn't create another user" do
@@ -62,14 +58,13 @@
it "assigns the session[:user_id]" do
get :create, provider: :developer
+
expect(session[:user_id]).to eq user.id
end
end
context "when the user has already signed in with instagram" do
-
before { request.env["omniauth.auth"] = OmniAuth.config.mock_auth[:instagram] }
-
let(:user) { User.find_or_create_user(OmniAuth.config.mock_auth[:instagram]) }
it "doesn't create another user" do
@@ -134,7 +129,6 @@
end
end
-
describe "DELETE #destroy" do
before { request.env["omniauth.auth"] = OmniAuth.config.mock_auth[:instagram] }
@@ -147,7 +141,6 @@
end
it "removes association between session[:user_id] and user.id" do
-
delete :destroy, provider: :instagram
expect(session[:user_id]).to_not eq user.id
diff --git a/spec/controllers/twi_subscriptions_controller_spec.rb b/spec/controllers/twi_subscriptions_controller_spec.rb
index 058052f..4005d70 100644
--- a/spec/controllers/twi_subscriptions_controller_spec.rb
+++ b/spec/controllers/twi_subscriptions_controller_spec.rb
@@ -1,11 +1,10 @@
require 'rails_helper'
require 'support/vcr_setup'
-
RSpec.describe TwiSubscriptionsController, type: :controller do
let(:log_in) {
- logged_user = create :user
- session[:user_id] = logged_user.id
+ @logged_user = create :user
+ session[:user_id] = @logged_user.id
}
describe "#index" do
@@ -25,40 +24,74 @@
# will need to refactor using this setup for VCR use
# only needed for tests that hit the API
- # VCR.use_cassette('whatever cassette name you want') do
- # # the body of the test would go here...
- # end
-
- it "assigns @results if logged in" do
- log_in
+ it "assigns @response if logged in" do
+ VCR.use_cassette('twitter user search') do
+ log_in
+ get :index, twitter_search: "lolcats"
- get :index, twitter_search: "lolcats"
-
- expect(assigns(:results)).to_not be_nil
+ expect(assigns(:response)).to_not be_nil
+ end
end
end
describe "#create" do
it "redirects to the home page" do
- log_in
- post :create, twitter_id: "777"
+ VCR.use_cassette('twitter subscription creation') do
+ log_in
+ post :create, twitter_id: "494335393"
- expect(subject).to redirect_to root_path
- expect(response).to have_http_status(302)
+ expect(subject).to redirect_to root_path
+ expect(response).to have_http_status(302)
+ end
+ end
+
+ it "redirects with an error if user is private" do
+ VCR.use_cassette('twitter private subscription') do
+ log_in
+ post :create, twitter_id: "3110548712"
+
+ expect(subject).to redirect_to root_path
+ expect(flash[:error]).to be_present
+ end
end
it "redirects to the home page if not logged in" do
- post :create, twitter_id: "777"
+ post :create, twitter_id: "494335393"
expect(subject).to redirect_to root_path
end
#associations method is adding the id to instragram, not twitter
it "associates the twitter subscription with user" do
- log_in
- post :create, twitter_id: "777"
+ VCR.use_cassette('twitter subscription creation') do
+ log_in
+ post :create, twitter_id: "494335393"
+
+ expect(assigns(:user).subscriptions).to include(Subscription.find_by(twitter_id: "494335393"))
+ end
+ end
+ end
+
+ describe "twi_subscriptions#refresh_twi" do
+ it "creates new posts" do
+ VCR.use_cassette('twitter feed refresh') do
+ log_in
+ twi_sub = (create :twi_sub, twitter_id: "494335393")
+ @logged_user.subscriptions << twi_sub
+ @logged_user.subscriptions << (create :ig_sub)
+
+ get :refresh_twi
+ post = Post.last
+
+ expect(post.subscription.id).to eq twi_sub.id
+ end
+ end
+
+ it "does not create new posts if not logged in" do
+ twi_sub = (create :twi_sub, twitter_id: "494335393")
+ get :refresh_twi
- expect(assigns(:user).subscriptions).to include(Subscription.find_by(twitter_id: "777"))
+ expect(Post.count).to eq 0
end
end
end
diff --git a/spec/factories.rb b/spec/factories.rb
index 8c42124..636f657 100644
--- a/spec/factories.rb
+++ b/spec/factories.rb
@@ -12,7 +12,7 @@
end
factory :ig_sub, class: Subscription do
- instagram_id "123456"
+ instagram_id "215892539" # LOLCATSPICZ ID
end
factory :nil_sub, class: Subscription do
diff --git a/spec/helpers/home_helper_spec.rb b/spec/helpers/home_helper_spec.rb
index 7d55f93..be51a08 100644
--- a/spec/helpers/home_helper_spec.rb
+++ b/spec/helpers/home_helper_spec.rb
@@ -1,15 +1,21 @@
require 'rails_helper'
-# Specs in this file have access to a helper object that includes
-# the HomeHelper. For example:
-#
-# describe HomeHelper 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 HomeHelper, type: :helper do
+RSpec.describe ApplicationHelper, type: :helper do
+ let(:log_in) {
+ @logged_user = create :user
+ session[:user_id] = @logged_user.id
+ }
+ describe "checks to see if user has a session" do
+ it "returns nil if no session" do
+
+ expect(helper.logged_in?).to eq nil
+ end
+
+ it "returns user id if session exists" do
+ log_in
+
+ expect(helper.logged_in?).to eq @logged_user.id
+ end
+ end
end
diff --git a/spec/helpers/sessions_helper_spec.rb b/spec/helpers/sessions_helper_spec.rb
deleted file mode 100644
index f0dae30..0000000
--- a/spec/helpers/sessions_helper_spec.rb
+++ /dev/null
@@ -1,15 +0,0 @@
-require 'rails_helper'
-
-# Specs in this file have access to a helper object that includes
-# the SessionsHelper. For example:
-#
-# describe SessionsHelper 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 SessionsHelper, type: :helper do
-
-end
diff --git a/spec/models/post_spec.rb b/spec/models/post_spec.rb
index e0f3bad..8651c7c 100644
--- a/spec/models/post_spec.rb
+++ b/spec/models/post_spec.rb
@@ -1,4 +1,5 @@
require 'rails_helper'
+require 'httparty'
RSpec.describe Post, type: :model do
let(:twisub) { build(:twi_sub) }
@@ -60,4 +61,84 @@
expect(no_sub.errors.keys).to include(:subscription_id)
end
end
+
+ describe "post model methods" do
+ context "#create_all_instagram_posts" do
+
+ # It took SOOO LONG to figure out how to make this work.
+ # Kept getting a Webmock error.
+
+ # NOTE: In order to get this spec to work with the current config
+ # you'll need to go into the spec_helper.rb file and temporarily uncomment
+ # 'require webmock' and 'WebMock.allow_net_connect!'
+ # run rspec so VCR can record the response
+ # then recomment out those two...
+ it "takes array of HTTParty objects, and finds in db or creates new posts (* SEE COMMENTS ABOVE SPEC)" do
+ VCR.use_cassette('instagram refresh create_all_instagram_posts') do
+ INSTA_URI = "https://api.instagram.com/v1/users/"
+ COUNT = 15
+ subscription = (create :ig_sub)
+ access_token = ENV["INSTAGRAM_ACCESS_TOKEN"]
+
+ array_of_httparty_objects = []
+ array_of_httparty_objects << HTTParty.get(INSTA_URI + "#{subscription.instagram_id}/media/recent/?count=#{COUNT}&access_token=" + access_token)
+
+ Post.create_all_instagram_posts(array_of_httparty_objects)
+
+ expect(Post.count).to eq 15
+ end
+ end
+
+ it "takes array of HTTParty objects, and finds in db or creates new posts (* SEE COMMENTS ABOVE SPEC)" do
+ array_of_httparty_objects = []
+ Post.create_all_instagram_posts(array_of_httparty_objects)
+
+ expect(Post.count).to eq 0
+ end
+ end
+
+ context "#create_instagram_post" do
+ before :each do
+ create :post
+ end
+
+ it "saves a new post to the db if it does not already exist " do
+ newed_post = build :post, content_id: "7890"
+ Post.create_instagram_post(newed_post)
+
+ expect(Post.count).to eq 2
+ end
+
+ it "does not save a newed post if the content id already exists" do
+ newed_post = build :post
+ Post.create_instagram_post(newed_post)
+
+ expect(Post.count).to eq 1
+ end
+ end
+
+ context "#create_twitter_posts" do
+ it "takes a hash of subscriber and tweet objects, and finds in db or creates new posts" do
+ VCR.use_cassette('create_twitter_posts') do
+ twisub = (create :twi_sub, twitter_id: "494335393")
+
+ client = Twitter::REST::Client.new do |config|
+ config.consumer_key = ENV["TWITTER_CLIENT_ID"]
+ config.consumer_secret = ENV["TWITTER_CLIENT_SECRET"]
+ config.access_token = ENV["TWITTER_ACCESS_TOKEN"]
+ config.access_token_secret = ENV["TWITTER_ACCESS_SECRET"]
+ end
+
+ tweet_array = []
+ client.user_timeline(twisub.twitter_id.to_i).each do |tweet|
+ tweet_array << tweet
+ end
+ subscription_twitter_ids = {twisub.id => tweet_array}
+ Post.create_twitter_posts(subscription_twitter_ids)
+
+ expect(Post.count).to eq 20
+ end
+ end
+ end
+ end
end
diff --git a/spec/models/subscription_spec.rb b/spec/models/subscription_spec.rb
index b66a518..2058337 100644
--- a/spec/models/subscription_spec.rb
+++ b/spec/models/subscription_spec.rb
@@ -20,7 +20,7 @@
igsub.save
user.subscriptions << igsub
- expect(user.subscriptions.first.instagram_id).to eq "123456"
+ expect(user.subscriptions.first.instagram_id).to eq "215892539"
expect(igsub.users.first.uid).to eq "789"
end
@@ -58,40 +58,71 @@
expect(dupsub.errors.messages).to include(:ids)
end
end
- # describe "model methods" do
- # context "#find_twitter_id" do
- # it "finds a twitter subscription according to twitter_id" do
- # twi_subscription.save
- #
- # expect(TwiSubscription.find_twitter_id("123465")).to eq twi_subscription
- # end
- #
- # it "returns an empty ActiveRecord object if twitter_id doesn't exist" do
- # subscription = TwiSubscription.find_twitter_id("0")
- #
- # expect(subscription).to be_nil
- # expect(TwiSubscription.count).to eq 0
- # end
- # end
- #
- # context "#find_or_create_subscription" do
- # let(:twisub) { TwiSubscription.find_or_create_subscription("123465") }
- # it "if subscription does not exist, it creates it" do
- # twisub
- #
- # expect(TwiSubscription.count).to eq 1
- # expect(TwiSubscription.first.twitter_id).to eq "123465"
- # end
- #
- # it "returns created subscription" do
- # expect(twisub).to eq TwiSubscription.first
- # end
- #
- # it "if subscription does exist, it finds & returns it" do
- # twi_subscription.save
- #
- # expect(twisub).to eq twi_subscription
- # end
- # end
- # end
+
+# Model Methods!
+ describe "twitter methods" do
+ context "#find_twitter_id" do
+ it "finds subscription by twitter_id" do
+ twisub.save
+
+ expect(Subscription.find_twitter_id("123456")).to eq twisub
+ end
+
+ it "returns an empty ActiveRecord object if twitter_id doesn't exist" do
+ subscription = Subscription.find_twitter_id("0")
+
+ expect(subscription).to be_nil
+ expect(Subscription.count).to eq 0
+ end
+ end
+
+ context "#find_or_create_twi_subscription" do
+
+ it "if subscription does not exist, it creates it" do
+ Subscription.find_or_create_twi_subscription("123456")
+
+ expect(Subscription.count).to eq 1
+ expect(Subscription.first.twitter_id).to eq "123456"
+ end
+
+ it "if subscription does exist, it finds & returns it" do
+ twisub.save
+
+ expect(Subscription.find_or_create_twi_subscription("123456")).to eq twisub
+ end
+ end
+ end
+
+ describe "instagram methods" do
+ context "#find_instagram_id" do
+ it "finds subscription by instagram_id" do
+ igsub.save
+
+ expect(Subscription.find_instagram_id("215892539")).to eq igsub
+ end
+
+ it "returns an empty ActiveRecord object if instagram_id doesn't exist" do
+ subscription = Subscription.find_instagram_id("0")
+
+ expect(subscription).to be_nil
+ expect(Subscription.count).to eq 0
+ end
+ end
+
+ context "#find_or_create_ig_subscription" do
+
+ it "if subscription does not exist, it creates it" do
+ Subscription.find_or_create_ig_subscription("215892539")
+
+ expect(Subscription.count).to eq 1
+ expect(Subscription.first.instagram_id).to eq "215892539"
+ end
+
+ it "if subscription does exist, it finds & returns it" do
+ igsub.save
+
+ expect(Subscription.find_or_create_ig_subscription("215892539")).to eq igsub
+ end
+ end
+ end
end
diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb
index 570e223..8706bfe 100644
--- a/spec/models/user_spec.rb
+++ b/spec/models/user_spec.rb
@@ -7,6 +7,46 @@
let(:igsub) { build(:ig_sub) }
let(:post) { build(:post) }
+ describe "model associations" do
+ it "a user has and belongs to a subscription from instagram" do
+ user.save
+ igsub.save
+ igsub.users << user
+
+ expect(igsub.users.first.uid).to eq "789"
+ expect(user.subscriptions.first.instagram_id).to eq "215892539"
+ end
+
+ it "a user has and belongs to a subscription from twitter" do
+ user.save
+ twisub.save
+ twisub.users << user
+
+ expect(twisub.users.first.uid).to eq "789"
+ expect(user.subscriptions.first.twitter_id).to eq "123456"
+ end
+
+ it "a user has posts through instagram subscriptions" do
+ user.save
+ igsub.save
+ igsub.users << user
+ post.save
+
+ expect(user.posts.count).to eq 1
+ expect(user.posts.first).to eq post
+ end
+
+ it "a user has posts through twitter subscriptions" do
+ user.save
+ twisub.save
+ twisub.users << user
+ post.save
+
+ expect(user.posts.count).to eq 1
+ expect(user.posts.first).to eq post
+ end
+ end
+
describe "validations" do
it "is valid with a provider and uid" do
@@ -71,7 +111,6 @@
it "when called on a user object associates the provided twitter subscription" do
user.save
twisub.save
-
user.associate_subscription(twisub)
expect(user.subscriptions.count).to eq 1
@@ -81,7 +120,6 @@
it "when called on a user object associates the provided instagram subscription" do
user.save
igsub.save
-
user.associate_subscription(igsub)
expect(user.subscriptions.count).to eq 1
@@ -89,43 +127,61 @@
end
end
- describe "model associations" do
- it "a user has and belongs to a subscription from instagram" do
+ describe "#dissociate_subscription" do
+ it "dissociates sub from user" do
+ user = create(:user)
+ igsub = create(:ig_sub)
+ twisub = create(:twi_sub)
+
+ user.subscriptions << [igsub, twisub]
+
+ user.dissociate_subscription(igsub)
+
+ expect(user.subscriptions.count).to eq 1
+ end
+ end
+
+ describe "#already subscribed" do
+ it "returns true if user is already subscribed to the twitter user" do
+ user.save
+ twisub.save
+ user.subscriptions << twisub
+
+ expect(user.already_subscribed?(twisub.twitter_id)).to eq true
+ end
+
+ it "returns true if user is already subscribed to the instagram user" do
user.save
igsub.save
- igsub.users << user
+ user.subscriptions << igsub
- expect(igsub.users.first.uid).to eq "789"
- expect(user.subscriptions.first.instagram_id).to eq "123456"
+ expect(user.already_subscribed?(igsub.instagram_id)).to eq true
end
- it "a user has and belongs to a subscription from twitter" do
+ it "returns nil if user is not subscribed to the twitter user" do
user.save
twisub.save
- twisub.users << user
- expect(twisub.users.first.uid).to eq "789"
- expect(user.subscriptions.first.twitter_id).to eq "123456"
+ expect(user.already_subscribed?(twisub.twitter_id)).to eq nil
end
- it "a user has posts through instagram subscriptions" do
+ it "returns nil if user is not subscribed to the instagram user" do
user.save
igsub.save
- igsub.users << user
- post.save
- expect(user.posts.count).to eq 1
- expect(user.posts.first).to eq post
+ expect(user.already_subscribed?(igsub.instagram_id)).to eq nil
end
+ end
- it "a user has posts through twitter subscriptions" do
+ describe "#instagram_subscriptions" do
+ it "return a collection of only instagram subscriptions for a user" do
user.save
+ igsub.save
twisub.save
- twisub.users << user
- post.save
+ igsubtwo = create(:ig_sub, instagram_id: "9876543")
+ user.subscriptions << [igsub, igsubtwo, twisub]
- expect(user.posts.count).to eq 1
- expect(user.posts.first).to eq post
+ expect(user.instagram_subscriptions.count).to eq 2
end
end
end
diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb
index 3e96b0f..5a5af4f 100644
--- a/spec/spec_helper.rb
+++ b/spec/spec_helper.rb
@@ -1,6 +1,10 @@
require 'simplecov'
require 'factory_girl'
+# UNCOMMENT 'require webmock' (AND 'WebMock.allow_net_connect!' BELOW) IF YOU WANT model/post_spec.rb #create_all_instagram_posts TEST TO WORK!!
+
+# require 'webmock'
+
SimpleCov.start 'rails'
# This file was generated by the `rails generate rspec:install` command. Conventionally, all
@@ -37,6 +41,10 @@
expectations.include_chain_clauses_in_custom_matcher_descriptions = true
end
+ # UNCOMMENT 'WebMock.allow_net_connect!' (AND 'require webmock' AT TOP OF FILE) IF YOU WANT model/post_spec.rb #create_all_instagram_posts TEST TO WORK!!
+
+ # WebMock.allow_net_connect!
+
config.before(:suite) do
# Once you have enabled test mode, all requests
# to OmniAuth will be short circuited