shixian-weixin_authorize 1.6.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.coveralls.yml +2 -0
- data/.gitignore +18 -0
- data/.rspec +2 -0
- data/.travis.yml +11 -0
- data/Gemfile +17 -0
- data/LICENSE.txt +22 -0
- data/README.md +67 -0
- data/Rakefile +1 -0
- data/lib/weixin_authorize/api/custom.rb +124 -0
- data/lib/weixin_authorize/api/data_cube.rb +8 -0
- data/lib/weixin_authorize/api/groups.rb +49 -0
- data/lib/weixin_authorize/api/mass.rb +78 -0
- data/lib/weixin_authorize/api/media.rb +136 -0
- data/lib/weixin_authorize/api/menu.rb +36 -0
- data/lib/weixin_authorize/api/oauth.rb +38 -0
- data/lib/weixin_authorize/api/qrcode.rb +57 -0
- data/lib/weixin_authorize/api/template.rb +34 -0
- data/lib/weixin_authorize/api/user.rb +47 -0
- data/lib/weixin_authorize/api.rb +3 -0
- data/lib/weixin_authorize/carrierwave/weixin_uploader.rb +4 -0
- data/lib/weixin_authorize/client.rb +86 -0
- data/lib/weixin_authorize/config.rb +29 -0
- data/lib/weixin_authorize/handler/exceptions.rb +7 -0
- data/lib/weixin_authorize/handler/global_code.rb +89 -0
- data/lib/weixin_authorize/handler/result_handler.rb +52 -0
- data/lib/weixin_authorize/handler.rb +3 -0
- data/lib/weixin_authorize/js_ticket/object_store.rb +21 -0
- data/lib/weixin_authorize/js_ticket/redis_store.rb +41 -0
- data/lib/weixin_authorize/js_ticket/store.rb +40 -0
- data/lib/weixin_authorize/token/object_store.rb +25 -0
- data/lib/weixin_authorize/token/redis_store.rb +35 -0
- data/lib/weixin_authorize/token/store.rb +72 -0
- data/lib/weixin_authorize/version.rb +3 -0
- data/lib/weixin_authorize.rb +81 -0
- data/spec/1_fetch_access_token_spec.rb +43 -0
- data/spec/2_fetch_jsticket_spec.rb +10 -0
- data/spec/api/custom_spec.rb +58 -0
- data/spec/api/groups_spec.rb +54 -0
- data/spec/api/mass_spec.rb +70 -0
- data/spec/api/media_spec.rb +70 -0
- data/spec/api/medias/favicon.ico +0 -0
- data/spec/api/medias/ruby-logo.jpg +0 -0
- data/spec/api/menu_spec.rb +26 -0
- data/spec/api/qrcode_spec.rb +22 -0
- data/spec/api/template_spec.rb +40 -0
- data/spec/api/user_spec.rb +26 -0
- data/spec/spec_helper.rb +130 -0
- data/weixin_authorize.gemspec +34 -0
- metadata +202 -0
@@ -0,0 +1,81 @@
|
|
1
|
+
require "rest-client"
|
2
|
+
require "carrierwave"
|
3
|
+
require 'yajl/json_gem'
|
4
|
+
|
5
|
+
require "weixin_authorize/carrierwave/weixin_uploader"
|
6
|
+
require "weixin_authorize/config"
|
7
|
+
require "weixin_authorize/handler"
|
8
|
+
require "weixin_authorize/api"
|
9
|
+
require "weixin_authorize/client"
|
10
|
+
|
11
|
+
module WeixinAuthorize
|
12
|
+
|
13
|
+
# token store
|
14
|
+
module Token
|
15
|
+
autoload(:Store, "weixin_authorize/token/store")
|
16
|
+
autoload(:ObjectStore, "weixin_authorize/token/object_store")
|
17
|
+
autoload(:RedisStore, "weixin_authorize/token/redis_store")
|
18
|
+
end
|
19
|
+
|
20
|
+
module JsTicket
|
21
|
+
autoload(:Store, "weixin_authorize/js_ticket/store")
|
22
|
+
autoload(:ObjectStore, "weixin_authorize/js_ticket/object_store")
|
23
|
+
autoload(:RedisStore, "weixin_authorize/js_ticket/redis_store")
|
24
|
+
end
|
25
|
+
|
26
|
+
OK_MSG = "ok"
|
27
|
+
OK_CODE = 0
|
28
|
+
GRANT_TYPE = "client_credential"
|
29
|
+
|
30
|
+
class << self
|
31
|
+
|
32
|
+
def http_get_without_token(url, headers={}, endpoint="plain")
|
33
|
+
get_api_url = endpoint_url(endpoint, url)
|
34
|
+
load_json(resource(get_api_url).get(params: headers))
|
35
|
+
end
|
36
|
+
|
37
|
+
def http_post_without_token(url, payload={}, headers={}, endpoint="plain")
|
38
|
+
post_api_url = endpoint_url(endpoint, url)
|
39
|
+
payload = JSON.dump(payload) if endpoint == "plain" # to json if invoke "plain"
|
40
|
+
load_json(resource(post_api_url).post(payload, params: headers))
|
41
|
+
end
|
42
|
+
|
43
|
+
def resource(url)
|
44
|
+
RestClient::Resource.new(url, rest_client_options)
|
45
|
+
end
|
46
|
+
|
47
|
+
# return hash
|
48
|
+
def load_json(string)
|
49
|
+
result_hash = JSON.parse(string)
|
50
|
+
code = result_hash.delete("errcode")
|
51
|
+
en_msg = result_hash.delete("errmsg")
|
52
|
+
ResultHandler.new(code, en_msg, result_hash)
|
53
|
+
end
|
54
|
+
|
55
|
+
def endpoint_url(endpoint, url)
|
56
|
+
send("#{endpoint}_endpoint") + url
|
57
|
+
end
|
58
|
+
|
59
|
+
def plain_endpoint
|
60
|
+
"#{api_endpoint}/cgi-bin"
|
61
|
+
end
|
62
|
+
|
63
|
+
def api_endpoint
|
64
|
+
"https://api.weixin.qq.com"
|
65
|
+
end
|
66
|
+
|
67
|
+
def file_endpoint
|
68
|
+
"http://file.api.weixin.qq.com/cgi-bin"
|
69
|
+
end
|
70
|
+
|
71
|
+
def mp_endpoint(url)
|
72
|
+
"https://mp.weixin.qq.com/cgi-bin#{url}"
|
73
|
+
end
|
74
|
+
|
75
|
+
def open_endpoint(url)
|
76
|
+
"https://open.weixin.qq.com#{url}"
|
77
|
+
end
|
78
|
+
|
79
|
+
end
|
80
|
+
|
81
|
+
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
# NOTE: the rspec should be test alonely.
|
2
|
+
describe WeixinAuthorize::Client do
|
3
|
+
describe "#get access_token" do
|
4
|
+
it "return a access_token nil value before authenticate" do
|
5
|
+
expect($client.access_token).to eq(nil)
|
6
|
+
end
|
7
|
+
|
8
|
+
it "appid and appsecret shoud be valid" do
|
9
|
+
valid_info = $client.is_valid?
|
10
|
+
expect(valid_info).to eq(true)
|
11
|
+
end
|
12
|
+
|
13
|
+
it "return the same access_token in the same thing twice" do
|
14
|
+
access_token_1 = $client.get_access_token
|
15
|
+
sleep 5
|
16
|
+
access_token_2 = $client.get_access_token
|
17
|
+
expect(access_token_1).to eq(access_token_2)
|
18
|
+
end
|
19
|
+
|
20
|
+
it "return errorcode and errormsg when appid or appsecret is invalid" do
|
21
|
+
$client_1 = WeixinAuthorize::Client.new("appid", "app_secret")
|
22
|
+
valid_info = $client_1.is_valid?
|
23
|
+
expect(valid_info).to eq(false)
|
24
|
+
end
|
25
|
+
|
26
|
+
it "#get_access_token should raise error if app_secret or appid is invalid" do
|
27
|
+
$client_2 = WeixinAuthorize::Client.new("appid_2", "app_secret_2")
|
28
|
+
expect{$client_2.get_access_token}.to raise_error(RuntimeError)
|
29
|
+
end
|
30
|
+
|
31
|
+
it "#token_expired return the different access_token after token is expired" do
|
32
|
+
token_1 = $client.get_access_token
|
33
|
+
if WeixinAuthorize.weixin_redis.nil?
|
34
|
+
$client.expired_at = Time.now.to_i - 7300
|
35
|
+
else
|
36
|
+
WeixinAuthorize.weixin_redis.del($client.redis_key)
|
37
|
+
end
|
38
|
+
token_2 = $client.get_access_token
|
39
|
+
expect(token_1).to_not eq(token_2)
|
40
|
+
end
|
41
|
+
|
42
|
+
end
|
43
|
+
end
|
@@ -0,0 +1,10 @@
|
|
1
|
+
describe WeixinAuthorize::Client do
|
2
|
+
describe "#get jsticket" do
|
3
|
+
it "return the same jsticket in the same thing twice" do
|
4
|
+
js_ticket_1 = $client.get_jsticket
|
5
|
+
sleep 5
|
6
|
+
js_ticket_2 = $client.get_jsticket
|
7
|
+
expect(js_ticket_1).to eq(js_ticket_2)
|
8
|
+
end
|
9
|
+
end
|
10
|
+
end
|
@@ -0,0 +1,58 @@
|
|
1
|
+
describe WeixinAuthorize::Api::Custom do
|
2
|
+
let(:text_message) do
|
3
|
+
"text Custom message"
|
4
|
+
end
|
5
|
+
|
6
|
+
let(:image_path) do
|
7
|
+
"#{File.dirname(__FILE__)}/medias/ruby-logo.jpg"
|
8
|
+
end
|
9
|
+
|
10
|
+
let(:image_file) do
|
11
|
+
File.new(image_path)
|
12
|
+
end
|
13
|
+
|
14
|
+
it "#send_text_custom" do
|
15
|
+
response = $client.send_text_custom(ENV["OPENID"], text_message)
|
16
|
+
expect(response.code).to eq(WeixinAuthorize::OK_CODE)
|
17
|
+
end
|
18
|
+
|
19
|
+
it "#send_news_custom" do
|
20
|
+
articles = [{
|
21
|
+
"title" => "Happy Day",
|
22
|
+
"description" => "Is Really A Happy Day",
|
23
|
+
"url" => "http://www.baidu.com",
|
24
|
+
"picurl" => "http://www.baidu.com/img/bdlogo.gif"
|
25
|
+
},
|
26
|
+
{
|
27
|
+
"title" => "Happy Day",
|
28
|
+
"description" => "Is Really A Happy Day",
|
29
|
+
"url" => "http://www.baidu.com",
|
30
|
+
"picurl"=> "http://www.baidu.com/img/bdlogo.gif"
|
31
|
+
}]
|
32
|
+
response = $client.send_news_custom(ENV["OPENID"], articles)
|
33
|
+
expect(response.code).to eq(WeixinAuthorize::OK_CODE)
|
34
|
+
end
|
35
|
+
|
36
|
+
it "#send_image_custom" do
|
37
|
+
image = $client.upload_media(image_file, "image")
|
38
|
+
media_id = image.result["media_id"]
|
39
|
+
response = $client.send_image_custom(ENV["OPENID"], media_id)
|
40
|
+
expect(response.code).to eq(WeixinAuthorize::OK_CODE)
|
41
|
+
end
|
42
|
+
|
43
|
+
it "#send_video_custom" do
|
44
|
+
pending("The test must have a media_id")
|
45
|
+
this_should_not_get_executed
|
46
|
+
end
|
47
|
+
|
48
|
+
it "#send_music_custom" do
|
49
|
+
pending("The test must have a media_id")
|
50
|
+
this_should_not_get_executed
|
51
|
+
end
|
52
|
+
|
53
|
+
it "#send_voice_custom" do
|
54
|
+
pending("The test must have a media_id")
|
55
|
+
this_should_not_get_executed
|
56
|
+
end
|
57
|
+
|
58
|
+
end
|
@@ -0,0 +1,54 @@
|
|
1
|
+
describe WeixinAuthorize::Api::Groups do
|
2
|
+
|
3
|
+
let(:group_name) do
|
4
|
+
"test group_name"
|
5
|
+
end
|
6
|
+
|
7
|
+
let(:group_name_2) do
|
8
|
+
"test group_name_2"
|
9
|
+
end
|
10
|
+
|
11
|
+
it "create a group" do
|
12
|
+
response = $client.create_group(group_name)
|
13
|
+
if response.code == WeixinAuthorize::OK_CODE
|
14
|
+
expect(response.result["group"]["name"]).to eq(group_name)
|
15
|
+
else
|
16
|
+
expect(response.code).to eq(-1)
|
17
|
+
puts "SB WEIXIN says: system error"
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
it "get groups" do
|
22
|
+
groups = $client.groups
|
23
|
+
expect(groups.result["groups"][-1]["name"]).to eq(group_name)
|
24
|
+
end
|
25
|
+
|
26
|
+
it "#get_group_for ENV['OPENID']" do
|
27
|
+
group = $client.get_group_for(ENV["OPENID"])
|
28
|
+
expect(group.result.keys).to eq(["groupid"])
|
29
|
+
end
|
30
|
+
|
31
|
+
it "#update_group_name" do
|
32
|
+
response = $client.create_group(group_name)
|
33
|
+
if response.code != WeixinAuthorize::OK_CODE
|
34
|
+
expect(response.code).to eq(-1)
|
35
|
+
puts "SB WEIXIN says: system error"
|
36
|
+
else
|
37
|
+
expect(response.result["group"]["name"]).to eq(group_name)
|
38
|
+
response = $client.update_group_name(response.result["group"]["id"], group_name_2)
|
39
|
+
expect(response.code).to eq(WeixinAuthorize::OK_CODE)
|
40
|
+
groups = $client.groups
|
41
|
+
expect(groups.result["groups"][-1]["name"]).to eq(group_name_2)
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
it "#update_group_for_openid" do
|
46
|
+
groups = $client.groups
|
47
|
+
last_group_id = groups.result["groups"][-1]["id"]
|
48
|
+
$client.update_group_for_openid(ENV["OPENID"], last_group_id)
|
49
|
+
group = $client.get_group_for(ENV["OPENID"])
|
50
|
+
expect(group.result["groupid"]).to eq(last_group_id)
|
51
|
+
$client.update_group_for_openid(ENV["OPENID"], 0)
|
52
|
+
end
|
53
|
+
|
54
|
+
end
|
@@ -0,0 +1,70 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe WeixinAuthorize::Api::Mass do
|
4
|
+
|
5
|
+
let(:image_jpg_path) do
|
6
|
+
"#{File.dirname(__FILE__)}/medias/ruby-logo.jpg"
|
7
|
+
end
|
8
|
+
|
9
|
+
let(:news_media_1) do
|
10
|
+
{
|
11
|
+
"thumb_media_id" => "",
|
12
|
+
"author" => "lanrion",
|
13
|
+
"title" => "Happy Day",
|
14
|
+
"content_source_url" => "www.qq.com",
|
15
|
+
"content" => "content",
|
16
|
+
"digest" => "digest"
|
17
|
+
}
|
18
|
+
end
|
19
|
+
|
20
|
+
let(:image_media_id) do
|
21
|
+
# Get image media_id
|
22
|
+
image_media = $client.upload_media(image_jpg_path, "image")
|
23
|
+
media_id = image_media.result["media_id"]
|
24
|
+
media_id
|
25
|
+
end
|
26
|
+
|
27
|
+
let(:mass_media_id) do
|
28
|
+
media = {"thumb_media_id" => image_media_id}
|
29
|
+
news_media = news_media_1.merge(media)
|
30
|
+
|
31
|
+
# upload news media
|
32
|
+
response = $client.upload_mass_news([news_media])
|
33
|
+
mass_media_id = response.result["media_id"]
|
34
|
+
mass_media_id
|
35
|
+
end
|
36
|
+
|
37
|
+
it "#upload_mass_news" do
|
38
|
+
media = {"thumb_media_id" => image_media_id}
|
39
|
+
news_media = news_media_1.merge(media)
|
40
|
+
|
41
|
+
# upload news media
|
42
|
+
response = $client.upload_mass_news([news_media])
|
43
|
+
expect(response.code).to eq(WeixinAuthorize::OK_CODE)
|
44
|
+
expect(response.result.keys).to eq(["type", "media_id", "created_at"])
|
45
|
+
end
|
46
|
+
|
47
|
+
it "#mass_with_group with mpnews" do
|
48
|
+
response = $client.mass_with_group("1", "mass_group_text", "text")
|
49
|
+
expect(response.code).to eq(WeixinAuthorize::OK_CODE)
|
50
|
+
end
|
51
|
+
|
52
|
+
it "#mass_with_groug send to all" do
|
53
|
+
response = $client.mass_with_group("1", "mass_group_text_to_all", "text", true)
|
54
|
+
expect(response.code).to eq(WeixinAuthorize::OK_CODE)
|
55
|
+
end
|
56
|
+
|
57
|
+
it "#mass_with_openids with mpnews and can delete message" do
|
58
|
+
response = $client.mass_with_openids([ENV["OPENID"]], mass_media_id)
|
59
|
+
expect(response.code).to eq(WeixinAuthorize::OK_CODE)
|
60
|
+
expect(response.result.keys).to eq(["msg_id"])
|
61
|
+
delete_res = $client.mass_delete_with_msgid(response.result["msg_id"])
|
62
|
+
expect(delete_res.code).to eq(WeixinAuthorize::OK_CODE)
|
63
|
+
end
|
64
|
+
|
65
|
+
it "#mass_preview can preview by openid" do
|
66
|
+
response = $client.mass_preview(ENV["OPENID"], "mass_text", "text")
|
67
|
+
expect(response.code).to eq(WeixinAuthorize::OK_CODE)
|
68
|
+
end
|
69
|
+
|
70
|
+
end
|
@@ -0,0 +1,70 @@
|
|
1
|
+
describe WeixinAuthorize::Api::Media do
|
2
|
+
|
3
|
+
let(:image_jpg_path) do
|
4
|
+
"#{File.dirname(__FILE__)}/medias/ruby-logo.jpg"
|
5
|
+
end
|
6
|
+
|
7
|
+
let(:image_ico_path) do
|
8
|
+
"#{File.dirname(__FILE__)}/medias/favicon.ico"
|
9
|
+
end
|
10
|
+
|
11
|
+
let(:image_jpg_file) do
|
12
|
+
File.new(image_jpg_path)
|
13
|
+
end
|
14
|
+
|
15
|
+
let(:image_ico_file) do
|
16
|
+
File.new(image_ico_path)
|
17
|
+
end
|
18
|
+
|
19
|
+
let(:remote_png_path) do
|
20
|
+
"https://ruby-china-files.b0.upaiyun.com/user/big_avatar/273.jpg"
|
21
|
+
end
|
22
|
+
|
23
|
+
let(:remote_jpg_path) do
|
24
|
+
"http://g.hiphotos.baidu.com/baike/c0%3Dbaike80%2C5%2C5%2C80%2C26/sign=ce55457e4334970a537e187df4a3baad/03087bf40ad162d99455ef4d13dfa9ec8b13632762d0ed14.jpg"
|
25
|
+
end
|
26
|
+
|
27
|
+
it "can upload a jpg File image" do
|
28
|
+
response = $client.upload_media(image_jpg_file, "image")
|
29
|
+
expect(response.code).to eq(WeixinAuthorize::OK_CODE)
|
30
|
+
expect(response.result.keys).to eq(["type", "media_id", "created_at"])
|
31
|
+
end
|
32
|
+
|
33
|
+
it "can upload a ico File image" do
|
34
|
+
response = $client.upload_media(image_ico_file, "image")
|
35
|
+
expect(response.code).to eq(WeixinAuthorize::OK_CODE)
|
36
|
+
expect(response.result.keys).to eq(["type", "media_id", "created_at"])
|
37
|
+
end
|
38
|
+
|
39
|
+
it "can upload a local image" do
|
40
|
+
response = $client.upload_media(image_jpg_path, "image")
|
41
|
+
expect(response.code).to eq(WeixinAuthorize::OK_CODE)
|
42
|
+
expect(response.result.keys).to eq(["type", "media_id", "created_at"])
|
43
|
+
end
|
44
|
+
|
45
|
+
it "can upload a local ico image" do
|
46
|
+
response = $client.upload_media(image_ico_path, "image")
|
47
|
+
expect(response.code).to eq(WeixinAuthorize::OK_CODE)
|
48
|
+
expect(response.result.keys).to eq(["type", "media_id", "created_at"])
|
49
|
+
end
|
50
|
+
|
51
|
+
it "can upload a remote png image" do
|
52
|
+
response = $client.upload_media(remote_png_path, "image")
|
53
|
+
expect(response.code).to eq(WeixinAuthorize::OK_CODE)
|
54
|
+
expect(response.result.keys).to eq(["type", "media_id", "created_at"])
|
55
|
+
end
|
56
|
+
|
57
|
+
it "can upload a remote jpg image" do
|
58
|
+
response = $client.upload_media(remote_jpg_path, "image")
|
59
|
+
expect(response.code).to eq(WeixinAuthorize::OK_CODE)
|
60
|
+
expect(response.result.keys).to eq(["type", "media_id", "created_at"])
|
61
|
+
end
|
62
|
+
|
63
|
+
it "#download_media_url return a String url" do
|
64
|
+
image = $client.upload_media(image_ico_path, "image")
|
65
|
+
media_id = image.result["media_id"]
|
66
|
+
image_url = $client.download_media_url(media_id)
|
67
|
+
expect(image_url.class).to eq(String)
|
68
|
+
end
|
69
|
+
|
70
|
+
end
|
Binary file
|
Binary file
|
@@ -0,0 +1,26 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
describe WeixinAuthorize::Api::Menu do
|
3
|
+
|
4
|
+
it "can create a menu" do
|
5
|
+
menu = '{"button":[{"type":"click","name":"今日歌曲","key":"V1001_TODAY_MUSIC"},{"type":"click","name":"歌手简介","key":"V1001_TODAY_SINGER"},{"name":"菜单","sub_button":[{"type":"view","name":"搜索","url":"http://www.soso.com/"},{"type":"view","name":"视频","url":"http://v.qq.com/"},{"type":"click","name":"赞一下我们","key":"V1001_GOOD"}]}]}'
|
6
|
+
response = $client.create_menu(JSON.load(menu)) # or Json string
|
7
|
+
expect(response.code).to eq(WeixinAuthorize::OK_CODE)
|
8
|
+
end
|
9
|
+
|
10
|
+
it "can't create a menu if invalid button size" do
|
11
|
+
menu = '{"button":[]}'
|
12
|
+
response = $client.create_menu(menu)
|
13
|
+
expect(response.code).not_to eq(WeixinAuthorize::OK_CODE)
|
14
|
+
end
|
15
|
+
|
16
|
+
it "can get a weixin Menu info" do
|
17
|
+
menu_info = $client.menu
|
18
|
+
expect(menu_info.result.keys[0]).to eq("menu")
|
19
|
+
expect(menu_info.code).to eq(WeixinAuthorize::OK_CODE)
|
20
|
+
end
|
21
|
+
|
22
|
+
it "can delete weixin Menu" do
|
23
|
+
response = $client.delete_menu
|
24
|
+
expect(response.code).to eq(WeixinAuthorize::OK_CODE)
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
describe WeixinAuthorize::Api::Qrcode do
|
2
|
+
|
3
|
+
it "#create_qr_scene" do
|
4
|
+
response = $client.create_qr_scene("123")
|
5
|
+
expect(response.code).to eq(WeixinAuthorize::OK_CODE)
|
6
|
+
expect(response.result.keys).to eq(["ticket", "expire_seconds", "url"])
|
7
|
+
expect(response.result["expire_seconds"]).to eq(1800)
|
8
|
+
end
|
9
|
+
|
10
|
+
it "#create_qr_limit_scene" do
|
11
|
+
response = $client.create_qr_limit_scene(scene_id: 1234, scene_str: "wechat")
|
12
|
+
expect(response.code).to eq(WeixinAuthorize::OK_CODE)
|
13
|
+
expect(response.result.keys).to eq(["ticket", "url"])
|
14
|
+
end
|
15
|
+
|
16
|
+
it "#return_qr_url" do
|
17
|
+
response = $client.create_qr_limit_scene(scene_id: 1234, scene_str: "wechat")
|
18
|
+
qr_url = $client.qr_code_url(response.result["ticket"])
|
19
|
+
expect(response.code).to eq(WeixinAuthorize::OK_CODE)
|
20
|
+
expect(qr_url).to match(/(\S+\.(com|net|org|edu|gov)(\/\S+)?)/)
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
describe WeixinAuthorize::Api::Template do
|
2
|
+
# {{first.DATA}}
|
3
|
+
# 项目名称:{{class.DATA}}
|
4
|
+
# 时间:{{time.DATA}}
|
5
|
+
# 地点:{{add.DATA}}
|
6
|
+
# {{remark.DATA}}
|
7
|
+
it "can send template msg" do
|
8
|
+
url = "http://www.baidu.com"
|
9
|
+
data = {
|
10
|
+
first: {
|
11
|
+
value: "报名结果通知",
|
12
|
+
color: "#173277"
|
13
|
+
},
|
14
|
+
class: {
|
15
|
+
value: "领导与管理课程培训/人格测评",
|
16
|
+
color: "#173177"
|
17
|
+
},
|
18
|
+
time: {
|
19
|
+
value: "11月6日—11月7日(周三—周五)",
|
20
|
+
color: "#274177"
|
21
|
+
},
|
22
|
+
add: {
|
23
|
+
value: "F302室",
|
24
|
+
color: "#274377"
|
25
|
+
},
|
26
|
+
remark: {
|
27
|
+
value: "您可点击【详情】查看详细信息。",
|
28
|
+
color: "#274377"
|
29
|
+
}
|
30
|
+
}
|
31
|
+
msg_result = $client.send_template_msg(
|
32
|
+
ENV["OPENID"],
|
33
|
+
ENV["TEMPLATE_ID"],
|
34
|
+
url,
|
35
|
+
"#173177",
|
36
|
+
data
|
37
|
+
)
|
38
|
+
expect(msg_result.code).to eq(WeixinAuthorize::OK_CODE)
|
39
|
+
end
|
40
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
describe WeixinAuthorize::Api::User do
|
2
|
+
it "can get a weixin User info" do
|
3
|
+
user_info = $client.user(ENV["OPENID"])
|
4
|
+
expect(user_info.code).to eq(WeixinAuthorize::OK_CODE)
|
5
|
+
expect(user_info.result["openid"]).to eq(ENV["OPENID"])
|
6
|
+
end
|
7
|
+
|
8
|
+
it "can get followers infos" do
|
9
|
+
valid_info = $client.is_valid?
|
10
|
+
expect(valid_info).to eq(true)
|
11
|
+
followers = $client.followers
|
12
|
+
expect(followers.code).to eq(WeixinAuthorize::OK_CODE)
|
13
|
+
expect(followers.result.keys).to eq(["total", "count", "data", "next_openid"])
|
14
|
+
|
15
|
+
valid_info = $client.is_valid?
|
16
|
+
expect(valid_info).to eq(true)
|
17
|
+
followers = $client.followers
|
18
|
+
expect(followers.code).to eq(WeixinAuthorize::OK_CODE)
|
19
|
+
expect(followers.result.keys).to eq(["total", "count", "data", "next_openid"])
|
20
|
+
end
|
21
|
+
|
22
|
+
it "can update user remark" do
|
23
|
+
user_info = $client.update_remark(ENV["OPENID"], "dylan")
|
24
|
+
expect(user_info.code).to eq(WeixinAuthorize::OK_CODE)
|
25
|
+
end
|
26
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,130 @@
|
|
1
|
+
# This file was generated by the `rspec --init` command. Conventionally, all
|
2
|
+
# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
|
3
|
+
# The generated `.rspec` file contains `--require spec_helper` which will cause this
|
4
|
+
# file to always be loaded, without a need to explicitly require it in any files.
|
5
|
+
#
|
6
|
+
# Given that it is always loaded, you are encouraged to keep this file as
|
7
|
+
# light-weight as possible. Requiring heavyweight dependencies from this file
|
8
|
+
# (such as loading up an entire rails app) will add to the boot time of your
|
9
|
+
# test suite on EVERY test run, even for an individual file that may not need
|
10
|
+
# all of that loaded.
|
11
|
+
#
|
12
|
+
# The `.rspec` file also contains a few flags that are not defaults but that
|
13
|
+
# users commonly want.
|
14
|
+
#
|
15
|
+
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
|
16
|
+
require "rspec"
|
17
|
+
require "weixin_authorize"
|
18
|
+
require 'yajl/json_gem'
|
19
|
+
require "redis"
|
20
|
+
require "redis-namespace"
|
21
|
+
|
22
|
+
require 'coveralls'
|
23
|
+
require 'simplecov'
|
24
|
+
require "codeclimate-test-reporter"
|
25
|
+
|
26
|
+
require "pry-rails"
|
27
|
+
|
28
|
+
Coveralls.wear!
|
29
|
+
|
30
|
+
SimpleCov.formatter = SimpleCov::Formatter::MultiFormatter[
|
31
|
+
SimpleCov::Formatter::HTMLFormatter,
|
32
|
+
Coveralls::SimpleCov::Formatter
|
33
|
+
]
|
34
|
+
|
35
|
+
SimpleCov.start
|
36
|
+
|
37
|
+
ENV['CODECLIMATE_REPO_TOKEN'] = "c91fecbbd9e414e7cc3ad7a7d99207145de0ac65a3368de09e8c19295343d399"
|
38
|
+
CodeClimate::TestReporter.start
|
39
|
+
|
40
|
+
# If you want test, change your weixin test profile
|
41
|
+
ENV["APPID"]="wx986f04063d341d04"
|
42
|
+
ENV["APPSECRET"]="1a941cd88cb4579ba98ec06b6813af03"
|
43
|
+
ENV["OPENID"]="o9k6BuB0kydAcPTc7sPxppB1GQqA"
|
44
|
+
ENV["TEMPLATE_ID"]="-8ooXrOK3VD3HuSS8--nH154PO9Lw2E7T-RV1uTaGLc"
|
45
|
+
|
46
|
+
# Comment to test for ClientStorage
|
47
|
+
redis = Redis.new(host: "127.0.0.1", port: "6379", db: 15)
|
48
|
+
|
49
|
+
namespace = "weixin_test:weixin_authorize"
|
50
|
+
|
51
|
+
# cleanup keys in the current namespace when restart server everytime.
|
52
|
+
exist_keys = redis.keys("#{namespace}:*")
|
53
|
+
exist_keys.each{|key|redis.del(key)}
|
54
|
+
|
55
|
+
redis_with_ns = Redis::Namespace.new("#{namespace}", :redis => redis)
|
56
|
+
|
57
|
+
WeixinAuthorize.configure do |config|
|
58
|
+
config.redis = redis_with_ns
|
59
|
+
config.rest_client_options = {timeout: 10, open_timeout: 10, verify_ssl: true}
|
60
|
+
end
|
61
|
+
|
62
|
+
$client = WeixinAuthorize::Client.new(ENV["APPID"], ENV["APPSECRET"])
|
63
|
+
|
64
|
+
RSpec.configure do |config|
|
65
|
+
# The settings below are suggested to provide a good initial experience
|
66
|
+
# with RSpec, but feel free to customize to your heart's content.
|
67
|
+
=begin
|
68
|
+
# These two settings work together to allow you to limit a spec run
|
69
|
+
# to individual examples or groups you care about by tagging them with
|
70
|
+
# `:focus` metadata. When nothing is tagged with `:focus`, all examples
|
71
|
+
# get run.
|
72
|
+
config.filter_run :focus
|
73
|
+
config.run_all_when_everything_filtered = true
|
74
|
+
|
75
|
+
# Many RSpec users commonly either run the entire suite or an individual
|
76
|
+
# file, and it's useful to allow more verbose output when running an
|
77
|
+
# individual spec file.
|
78
|
+
if config.files_to_run.one?
|
79
|
+
# RSpec filters the backtrace by default so as not to be so noisy.
|
80
|
+
# This causes the full backtrace to be printed when running a single
|
81
|
+
# spec file (e.g. to troubleshoot a particular spec failure).
|
82
|
+
config.full_backtrace = true
|
83
|
+
|
84
|
+
# Use the documentation formatter for detailed output,
|
85
|
+
# unless a formatter has already been configured
|
86
|
+
# (e.g. via a command-line flag).
|
87
|
+
config.formatter = 'doc' if config.formatters.none?
|
88
|
+
end
|
89
|
+
|
90
|
+
# Print the 10 slowest examples and example groups at the
|
91
|
+
# end of the spec run, to help surface which specs are running
|
92
|
+
# particularly slow.
|
93
|
+
config.profile_examples = 10
|
94
|
+
|
95
|
+
# Run specs in random order to surface order dependencies. If you find an
|
96
|
+
# order dependency and want to debug it, you can fix the order by providing
|
97
|
+
# the seed, which is printed after each run.
|
98
|
+
# --seed 1234
|
99
|
+
config.order = :random
|
100
|
+
|
101
|
+
# Seed global randomization in this process using the `--seed` CLI option.
|
102
|
+
# Setting this allows you to use `--seed` to deterministically reproduce
|
103
|
+
# test failures related to randomization by passing the same `--seed` value
|
104
|
+
# as the one that triggered the failure.
|
105
|
+
Kernel.srand config.seed
|
106
|
+
|
107
|
+
# rspec-expectations config goes here. You can use an alternate
|
108
|
+
# assertion/expectation library such as wrong or the stdlib/minitest
|
109
|
+
# assertions if you prefer.
|
110
|
+
config.expect_with :rspec do |expectations|
|
111
|
+
# Enable only the newer, non-monkey-patching expect syntax.
|
112
|
+
# For more details, see:
|
113
|
+
# - http://myronmars.to/n/dev-blog/2012/06/rspecs-new-expectation-syntax
|
114
|
+
expectations.syntax = :expect
|
115
|
+
end
|
116
|
+
|
117
|
+
# rspec-mocks config goes here. You can use an alternate test double
|
118
|
+
# library (such as bogus or mocha) by changing the `mock_with` option here.
|
119
|
+
config.mock_with :rspec do |mocks|
|
120
|
+
# Enable only the newer, non-monkey-patching expect syntax.
|
121
|
+
# For more details, see:
|
122
|
+
# - http://teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
|
123
|
+
mocks.syntax = :expect
|
124
|
+
|
125
|
+
# Prevents you from mocking or stubbing a method that does not exist on
|
126
|
+
# a real object. This is generally recommended.
|
127
|
+
mocks.verify_partial_doubles = true
|
128
|
+
end
|
129
|
+
=end
|
130
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'weixin_authorize/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "shixian-weixin_authorize"
|
8
|
+
spec.version = WeixinAuthorize::VERSION
|
9
|
+
spec.authors = ["lanrion"]
|
10
|
+
spec.email = ["huaitao-deng@foxmail.com"]
|
11
|
+
spec.description = %q{weixin api authorize access_token}
|
12
|
+
spec.summary = %q{weixin api authorize access_token}
|
13
|
+
spec.homepage = "https://github.com/shixiancom/weixin_authorize"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files`.split($/)
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_dependency "rest-client", ">= 1.6.7"
|
22
|
+
spec.add_dependency "redis", ">= 3.1.0"
|
23
|
+
|
24
|
+
spec.add_dependency "carrierwave", ">= 0.10.0"
|
25
|
+
spec.add_dependency 'mini_magick', '>= 3.7.0'
|
26
|
+
|
27
|
+
# A streaming JSON parsing and encoding library for Ruby (C bindings to yajl)
|
28
|
+
# https://github.com/brianmario/yajl-ruby
|
29
|
+
spec.add_dependency "yajl-ruby", ">= 1.2.0"
|
30
|
+
|
31
|
+
spec.add_development_dependency "bundler"
|
32
|
+
spec.add_development_dependency "rake"
|
33
|
+
|
34
|
+
end
|