Class: NeAPI::Testing::FakeAuth

Inherits:
Object
  • Object
show all
Defined in:
lib/ne_api/testing/fake_auth.rb

Overview

NeAPI::Auth のスタブクラス

Examples:

基本的な使い方

fake = NeAPI::Testing::FakeAuth.new(redirect_url: "http://localhost:3000/callback")
fake.ne_auth("uid123", "state456")
fake.tokens  #=> { access_token: "...", refresh_token: "..." }

カスタムレスポンス

fake.stub_auth_response({ "access_token" => "custom_token", ... })
fake.ne_auth("uid", "state")

認証失敗のシミュレート

fake.stub_auth_failure
fake.ne_auth("uid", "state")  #=> raises NeAPIException

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(redirect_url: "http://localhost:3000/callback") ⇒ FakeAuth

Returns a new instance of FakeAuth.



21
22
23
24
25
26
27
# File 'lib/ne_api/testing/fake_auth.rb', line 21

def initialize(redirect_url: "http://localhost:3000/callback")
  @redirect_url = redirect_url
  @ne_user = nil
  @wait_flag = false
  @auth_response = nil
  @should_fail = false
end

Instance Attribute Details

#ne_userObject

Returns the value of attribute ne_user.



19
20
21
# File 'lib/ne_api/testing/fake_auth.rb', line 19

def ne_user
  @ne_user
end

#redirect_urlObject

Returns the value of attribute redirect_url.



19
20
21
# File 'lib/ne_api/testing/fake_auth.rb', line 19

def redirect_url
  @redirect_url
end

#wait_flagObject

Returns the value of attribute wait_flag.



19
20
21
# File 'lib/ne_api/testing/fake_auth.rb', line 19

def wait_flag
  @wait_flag
end

Instance Method Details

#ne_auth(uid, state, client_id = nil, client_secret = nil) ⇒ Hash

認証処理のスタブ

Parameters:

  • uid (String)

    UID

  • state (String)

    State

  • client_id (String) (defaults to: nil)

    クライアントID

  • client_secret (String) (defaults to: nil)

    クライアントシークレット

Returns:

  • (Hash)

    認証レスポンス

Raises:



75
76
77
78
79
# File 'lib/ne_api/testing/fake_auth.rb', line 75

def ne_auth(uid, state, client_id = nil, client_secret = nil)
  raise NeAPIException, "003001:認証に失敗しました" if @should_fail

  @ne_user = @auth_response || default_auth_response(uid)
end

#reset!self

スタブをリセットする

Returns:

  • (self)


49
50
51
52
53
54
# File 'lib/ne_api/testing/fake_auth.rb', line 49

def reset!
  @ne_user = nil
  @auth_response = nil
  @should_fail = false
  self
end

#sign_in(client_id = nil, client_secret = nil) ⇒ String

ブラウザを開かずにサインインURLを返す

Parameters:

  • client_id (String) (defaults to: nil)

    クライアントID

  • client_secret (String) (defaults to: nil)

    クライアントシークレット

Returns:

  • (String)

    コールバックURLの例



61
62
63
64
65
# File 'lib/ne_api/testing/fake_auth.rb', line 61

def (client_id = nil, client_secret = nil)
  # 実際のブラウザは開かない
  # テスト用にコールバックURLを返す
  "#{@redirect_url}?uid=test_uid&state=test_state"
end

#stub_auth_failureself

認証失敗をシミュレートする

Returns:

  • (self)


41
42
43
44
# File 'lib/ne_api/testing/fake_auth.rb', line 41

def stub_auth_failure
  @should_fail = true
  self
end

#stub_auth_response(response) ⇒ self

認証レスポンスをカスタマイズする

Parameters:

  • response (Hash)

    認証レスポンス

Returns:

  • (self)


33
34
35
36
# File 'lib/ne_api/testing/fake_auth.rb', line 33

def stub_auth_response(response)
  @auth_response = response
  self
end

#tokensHash?

トークンを取得する

Returns:

  • (Hash, nil)

    トークン情報



84
85
86
87
88
89
90
91
# File 'lib/ne_api/testing/fake_auth.rb', line 84

def tokens
  return nil if @ne_user.nil?

  {
    access_token: @ne_user["access_token"],
    refresh_token: @ne_user["refresh_token"]
  }
end