<commit_before># -*- coding: utf-8 -*-
require 'pedant/rspec/common'

describe "users", :users do

  let(:public_key_regex) do
    /^-----BEGIN (RSA )?PUBLIC KEY-----/
  end

  let(:private_key_regex) do
    /^-----BEGIN (RSA )?PRIVATE KEY-----/
  end

  # Pedant has configurable test users.
  # Selects Pedant users that are marked as associated
  let(:default_pedant_user_names) { platform.users.select(&:associate).map(&:name).sort }
  let(:default_users_body)        { default_pedant_user_names.map { |user| {"user" => {"username" => user} } } }

  context "/users endpoint" do
    let(:request_url) { "#{platform.server}/users" }

    context "GET /users" do
      let(:users_body) do
        {
          # There are other users, but these are ours, so they should always be
          # somewhere in the userspace soup.
          "pivotal" => "#{request_url}/pivotal",
          platform.bad_user.name => "#{request_url}/#{platform.bad_user.name}",
          platform.admin_user.name => "#{request_url}/#{platform.admin_user.name}",
          platform.non_admin_user.name => "#{request_url}/#{platform.non_admin_user.name}",
        }
      end
      let(:empty_users_body) do
        {}
      end
      let(:filtered_users_body) do
        {
          platform.non_admin_user.name => "#{request_url}/#{platform.non_admin_user.name}"
        }
      end

      context "superuser" do
        it "can get all users", :smoke do
          get(request_url, platform.superuser).should look_like({
              :status => 200,
              :body => users_body
            })
        end

        it "returns no users when filtering by non-existing email", :smoke do
          get("#{request_url}?email=<EMAIL>", platform.superuser).should look_like({
              :status => 200,
              :body_exact => empty_users_body
            })
        end

        it "returns a single user when filtering by that user's email address", :smoke do
          # Let's get a known user and mail address.
          response = get("#{request_url}/#{platform.non_admin_user.name}", platform.superuser)
          email = JSON.parse(response)["email"]
          get("#{request_url}?email=#{email}", platform.superuser).should look_like({
              :status => 200,
              :body_exact => filtered_users_body
            })
        end

        context "saml user" do
          let(:username) do
            "user_with_external_uid"
          end

          let(:external_auth_id) do
            "sam.l.jackson"
          end

          let(:user_options) do
            { :overrides => {
                "first_name" => "external",
                "last_name" => "user",
                "display_name" => "SAML USER",
                "external_authentication_uid" => external_auth_id
              }
            }
          end

          let(:filtered_external_users_body) do
            { username => "#{request_url}/#{username}" }
          end

          before :each do
            @user = platform.create_user(username, user_options)
          end

          after :each do
            platform.delete_user(@user)
          end

          it "returns no users when filtering by non-existing external_authentication_uid", :smoke do
            get("#{request_url}?external_authentication_uid=s<EMAIL>", platform.superuser).should look_like({
                :status => 200,
                :body_exact => empty_users_body
              })
          end

          it "returns a single user when filtering by that user's external_authentication_uid", :smoke do
            get("#{request_url}?external_authentication_uid=#{external_auth_id}", platform.superuser).should look_like({
                :status => 200,
                :body_exact => filtered_external_users_body
              })
          end
        end

        it "returns a verbose list of users upon request" do
          body = JSON.parse(get("#{request_url}?verbose=true", platform.superuser))
          [ platform.non_admin_user.name, platform.admin_user.name, platform.superuser.name ].each do |name|
            data = body[name]
            data.should_not be nil
            data.key?("first_name").should be true
            data.key?("last_name").should be true<commit_msg>Added display_name for verbose user.<commit_after># -*- coding: utf-8 -*-
require 'pedant/rspec/common'

describe "users", :users do

  let(:public_key_regex) do
    /^-----BEGIN (RSA )?PUBLIC KEY-----/
  end

  let(:private_key_regex) do
    /^-----BEGIN (RSA )?PRIVATE KEY-----/
  end

  # Pedant has configurable test users.
  # Selects Pedant users that are marked as associated
  let(:default_pedant_user_names) { platform.users.select(&:associate).map(&:name).sort }
  let(:default_users_body)        { default_pedant_user_names.map { |user| {"user" => {"username" => user} } } }

  context "/users endpoint" do
    let(:request_url) { "#{platform.server}/users" }

    context "GET /users" do
      let(:users_body) do
        {
          # There are other users, but these are ours, so they should always be
          # somewhere in the userspace soup.
          "pivotal" => "#{request_url}/pivotal",
          platform.bad_user.name => "#{request_url}/#{platform.bad_user.name}",
          platform.admin_user.name => "#{request_url}/#{platform.admin_user.name}",
          platform.non_admin_user.name => "#{request_url}/#{platform.non_admin_user.name}",
        }
      end
      let(:empty_users_body) do
        {}
      end
      let(:filtered_users_body) do
        {
          platform.non_admin_user.name => "#{request_url}/#{platform.non_admin_user.name}"
        }
      end

      context "superuser" do
        it "can get all users", :smoke do
          get(request_url, platform.superuser).should look_like({
              :status => 200,
              :body => users_body
            })
        end

        it "returns no users when filtering by non-existing email", :smoke do
          get("#{request_url}?email=<EMAIL>", platform.superuser).should look_like({
              :status => 200,
              :body_exact => empty_users_body
            })
        end

        it "returns a single user when filtering by that user's email address", :smoke do
          # Let's get a known user and mail address.
          response = get("#{request_url}/#{platform.non_admin_user.name}", platform.superuser)
          email = JSON.parse(response)["email"]
          get("#{request_url}?email=#{email}", platform.superuser).should look_like({
              :status => 200,
              :body_exact => filtered_users_body
            })
        end

        context "saml user" do
          let(:username) do
            "user_with_external_uid"
          end

          let(:external_auth_id) do
            "sam.l.jackson"
          end

          let(:user_options) do
            { :overrides => {
                "first_name" => "external",
                "last_name" => "user",
                "display_name" => "SAML USER",
                "external_authentication_uid" => external_auth_id
              }
            }
          end

          let(:filtered_external_users_body) do
            { username => "#{request_url}/#{username}" }
          end

          before :each do
            @user = platform.create_user(username, user_options)
          end

          after :each do
            platform.delete_user(@user)
          end

          it "returns no users when filtering by non-existing external_authentication_uid", :smoke do
            get("#{request_url}?external_authentication_uid=<EMAIL>", platform.superuser).should look_like({
                :status => 200,
                :body_exact => empty_users_body
              })
          end

          it "returns a single user when filtering by that user's external_authentication_uid", :smoke do
            get("#{request_url}?external_authentication_uid=#{external_auth_id}", platform.superuser).should look_like({
                :status => 200,
                :body_exact => filtered_external_users_body
              })
          end
        end

        it "returns a verbose list of users upon request" do
          body = JSON.parse(get("#{request_url}?verbose=true", platform.superuser))
          [ platform.non_admin_user.name, platform.admin_user.name, platform.superuser.name ].each do |name|
            data = body[name]
            data.should_not be nil
            data.key?("display_name").should be true
            data.key?("first_name").should be true
            data.key?("last_name").should be true
{'fraction_non_alphanumeric': 0.11933341102435614, 'fraction_numerical': 0.0037291690945111295, 'mean_word_length': 2.0292975644193434, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 53, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2998657', 'n_tokens_mistral': 2507, 'n_tokens_neox': 2385, 'n_words': 558}