<commit_before>require_relative "../../app/services/identity_resolver"

describe IdentityResolver do
  let(:resolver) { IdentityResolver.new current_user, auth }
  let(:user_id) { "mee" }
  let(:token) { "<PASSWORD>" }

  describe "creating a new session" do
    let(:auth) do
      {
        "provider" => "beeminder",
        "uid" => user_id,
        "credentials" => {
          "token" => token
        }
      }.with_indifferent_access
    end

    context "when session does not exists" do
      let(:current_user) { nil }

      context "when the user is new" do
        it "set's correct flash message" do
          expect(resolver.flash).to eq("Signed in!")
        end
        it "inserts a new user" do
          user = resolver.sign_in_user
          expect(user).to be_kind_of(User)
          expect(user.beeminder_token).to eq(token)
          expect(user).to be_persisted
        end
      end
      context "when the user exists" do
        it "set's correct flash message" do
          expect(resolver.flash).to eq("Signed in!")
        end

        it "updates to token" do
          preexisting_user = create :user,
                                    beeminder_token: "previous_token",
                                    beeminder_user_id: user_id

          user = resolver.sign_in_user
          expect(user.id).to eq(preexisting_user.id)
          expect(user.beeminder_token).to eq(token)
          expect(user).to be_persisted
        end
      end
    end

    context "when session already exists" do
      let(:current_user) { double }

      it "notifies user" do
        expect(resolver.flash).to eq("Already signed in!")
      end
      it "does not expose user" do
        expect(resolver.sign_in_user).to be_nil
      end
    end
  end

  describe "creating new providers" do
    let(:auth) do
      {
        "provider" => "pocket",
        "uid" => user_id,
        "credentials" => {
          "token" => token
        },
        "info" => { "in" => "fo" },
        "extra" => { "ex" => "tra" }
      }.with_indifferent_access
    end
    context "when provider is new" do
      context "and a user is logged in" do
        let(:current_user) { create :user }

        it "set's correct flash message" do
          expect(resolver.flash).to eq("Connected successfully.")
        end
        it "add provider to the signed in user" do
          expect(resolver.sign_in_user).to be_nil # there is no need to sign in again
          provider = current_user.providers.where(
            name: auth.fetch("provider"),
            uid: auth.fetch("uid")
          ).first
          expect(provider).not_to be_nil
          expect(provider.credentials).to eq(auth["credentials"])
          expect(provider.extra).to eq(auth["extra"])
          expect(provider.info).to eq(auth["info"])
        end
      end
      context "and a user is not logged in" do
        let(:current_user) { nil }

        it "set's correct flash message" do
          expect(resolver.flash).to eq("Please sign in first.")
        end
        it "And doesn't set user" do
          expect(resolver.sign_in_user).to be_nil
        end
      end
    end

    context "when provider is known" do
      let!(:provider) { create :provider, uid: user_id }

      context "and its user is not logged in" do
        let(:current_user) { nil }<commit_msg>Fix false negative test to match a regex<commit_after>require_relative "../../app/services/identity_resolver"

describe IdentityResolver do
  let(:resolver) { IdentityResolver.new current_user, auth }
  let(:user_id) { "mee" }
  let(:token) { "<PASSWORD>" }

  describe "creating a new session" do
    let(:auth) do
      {
        "provider" => "beeminder",
        "uid" => user_id,
        "credentials" => {
          "token" => token
        }
      }.with_indifferent_access
    end

    context "when session does not exists" do
      let(:current_user) { nil }

      context "when the user is new" do
        it "set's correct flash message" do
          expect(resolver.flash).to eq("Signed in!")
        end
        it "inserts a new user" do
          user = resolver.sign_in_user
          expect(user).to be_kind_of(User)
          expect(user.beeminder_token).to eq(token)
          expect(user).to be_persisted
        end
      end
      context "when the user exists" do
        it "set's correct flash message" do
          expect(resolver.flash).to eq("Signed in!")
        end

        it "updates to token" do
          preexisting_user = create :user,
                                    beeminder_token: "<PASSWORD>",
                                    beeminder_user_id: user_id

          user = resolver.sign_in_user
          expect(user.id).to eq(preexisting_user.id)
          expect(user.beeminder_token).to eq(token)
          expect(user).to be_persisted
        end
      end
    end

    context "when session already exists" do
      let(:current_user) { double }

      it "notifies user" do
        expect(resolver.flash).to eq("Already signed in!")
      end
      it "does not expose user" do
        expect(resolver.sign_in_user).to be_nil
      end
    end
  end

  describe "creating new providers" do
    let(:auth) do
      {
        "provider" => "pocket",
        "uid" => user_id,
        "credentials" => {
          "token" => token
        },
        "info" => { "in" => "fo" },
        "extra" => { "ex" => "tra" }
      }.with_indifferent_access
    end
    context "when provider is new" do
      context "and a user is logged in" do
        let(:current_user) { create :user }

        it "set's correct flash message" do
          expect(resolver.flash).to match(/Connected successfully/)
        end
        it "add provider to the signed in user" do
          expect(resolver.sign_in_user).to be_nil # there is no need to sign in again
          provider = current_user.providers.where(
            name: auth.fetch("provider"),
            uid: auth.fetch("uid")
          ).first
          expect(provider).not_to be_nil
          expect(provider.credentials).to eq(auth["credentials"])
          expect(provider.extra).to eq(auth["extra"])
          expect(provider.info).to eq(auth["info"])
        end
      end
      context "and a user is not logged in" do
        let(:current_user) { nil }

        it "set's correct flash message" do
          expect(resolver.flash).to eq("Please sign in first.")
        end
        it "And doesn't set user" do
          expect(resolver.sign_in_user).to be_nil
        end
      end
    end

    context "when provider is known" do
      let!(:provider) { create :provider, uid: user_id }

      context "and its user is not logged in" do
        let(:current_user) { nil }
{'fraction_non_alphanumeric': 0.10473926608230574, 'fraction_numerical': 0.0, 'mean_word_length': 1.9410222804718218, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 30, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1545949', 'n_tokens_mistral': 2003, 'n_tokens_neox': 1958, 'n_words': 567}