<commit_before>require "rails_helper"

describe UserAnonymizer do

  describe "make_anonymous" do
    let(:admin) { Fabricate(:admin) }
    let(:user) { Fabricate(:user, username: "edward", auth_token: "<PASSWORD>") }

    subject(:make_anonymous) { described_class.make_anonymous(user, admin) }

    it "changes username" do
      make_anonymous
      expect(user.reload.username).to match(/^anon\d{3,}$/)
    end

    it "changes email address" do
      make_anonymous
      expect(user.reload.email).to eq("<EMAIL>")
    end

    it "turns off all notifications" do
      user.user_option.update_columns(
        email_always: true
      )

      make_anonymous
      user.reload
      expect(user.user_option.email_digests).to eq(false)
      expect(user.user_option.email_private_messages).to eq(false)
      expect(user.user_option.email_direct).to eq(false)
      expect(user.user_option.email_always).to eq(false)
      expect(user.user_option.mailing_list_mode).to eq(false)
    end

    it "resets profile to default values" do
      user.update_attributes( name: "Bibi", date_of_birth: 19.years.ago, title: "Super Star" )

      profile = user.user_profile(true)
      profile.update_attributes( location: "Moose Jaw",
                                 website: "www.bim.com",
                                 bio_raw: "I'm Bibi from Moosejaw. I sing and dance.",
                                 bio_cooked: "I'm Bibi from Moosejaw. I sing and dance.",
                                 profile_background: "http://example.com/bg.jpg",
                                 bio_cooked_version: 2,
                                 card_background: "http://example.com/cb.jpg")
      make_anonymous
      user.reload

      expect(user.name).not_to be_present
      expect(user.date_of_birth).to eq(nil)
      expect(user.title).not_to be_present
      expect(user.auth_token).to eq(nil)

      profile = user.user_profile(true)
      expect(profile.location).to eq(nil)
      expect(profile.website).to eq(nil)
      expect(profile.bio_cooked).to eq(nil)
      expect(profile.profile_background).to eq(nil)
      expect(profile.bio_cooked_version).to eq(nil)
      expect(profile.card_background).to eq(nil)
    end

    it "removes the avatar" do
      upload = Fabricate(:upload, user: user)
      user.user_avatar = UserAvatar.new(user_id: user.id, custom_upload_id: upload.id)
      user.uploaded_avatar_id = upload.id # chosen in user preferences
      user.save!
      expect { make_anonymous }.to change { Upload.count }.by(-1)
      user.reload
      expect(user.user_avatar).to eq(nil)
      expect(user.uploaded_avatar_id).to eq(nil)<commit_msg>Updated test to check for Site Setting full_name_required<commit_after>require "rails_helper"

describe UserAnonymizer do

  describe "make_anonymous" do
    let(:admin) { Fabricate(:admin) }
    let(:user) { Fabricate(:user, username: "edward", auth_token: "<PASSWORD>") }

    subject(:make_anonymous) { described_class.make_anonymous(user, admin) }

    it "changes username" do
      make_anonymous
      expect(user.reload.username).to match(/^anon\d{3,}$/)
    end

    it "changes email address" do
      make_anonymous
      expect(user.reload.email).to eq("<EMAIL>")
    end

    it "turns off all notifications" do
      user.user_option.update_columns(
        email_always: true
      )

      make_anonymous
      user.reload
      expect(user.user_option.email_digests).to eq(false)
      expect(user.user_option.email_private_messages).to eq(false)
      expect(user.user_option.email_direct).to eq(false)
      expect(user.user_option.email_always).to eq(false)
      expect(user.user_option.mailing_list_mode).to eq(false)
    end

    context "Site Settings do not require full name" do
      before do
        SiteSetting.full_name_required = false
      end

      it "resets profile to default values" do
        user.update_attributes( name: "Bibi", date_of_birth: 19.years.ago, title: "Super Star" )

        profile = user.user_profile(true)
        profile.update_attributes( location: "Moose Jaw",
                                   website: "www.bim.com",
                                   bio_raw: "I'm Bibi from Moosejaw. I sing and dance.",
                                   bio_cooked: "I'm Bibi from Moosejaw. I sing and dance.",
                                   profile_background: "http://example.com/bg.jpg",
                                   bio_cooked_version: 2,
                                   card_background: "http://example.com/cb.jpg")
        make_anonymous
        user.reload

        expect(user.name).not_to be_present
        expect(user.date_of_birth).to eq(nil)
        expect(user.title).not_to be_present
        expect(user.auth_token).to eq(nil)

        profile = user.user_profile(true)
        expect(profile.location).to eq(nil)
        expect(profile.website).to eq(nil)
        expect(profile.bio_cooked).to eq(nil)
        expect(profile.profile_background).to eq(nil)
        expect(profile.bio_cooked_version).to eq(nil)
        expect(profile.card_background).to eq(nil)
      end
    end

    context "Site Settings require full name" do
      before do
        SiteSetting.full_name_required = true
      end

      it "changes name to anonymized username" do
        user.update_attributes( name: "Bibi", date_of_birth: 19.years.ago, title: "Super Star" )

        make_anonymous
        user.reload

        expect(user.name).to eq(user.username)
      end
    end

    it "removes the avatar" do
      upload = Fabricate(:upload, user: user)
      user.user_avatar = UserAvatar.new(user_id: user.id, custom_upload_id: upload.id)
      user.uploaded_avatar_id = upload.id # chosen in user preferences
      user.save!
      expect { make_anonymous }.to change { Upload.count }.by(-1)
      user.reload
      expect(user.user_avatar).to eq(nil)
      expect(user.uploaded_avatar_id).to eq(nil)
{'fraction_non_alphanumeric': 0.10357624831309042, 'fraction_numerical': 0.0020242914979757085, 'mean_word_length': 2.42914979757085, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '114217', 'n_tokens_mistral': 1869, 'n_tokens_neox': 1800, 'n_words': 378}