# frozen_string_literal: true require File.expand_path("spec_helper", __dir__) module Danger describe Gitlab::Client do before do # # @sam = Gitlab::User.new(1, "Sam") # @tom = Gitlab::User.new(2, "Tom") # @nic = Gitlab::User.new(3, "Nic") # @luke = Gitlab::User.new(4, "Luke") @mock_client = double(Gitlab::Client) @client_helper = Gitlab::ClientHelper.new(@mock_client) @sam = Gitlab::User.new(1, "Sam") @tom = Gitlab::User.new(2, "Tom") @nic = Gitlab::User.new(3, "Nic") @reviewers = [{'id' => 1, 'username' => "Sam"}, {'id' => 2, 'username' => "Tom"}, {'id' => 3, 'username' => "Nic"}] end it "Correctly calculates users with pending reviews" do already_reviewed = [@reviewers[0]] allow(@mock_client).to receive(:merge_requests).with(10, {:per_page=>"100", :state=>"opened"}).and_return([Gitlab::ObjectifiedHash.new({'iid' => 110, 'reviewers' => @reviewers})]) allow(@mock_client).to receive(:merge_request_approval_state).with(10, 110).and_return(Gitlab::ObjectifiedHash.new({'rules' => [{'approved_by' => already_reviewed}]})) expect(@client_helper.users_with_pending_mr_review(10)).to be == [@tom, @nic] end it "Correctly calculates users with pending reviews (multi rule)" do allow(@mock_client).to receive(:merge_requests).with(10, {:per_page=>"100", :state=>"opened"}).and_return([Gitlab::ObjectifiedHash.new({'iid' => 110, 'reviewers' => @reviewers})]) allow(@mock_client).to receive(:merge_request_approval_state).with(10, 110).and_return(Gitlab::ObjectifiedHash.new({'rules' => [{'approved_by' => [{'id' => 1, 'username' => "Sam"}]}, {'approved_by' => [{'id' => 2, 'username' => "Tom"}]}]})) expect(@client_helper.users_with_pending_mr_review(10)).to be == [@nic] end it "Fetches only active developer users for group" do active_users = [Gitlab::ObjectifiedHash.new({'id' => 1, 'username' => 'Sam', 'state' => 'active', 'access_level' => 30, 'is_using_seat' => true}), Gitlab::ObjectifiedHash.new({'id' => 2, 'username' => 'Tom', 'state' => 'active', 'access_level' => 50, 'is_using_seat' => true})] inactive_users = [Gitlab::ObjectifiedHash.new({'id' => 3, 'username' => 'OldSam', 'state' => 'inactive', 'access_level' => 30, 'is_using_seat' => true})] non_developer_users = [Gitlab::ObjectifiedHash.new({'id' => 4, 'username' => 'Reporter', 'state' => 'active', 'access_level' => 20, 'is_using_seat' => true})] bot_users = [Gitlab::ObjectifiedHash.new({'id' => 5, 'username' => 'danger_bot', 'state' => 'active', 'access_level' => 30, 'is_using_seat' => false})] allow(@mock_client).to receive(:group_search).with("mygroup").and_return([Gitlab::ObjectifiedHash.new({'id' => 10, 'full_path' => "mygroup"})]) allow(@mock_client).to receive(:group_members).with(10, {:per_page=>100, :show_seat_info=>true}).and_return(active_users + inactive_users + non_developer_users + bot_users) expect(@client_helper.fetch_users_for_group("mygroup")).to be == [@sam, @tom] end end end