fogbugz_controller_spec.rb 3.17 KB
Newer Older
1 2
# frozen_string_literal: true

Jared Szechy's avatar
Jared Szechy committed
3 4 5 6 7 8 9 10 11 12 13
require 'spec_helper'

describe Import::FogbugzController do
  include ImportSpecHelper

  let(:user) { create(:user) }

  before do
    sign_in(user)
  end

Stan Hu's avatar
Stan Hu committed
14 15 16 17 18 19 20 21 22 23 24 25 26 27
  describe 'POST #callback' do
    let(:token) { FFaker::Lorem.characters(8) }
    let(:uri) { 'https://example.com' }
    let(:xml_response) { %Q(<?xml version=\"1.0\" encoding=\"UTF-8\"?><response><token><![CDATA[#{token}]]></token></response>) }

    it 'attempts to contact Fogbugz server' do
      stub_request(:post, "https://example.com/api.asp").to_return(status: 200, body: xml_response, headers: {})

      post :callback, params: { uri: uri, email: 'test@example.com', password: 'mypassword' }

      expect(session[:fogbugz_token]).to eq(token)
      expect(session[:fogbugz_uri]).to eq(uri)
      expect(response).to redirect_to(new_user_map_import_fogbugz_path)
    end
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56

    context 'verify url' do
      shared_examples 'denies local request' do |reason|
        it 'does not allow requests' do
          post :callback, params: { uri: uri, email: 'test@example.com', password: 'mypassword' }

          expect(response).to redirect_to(new_import_fogbugz_url)
          expect(flash[:alert]).to eq("Specified URL cannot be used: \"#{reason}\"")
        end
      end

      context 'when host is localhost' do
        let(:uri) { 'https://localhost:3000' }

        include_examples 'denies local request', 'Requests to localhost are not allowed'
      end

      context 'when host is on local network' do
        let(:uri) { 'http://192.168.0.1/' }

        include_examples 'denies local request', 'Requests to the local network are not allowed'
      end

      context 'when host is ftp protocol' do
        let(:uri) { 'ftp://testing' }

        include_examples 'denies local request', 'Only allowed schemes are http, https'
      end
    end
Stan Hu's avatar
Stan Hu committed
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
  end

  describe 'POST #create_user_map' do
    let(:user_map) do
      {
        "2" => {
          "name" => "Test User",
          "email" => "testuser@example.com",
          "gitlab_user" => "3"
        }
      }
    end

    it 'stores the user map in the session' do
      client = double(user_map: {})
      expect(controller).to receive(:client).and_return(client)

      post :create_user_map, params: { users: user_map }

      expect(session[:fogbugz_user_map]).to eq(user_map)
      expect(response).to redirect_to(status_import_fogbugz_path)
    end
  end

Jared Szechy's avatar
Jared Szechy committed
81 82 83 84 85 86 87
  describe 'GET status' do
    before do
      @repo = OpenStruct.new(name: 'vim')
      stub_client(valid?: true)
    end

    it 'assigns variables' do
88
      @project = create(:project, import_type: 'fogbugz', creator_id: user.id)
Jared Szechy's avatar
Jared Szechy committed
89 90 91 92 93 94 95 96 97
      stub_client(repos: [@repo])

      get :status

      expect(assigns(:already_added_projects)).to eq([@project])
      expect(assigns(:repos)).to eq([@repo])
    end

    it 'does not show already added project' do
98
      @project = create(:project, import_type: 'fogbugz', creator_id: user.id, import_source: 'vim')
Jared Szechy's avatar
Jared Szechy committed
99 100 101 102 103 104 105 106
      stub_client(repos: [@repo])

      get :status

      expect(assigns(:already_added_projects)).to eq([@project])
      expect(assigns(:repos)).to eq([])
    end
  end
107 108 109 110

  describe 'POST create' do
    it_behaves_like 'project import rate limiter'
  end
Jared Szechy's avatar
Jared Szechy committed
111
end