Sha256: 074782fc01d65927396468566fa66a80c6c81f2488b212c78782cde25a197c4b

Contents?: true

Size: 1.96 KB

Versions: 1

Compression:

Stored size: 1.96 KB

Contents

require File.expand_path("../../spec_helper", __FILE__)

RSpec.describe SimpleWechat::AuthClient do
  it 'should generate authorize_url' do
    client = SimpleWechat::Client.new("theappid", "thesecret").get_auth_client
    authorize_url = client.authorize_url("http://example.com/wechat", "999")
    expected_url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=theappid&redirect_uri=http%3A%2F%2Fexample.com%2Fwechat&response_type=code&scope=snsapi_base&state=999#wechat_redirect" 
    expect(authorize_url).to eq(expected_url)
  end

  context "correct params" do
    it 'should get token' do
      client = SimpleWechat::AuthClient.new("theappid", "thesecret")
      body = %q(
      {
        "access_token":"ACCESS_TOKEN",
        "expires_in":7200,
        "refresh_token":"REFRESH_TOKEN",
        "openid":"OPENID",
        "scope":"SCOPE"
      }
      )
      stub_request(:get, "https://api.weixin.qq.com/sns/oauth2/access_token?appid=#{client.appid}&secret=#{client.secret}&code=thecode&grant_type=authorization_code").to_return(body: body)

      access_token = client.get_token("thecode")
      
      expect(access_token.access_token).to eq("ACCESS_TOKEN")
      expect(access_token.expires_in).to eq(7200)
      expect(access_token.refresh_token).to eq("REFRESH_TOKEN")
      expect(access_token.openid).to eq("OPENID")
      expect(access_token.scope).to eq("SCOPE")
    end
  end

  context "invalid params" do
    it 'should get error' do
      client = SimpleWechat::AuthClient.new("theappid", "thesecret")
      body = %q(
      {
        "errcode": 40029,
        "errmsg": "invalid code"
      }
      )
      stub_request(:get, "https://api.weixin.qq.com/sns/oauth2/access_token?appid=#{client.appid}&secret=#{client.secret}&code=thecode&grant_type=authorization_code").to_return(body: body)

      access_token = client.get_token("thecode")
      
      expect(access_token.errcode).to eq(40029)
      expect(access_token.errmsg).to eq("invalid code")
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
simple_wechat-0.0.1 spec/simple_wechat/auth_client_spec.rb