Untitled

 avatar
unknown
ruby
a year ago
4.0 kB
11
Indexable
describe RatingsController, type: :request do
  let!(:user1) { create(:user, email: "user1@mail.com", confirmed_at: Time.zone.now) }
  let!(:user2) { create(:user, email: "user2@mail.com", confirmed_at: Time.zone.now) }
  let!(:post1) { create(:post, user: user1) }

  describe "POST #create" do
    it "should return 400 (bad request) when rating_score is absent" do
      post "/rating/#{post1.slug}"

      expect(response).to have_http_status(:bad_request)
    end

    it "should return 404 (not found) when post with provided slug does not exists" do
      post "/rating/random-slug-string", params: { rating_score: 5 }

      expect(response).to have_http_status(:not_found)
    end

    it "should return 400 (bad request) if user is same as post author" do
      sign_in user1
      post "/rating/#{post1.slug}", params: { rating_score: 5 }

      expect(response).to have_http_status(:bad_request)
    end

    it "should create a post rating relation for the post" do
      post "/rating/#{post1.slug}", params: { rating_score: 5 }

      expect(post1.post_rating).to_not eq nil
    end

    context "with ip address" do
      before do
        post "/rating/#{post1.slug}", params: { rating_score: 5 }, env: { HTTP_CF_CONNECTING_IP: "123.456.789.123" }
      end

      context "and rate once" do
        it "should return 200 (ok) and create a rating with ip address" do
          expect(response).to have_http_status(:ok)
          expect(post1.post_rating.ratings.count).to eq 1
          expect(post1.post_rating.ratings.last.ip_address).to eq "123.456.789.123"
        end
      end

      context "and rate twice" do
        context "with the same ip address" do
          before do
            post "/rating/#{post1.slug}", params: { rating_score: 5 }, env: { HTTP_CF_CONNECTING_IP: "123.456.789.123" }
          end

          it "should return 200 (ok) but should not create a new rating" do
            expect(response).to have_http_status(:ok)
            expect(post1.post_rating.ratings.count).to eq 1
          end
        end

        context "with a different ip address" do
          before do
            post "/rating/#{post1.slug}", params: { rating_score: 5 }, env: { HTTP_CF_CONNECTING_IP: "123.456.789.124" }
          end

          it "should return 200 (ok) and create a rating with ip address" do
            expect(response).to have_http_status(:ok)
            expect(post1.post_rating.ratings.count).to eq 2
            expect(post1.post_rating.ratings.last.ip_address).to eq "123.456.789.124"
          end
        end
      end
    end

    context "as an anonymous user" do
      before do
        post "/rating/#{post1.slug}", params: { rating_score: 5 }
      end

      it "should return 200 (ok) and create a rating without user id" do
        expect(response).to have_http_status(:ok)
        expect(post1.post_rating.ratings.count).to eq 1
        expect(post1.post_rating.ratings.last.user_id).to eq nil
      end
    end

    context "as a logged in user" do
      context "and user has not rated" do
        before do
          sign_in user2
          post "/rating/#{post1.slug}", params: { rating_score: 5 }
        end

        it "should return 200 (ok) and create a rating with user id" do
          expect(response).to have_http_status(:ok)
          expect(post1.post_rating.ratings.count).to eq 1
          expect(post1.post_rating.ratings.last.user_id).to eq user2.id
        end
      end

      context "and user has already rated" do
        let!(:post_rating1) { create(:post_rating, post: post1) }
        let!(:rating) { create(:rating, post_rating: post_rating1, user: user2, rating_score: 5) }

        before do
          sign_in user2
          post "/rating/#{post1.slug}", params: { rating_score: 5 }
        end

        it "should return 400 (bad request) and not create a new rating" do
          expect(response).to have_http_status(:bad_request)
          expect(post1.post_rating.ratings.count).to eq 1
        end
      end
    end
  end
end
Editor is loading...
Leave a Comment