gitee.com/larksuite/oapi-sdk-go/v3@v3.0.3/event/dispatcher/dispatcher_test.go (about) 1 /* 2 * MIT License 3 * 4 * Copyright (c) 2022 Lark Technologies Pte. Ltd. 5 * 6 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 7 * 8 * The above copyright notice and this permission notice, shall be included in all copies or substantial portions of the Software. 9 * 10 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 11 */ 12 13 package dispatcher 14 15 import ( 16 "context" 17 "encoding/json" 18 "fmt" 19 "net/http" 20 "testing" 21 22 "gitee.com/larksuite/oapi-sdk-go/v3/core" 23 "gitee.com/larksuite/oapi-sdk-go/v3/event" 24 "gitee.com/larksuite/oapi-sdk-go/v3/service/contact/v3" 25 "gitee.com/larksuite/oapi-sdk-go/v3/service/im/v1" 26 ) 27 28 func mockEncryptedBody(encrypteKey string) []byte { 29 30 eventBody := "" 31 en, _ := larkcore.EncryptedEventMsg(context.Background(), eventBody, encrypteKey) 32 fmt.Println(encrypteKey) 33 34 encrypt := larkevent.EventEncryptMsg{Encrypt: en} 35 body1, _ := json.Marshal(encrypt) 36 37 return body1 38 } 39 40 func mockEvent() []byte { 41 42 eventBody := "" 43 44 body1, _ := json.Marshal(eventBody) 45 46 return body1 47 } 48 49 func TestVerifyUrlOk(t *testing.T) { 50 handler := NewEventDispatcher("v", "1212121212").OnP2MessageReceiveV1(func(ctx context.Context, event *larkim.P2MessageReceiveV1) error { 51 fmt.Println(larkcore.Prettify(event)) 52 return nil 53 }).OnP2UserCreatedV3(func(ctx context.Context, event *larkcontact.P2UserCreatedV3) error { 54 fmt.Println(larkcore.Prettify(event)) 55 return nil 56 }) 57 58 //plainEventJsonStr := "{\"schema\":\"2.0\",\"header\":{\"event_id\":\"f7984f25108f8137722bb63cee927e66\",\"event_type\":\"contact.user.created_v3\",\"app_id\":\"cli_xxxxxxxx\",\"tenant_key\":\"xxxxxxx\",\"create_time\":\"1603977298000000\",\"token\":\"v\"},\"event\":{\"object\":{\"open_id\":\"ou_7dab8a3d3cdcc9da365777c7ad535d62\",\"union_id\":\"on_576833b917gda3d939b9a3c2d53e72c8\",\"user_id\":\"e33ggbyz\",\"name\":\"张三\",\"employee_no\":\"employee_no\"}},\"challenge\":\"1212\",\"type\":\"url_verification\"}" 59 _, err := handler.AuthByChallenge(context.Background(), larkevent.ReqTypeEventCallBack, "", "") 60 if err != nil { 61 t.Errorf("verfiy url failed ,%v", err) 62 } 63 64 } 65 66 func TestVerifyUrlFailed(t *testing.T) { 67 // 创建card处理器 68 handler := NewEventDispatcher("v", "1212121212").OnP2MessageReceiveV1(func(ctx context.Context, event *larkim.P2MessageReceiveV1) error { 69 fmt.Println(larkcore.Prettify(event)) 70 return nil 71 }).OnP2UserCreatedV3(func(ctx context.Context, event *larkcontact.P2UserCreatedV3) error { 72 fmt.Println(larkcore.Prettify(event)) 73 return nil 74 }) 75 //plainEventJsonStr := "{\"schema\":\"2.0\",\"header\":{\"event_id\":\"f7984f25108f8137722bb63cee927e66\",\"event_type\":\"contact.user.created_v3\",\"app_id\":\"cli_xxxxxxxx\",\"tenant_key\":\"xxxxxxx\",\"create_time\":\"1603977298000000\",\"token\":\"1v\"},\"event\":{\"object\":{\"open_id\":\"ou_7dab8a3d3cdcc9da365777c7ad535d62\",\"union_id\":\"on_576833b917gda3d939b9a3c2d53e72c8\",\"user_id\":\"e33ggbyz\",\"name\":\"张三\",\"employee_no\":\"employee_no\"}},\"challenge\":\"1212\",\"type\":\"url_verification\"}" 76 _, err := handler.AuthByChallenge(context.Background(), larkevent.ReqTypeEventCallBack, "", "") 77 if err == nil { 78 fmt.Println(err) 79 return 80 } 81 } 82 83 func mockEventReq(token string) *larkevent.EventReq { 84 85 req, _ := http.NewRequest(http.MethodPost, "http://127.0.0.1:9999/webhook/event", nil) 86 87 body := "{\"schema\":\"2.0\",\"header\":{\"event_id\":\"f7984f25108f8137722bb63cee927e66\",\"event_type\":\"contact.user.created_v3\",\"app_id\":\"cli_xxxxxxxx\",\"tenant_key\":\"xxxxxxx\",\"create_time\":\"1603977298000000\",\"token\":\"v\"},\"event\":{\"object\":{\"open_id\":\"ou_7dab8a3d3cdcc9da365777c7ad535d62\",\"union_id\":\"on_576833b917gda3d939b9a3c2d53e72c8\",\"user_id\":\"e33ggbyz\",\"name\":\"张三\",\"employee_no\":\"employee_no\"}},\"challenge\":\"1212\",\"type\":\"url_verification\"}" 88 89 var timestamp = "timestamp" 90 var nonce = "nonce" 91 sourceSign := larkevent.Signature(timestamp, nonce, token, string(body)) 92 93 // 添加header 94 req.Header.Set(larkevent.EventRequestTimestamp, timestamp) 95 req.Header.Set(larkevent.EventRequestNonce, nonce) 96 req.Header.Set(larkevent.EventSignature, sourceSign) 97 98 eventReq := larkevent.EventReq{ 99 Header: req.Header, 100 Body: []byte(body), 101 } 102 103 return &eventReq 104 } 105 106 func TestParseReq(t *testing.T) { 107 // 创建card处理器 108 handler := NewEventDispatcher("", "").OnP2MessageReceiveV1(func(ctx context.Context, event *larkim.P2MessageReceiveV1) error { 109 fmt.Println(larkcore.Prettify(event)) 110 return nil 111 }).OnP2UserCreatedV3(func(ctx context.Context, event *larkcontact.P2UserCreatedV3) error { 112 fmt.Println(larkcore.Prettify(event)) 113 return nil 114 }) 115 116 config := &larkcore.Config{} 117 larkcore.NewLogger(config) 118 handler.Config = config 119 120 // mock请求 121 req := mockEventReq("121") 122 resp, err := handler.ParseReq(context.Background(), req) 123 if err != nil { 124 t.Errorf("TestParseReq failed ,%v", err) 125 return 126 } 127 128 fmt.Println(resp) 129 } 130 131 func TestDecryptEvent(t *testing.T) { 132 // 创建card处理器 133 handler := NewEventDispatcher("v", "1212121212").OnP2MessageReceiveV1(func(ctx context.Context, event *larkim.P2MessageReceiveV1) error { 134 fmt.Println(larkcore.Prettify(event)) 135 return nil 136 }).OnP2UserCreatedV3(func(ctx context.Context, event *larkcontact.P2UserCreatedV3) error { 137 fmt.Println(larkcore.Prettify(event)) 138 return nil 139 }) 140 141 config := &larkcore.Config{} 142 larkcore.NewLogger(config) 143 handler.Config = config 144 145 resp, err := handler.DecryptEvent(context.Background(), "bZ3L7yh6m3Fkuffl4g+3uGjIHnhOm5fVZbKVuyT8t7tcd5ABMYm8l28/X900ZL3knZ7n+sCREu/H2WnIzft0amC7+xWqNH8o25IU63N4BnZWfHh+4hyG76QPd19vkw2bPJCx9aqxK8Nz+xqFNbk0RdgyWhmgd30jSxHtcQXAllkI7FMpGpOCteJad3bLXPDBQIV/xkCtKICCS7Z63gakpxZCLaRZ3qCXP1fapHh+LBIupxenrU6ysc7I3nHmjmKie41IiWwS5puG4zQHhVbq6KWLcgWm/3NBZOPQy53ucMu75SXA55I7jarVLZXWUcqBGrcgE3vouWbtwgZuzmoTQl0GSh5VYSVvpW992BuGxUWj0XjPYdICJm6Cr7xouNXwMcdb7N8caVdkdSZeEnswG19qSyDoQhklwzNGW0yiaayulBqJNjfge/G5V3401c2XaIuAeEIo+QQ4RSNpRGfnHkbu/j55FGQAGWjpuBNaIwZbaUoVP3NkGP+vM5rpEDe3sL2GN+Xsd+g9yBs7FqdMV8mXTGgLjCqjrPrke5/km76Q3Pe6KPs2YexMRG4MkSx3xUTzZnNn7zIzShPcjeSwBd2pxk6ht5N+fzueZdxl6Oo=") 146 if err != nil { 147 t.Errorf("TestDecryptEvent failed ,%v", err) 148 return 149 } 150 151 fmt.Println(resp) 152 } 153 154 func TestVerifySignOk(t *testing.T) { 155 // 创建card处理器 156 handler := NewEventDispatcher("v", "1212121212").OnP2MessageReceiveV1(func(ctx context.Context, event *larkim.P2MessageReceiveV1) error { 157 fmt.Println(larkcore.Prettify(event)) 158 return nil 159 }).OnP2UserCreatedV3(func(ctx context.Context, event *larkcontact.P2UserCreatedV3) error { 160 fmt.Println(larkcore.Prettify(event)) 161 return nil 162 }) 163 164 config := &larkcore.Config{} 165 larkcore.NewLogger(config) 166 handler.Config = config 167 168 req := mockEventReq("1212121212") 169 err := handler.VerifySign(context.Background(), req) 170 if err != nil { 171 t.Errorf("TestVerifySignOk failed ,%v", err) 172 return 173 } 174 } 175 func TestAppTicket(t *testing.T) { 176 177 event := AppTicketEvent{ 178 EventBase: &larkevent.EventBase{ 179 Ts: "", 180 UUID: "", 181 Token: "1212121212", 182 Type: "", 183 }, 184 Event: &appTicketEventData{ 185 AppId: "jiaduoappId", 186 Type: "app_ticket", 187 AppTicket: "AppTicketvalue", 188 }, 189 } 190 191 body, _ := json.Marshal(event) 192 fmt.Println(string(body)) 193 194 }