You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

logic_ext_test.go 5.3 KiB

1 month ago
1 month ago
1 month ago
4 days ago
1 month ago
4 days ago
1 month ago
4 days ago
1 month ago
4 days ago
1 month ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. package api
  2. import (
  3. "context"
  4. "egg-im/config"
  5. "egg-im/internal/logic/domain/message/model"
  6. "egg-im/pkg/db"
  7. "egg-im/pkg/pb"
  8. "egg-im/pkg/util"
  9. "fmt"
  10. "github.com/jinzhu/gorm"
  11. "strconv"
  12. "testing"
  13. "time"
  14. "google.golang.org/grpc"
  15. "google.golang.org/grpc/metadata"
  16. "google.golang.org/protobuf/proto"
  17. )
  18. func getLogicExtClient() pb.LogicExtClient {
  19. conn, err := grpc.Dial("127.0.0.1:8000", grpc.WithInsecure())
  20. if err != nil {
  21. fmt.Println(err)
  22. return nil
  23. }
  24. return pb.NewLogicExtClient(conn)
  25. }
  26. // deprecated:
  27. func getCtx() context.Context {
  28. token := "JQWORFEWJNSYLCTKSXAMGCVANANNUMGLZOAAUTIQ"
  29. return metadata.NewOutgoingContext(context.TODO(), metadata.Pairs(
  30. "user_id", "5",
  31. "device_id", "1",
  32. "token", token,
  33. "request_id", strconv.FormatInt(time.Now().UnixNano(), 10)))
  34. }
  35. func TestLogicExtServer_RegisterDevice(t *testing.T) {
  36. resp, err := getLogicExtClient().RegisterDevice(context.TODO(),
  37. &pb.RegisterDeviceReq{
  38. Type: 1,
  39. Brand: "huawei",
  40. Model: "huawei P30",
  41. SystemVersion: "1.0.0",
  42. SdkVersion: "1.0.0",
  43. })
  44. if err != nil {
  45. fmt.Println(err)
  46. return
  47. }
  48. fmt.Printf("%+v\n", resp)
  49. }
  50. func TestLogicExtServer_SendMessage(t *testing.T) {
  51. buf, err := proto.Marshal(&pb.Text{
  52. Text: "hello world!",
  53. })
  54. if err != nil {
  55. fmt.Println(err)
  56. return
  57. }
  58. var one = string(buf)
  59. fmt.Printf(one)
  60. resp, err := getLogicExtClient().SendMessage(getCtx(),
  61. &pb.SendMessageReq{
  62. ReceiverType: pb.ReceiverType_RT_USER,
  63. ReceiverId: 21,
  64. ToUserIds: nil,
  65. MessageType: pb.MessageType_MT_TEXT,
  66. MessageContent: buf,
  67. IsPersist: true,
  68. SendTime: util.UnixMilliTime(time.Now()),
  69. })
  70. if err != nil {
  71. fmt.Println(err)
  72. return
  73. }
  74. fmt.Printf("%+v\n", resp)
  75. }
  76. func TestLogicExtServer_protoUnmarshal(t *testing.T) {
  77. db.InitMysql(config.MySQL)
  78. var msg model.Message
  79. if err := db.DB.Table("message_000").Where("`id` = 194581").First(&msg).Error; err != nil {
  80. if err == gorm.ErrRecordNotFound {
  81. fmt.Println(err)
  82. return
  83. }
  84. }
  85. var temp pb.RED_PACKAGE
  86. //var temp1 pb.RED_PACKAGE
  87. err := proto.Unmarshal(msg.Content, &temp)
  88. if err != nil {
  89. fmt.Println(err)
  90. return
  91. }
  92. //err = proto.Unmarshal(temp.Data, &temp1)
  93. //if err != nil {
  94. // fmt.Println(err)
  95. // return
  96. //}
  97. fmt.Println(temp)
  98. fmt.Printf("!!!!")
  99. }
  100. func TestLogicExtServer_SendImageMessage(t *testing.T) {
  101. buf, err := proto.Marshal(&pb.Image{
  102. Id: "",
  103. Width: 0,
  104. Height: 0,
  105. Url: "https://img.iplaysoft.com/wp-content/uploads/2019/free-images/free_stock_photo.jpg",
  106. ThumbnailUrl: "",
  107. })
  108. if err != nil {
  109. fmt.Println(err)
  110. return
  111. }
  112. resp, err := getLogicExtClient().SendMessage(getCtx(),
  113. &pb.SendMessageReq{
  114. ReceiverType: pb.ReceiverType_RT_USER,
  115. ReceiverId: 1,
  116. ToUserIds: nil,
  117. MessageType: pb.MessageType_MT_IMAGE,
  118. MessageContent: buf,
  119. IsPersist: true,
  120. SendTime: util.UnixMilliTime(time.Now()),
  121. })
  122. if err != nil {
  123. fmt.Println(err)
  124. return
  125. }
  126. fmt.Printf("%+v\n", resp)
  127. }
  128. func TestLogicExtServer_CreateGroup(t *testing.T) {
  129. resp, err := getLogicExtClient().CreateGroup(getCtx(),
  130. &pb.CreateGroupReq{
  131. Name: "10",
  132. Introduction: "10",
  133. Extra: "10",
  134. })
  135. if err != nil {
  136. fmt.Println(err)
  137. return
  138. }
  139. fmt.Printf("%+v\n", resp)
  140. }
  141. func TestLogicExtServer_UpdateGroup(t *testing.T) {
  142. resp, err := getLogicExtClient().UpdateGroup(getCtx(),
  143. &pb.UpdateGroupReq{
  144. GroupId: 2,
  145. Name: "11",
  146. Introduction: "11",
  147. Extra: "11",
  148. })
  149. if err != nil {
  150. fmt.Println(err)
  151. return
  152. }
  153. fmt.Printf("%+v\n", resp)
  154. }
  155. func TestLogicExtServer_GetGroup(t *testing.T) {
  156. resp, err := getLogicExtClient().GetGroup(getCtx(),
  157. &pb.GetGroupReq{
  158. GroupId: 2,
  159. })
  160. if err != nil {
  161. fmt.Println(err)
  162. return
  163. }
  164. fmt.Printf("%+v\n", resp)
  165. }
  166. func TestLogicExtServer_GetUserGroups(t *testing.T) {
  167. resp, err := getLogicExtClient().GetGroups(getCtx(), &pb.Empty{})
  168. if err != nil {
  169. fmt.Println(err)
  170. return
  171. }
  172. // todo 不能获取用户所在的超大群组
  173. fmt.Printf("%+v\n", resp)
  174. }
  175. func TestLogicExtServer_AddGroupMember(t *testing.T) {
  176. resp, err := getLogicExtClient().AddGroupMembers(getCtx(),
  177. &pb.AddGroupMembersReq{
  178. GroupId: 2,
  179. })
  180. if err != nil {
  181. fmt.Println(err)
  182. return
  183. }
  184. fmt.Printf("%+v\n", resp)
  185. }
  186. func TestLogicExtServer_UpdateGroupMember(t *testing.T) {
  187. resp, err := getLogicExtClient().UpdateGroupMember(getCtx(),
  188. &pb.UpdateGroupMemberReq{
  189. GroupId: 2,
  190. UserId: 3,
  191. Remarks: "2",
  192. Extra: "2",
  193. })
  194. if err != nil {
  195. fmt.Println(err)
  196. return
  197. }
  198. fmt.Printf("%+v\n", resp)
  199. }
  200. func TestLogicExtServer_DeleteGroupMember(t *testing.T) {
  201. resp, err := getLogicExtClient().DeleteGroupMember(getCtx(),
  202. &pb.DeleteGroupMemberReq{
  203. GroupId: 10,
  204. //UserId: 1,
  205. })
  206. if err != nil {
  207. fmt.Println(err)
  208. return
  209. }
  210. fmt.Printf("%+v\n", resp)
  211. }
  212. func TestLogicExtServer_GetGroupMembers(t *testing.T) {
  213. resp, err := getLogicExtClient().GetGroupMembers(getCtx(),
  214. &pb.GetGroupMembersReq{
  215. GroupId: 2,
  216. })
  217. if err != nil {
  218. fmt.Println(err)
  219. return
  220. }
  221. fmt.Printf("%+v\n", resp)
  222. }
  223. func TestLogicExtServer_AddFriend(t *testing.T) {
  224. resp, err := getLogicExtClient().AddFriend(getCtx(),
  225. &pb.AddFriendReq{
  226. FriendId: 2,
  227. Remarks: "weihan",
  228. Description: "hello",
  229. })
  230. if err != nil {
  231. fmt.Println(err)
  232. return
  233. }
  234. fmt.Printf("%+v\n", resp)
  235. }