File: conversation_spec.rb

package info (click to toggle)
ruby-mongo 2.21.3-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 14,764 kB
  • sloc: ruby: 108,806; makefile: 5; sh: 2
file content (124 lines) | stat: -rw-r--r-- 2,793 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# frozen_string_literal: true
# rubocop:todo all

require 'spec_helper'

describe Mongo::Auth::Gssapi::Conversation do
  require_mongo_kerberos

  let(:user) do
    Mongo::Auth::User.new(user: 'test')
  end

  let(:conversation) do
    described_class.new(user, 'test.example.com')
  end

  let(:authenticator) do
    double('authenticator')
  end

  let(:connection) do
    double('connection')
  end

  before do
    expect(Mongo::Auth::Gssapi::Authenticator).to receive(:new).
      with(user, 'test.example.com').
      and_return(authenticator)
  end

  context 'when the user has a realm', if: RUBY_PLATFORM == 'java' do

    let(:user) do
      Mongo::Auth::User.new(user: 'user1@MYREALM.ME')
    end

    it 'includes the realm in the username as it was provided' do
      expect(conversation.user.name).to eq(user.name)
    end
  end

  describe '#start' do

    let(:query) do
      conversation.start(connection)
    end

    let(:selector) do
      query.selector
    end

    before do
      expect(authenticator).to receive(:initialize_challenge).and_return('test')
    end

    it 'sets the sasl start flag' do
      expect(selector[:saslStart]).to eq(1)
    end

    it 'sets the auto authorize flag' do
      expect(selector[:autoAuthorize]).to eq(1)
    end

    it 'sets the mechanism' do
      expect(selector[:mechanism]).to eq('GSSAPI')
    end

    it 'sets the payload', unless: BSON::Environment.jruby? do
      expect(selector[:payload]).to start_with('test')
    end

    it 'sets the payload', if: BSON::Environment.jruby? do
      expect(selector[:payload].data).to start_with('test')
    end
  end

  describe '#finalize' do

    let(:continue_token) do
      BSON::Environment.jruby? ? BSON::Binary.new('testing') : 'testing'
    end

    context 'when the conversation is a success' do

      let(:reply_document) do
        BSON::Document.new(
          'conversationId' => 1,
          'done' => false,
          'payload' => continue_token,
          'ok' => 1.0,
        )
      end

      let(:query) do
        conversation.finalize(reply_document, connection)
      end

      let(:selector) do
        query.selector
      end

      before do
        expect(authenticator).to receive(:evaluate_challenge).
          with('testing').and_return(continue_token)
      end

      it 'sets the conversation id' do
        expect(selector[:conversationId]).to eq(1)
      end

      it 'sets the payload', unless: BSON::Environment.jruby? do
        expect(selector[:payload]).to eq(continue_token)
      end

      it 'sets the payload', if: BSON::Environment.jruby? do
        expect(selector[:payload].data).to eq(continue_token)
      end

      it 'sets the continue flag' do
        expect(selector[:saslContinue]).to eq(1)
      end
    end
  end
end