registry_test.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. package main
  2. import (
  3. "sparrow/pkg/models"
  4. "sparrow/pkg/mysql"
  5. "sparrow/pkg/rpcs"
  6. "testing"
  7. )
  8. func testVendor(t *testing.T, r *Registry) {
  9. vendor := &models.Vendor{
  10. VendorName: "testvendor",
  11. VendorDescription: "this is a test vendor",
  12. }
  13. err := r.SaveVendor(vendor, vendor)
  14. if err != nil {
  15. t.Fatal(err)
  16. }
  17. err = r.FindVendor(int32(vendor.ID), vendor)
  18. if err != nil {
  19. t.Fatal(err)
  20. }
  21. updateVendor := &models.Vendor{
  22. VendorName: "testvendorupdate",
  23. VendorDescription: "this is a test vendor",
  24. }
  25. updateVendor.ID = vendor.ID
  26. err = r.SaveVendor(updateVendor, updateVendor)
  27. if err != nil {
  28. t.Fatal(err)
  29. }
  30. vendorRow := &models.Vendor{}
  31. err = r.FindVendor(int32(updateVendor.ID), vendorRow)
  32. if err != nil {
  33. t.Fatal(err)
  34. }
  35. if vendorRow.VendorName != updateVendor.VendorName {
  36. t.Errorf("expect vendorName:%v, got:%v", updateVendor.VendorName, vendorRow.VendorName)
  37. }
  38. t.Log(vendor)
  39. }
  40. var (
  41. testProductKey = ""
  42. )
  43. func testProduct(t *testing.T, r *Registry) {
  44. product := &models.Product{
  45. VendorID: 1,
  46. ProductName: "test product.",
  47. ProductDescription: "this is a test product",
  48. ProductConfig: "{}",
  49. }
  50. err := r.SaveProduct(product, product)
  51. if err != nil {
  52. t.Fatal(err)
  53. }
  54. t.Log(product)
  55. productRow := &models.Product{}
  56. err = r.FindProduct(int32(product.ID), productRow)
  57. if err != nil {
  58. t.Fatal(err)
  59. }
  60. if product.ProductKey != productRow.ProductKey {
  61. t.Fatalf("expected %v, got %v", product.ProductKey, productRow.ProductKey)
  62. }
  63. product.ProductName = "test for update."
  64. err = r.SaveProduct(product, product)
  65. if err != nil {
  66. t.Fatal(err)
  67. }
  68. t.Log(product)
  69. reply := &models.Product{}
  70. err = r.ValidateProduct(product.ProductKey, reply)
  71. if err != nil {
  72. t.Error(err)
  73. }
  74. t.Log(reply)
  75. if reply.ProductName != product.ProductName {
  76. t.Errorf("expected %v, got %v", product.ProductName, reply.ProductName)
  77. }
  78. testProductKey = product.ProductKey
  79. err = r.ValidateProduct("this is a wrong key , you know", reply)
  80. if err == nil {
  81. t.Error("wrong key should fail product key validation.")
  82. }
  83. }
  84. func testApplication(t *testing.T, r *Registry) {
  85. app := &models.Application{
  86. AppToken: "test-token",
  87. ReportUrl: "http://localhost://6060",
  88. AppName: "test",
  89. AppDescription: "this is a test app",
  90. AppDomain: "/*",
  91. }
  92. err := r.SaveApplication(app, app)
  93. if err != nil {
  94. t.Fatal(err)
  95. }
  96. t.Log(app)
  97. appRow := &models.Application{}
  98. err = r.FindApplication(int32(app.ID), appRow)
  99. if err != nil {
  100. t.Fatal(err)
  101. }
  102. app.AppName = "another desc."
  103. err = r.SaveApplication(app, app)
  104. if err != nil {
  105. t.Fatal(err)
  106. }
  107. t.Log(app)
  108. err = r.FindApplication(int32(app.ID), appRow)
  109. if err != nil {
  110. t.Fatal(err)
  111. }
  112. if appRow.AppName != app.AppName {
  113. t.Errorf("expected %v, got %v", app.AppName, appRow.AppName)
  114. }
  115. reply := &models.Application{}
  116. err = r.ValidateApplication(app.AppKey, reply)
  117. if err != nil {
  118. t.Error(err)
  119. }
  120. t.Log(reply)
  121. err = r.ValidateApplication("this is a wrong key , you know", reply)
  122. if err == nil {
  123. t.Error("wrong key should fail product key validation.")
  124. }
  125. }
  126. func testRegister(t *testing.T, r *Registry) {
  127. args := &models.User{
  128. UserName: "lijian",
  129. UserPass: "Lijian",
  130. Phone: "15275410996",
  131. Email: "Email@qq1.com",
  132. }
  133. reply := models.User{}
  134. err := r.Register(args, &reply)
  135. if err != nil {
  136. t.Fatal(err)
  137. }
  138. t.Log(args)
  139. }
  140. func testDevice(t *testing.T, r *Registry) {
  141. args := &rpcs.ArgsDeviceRegister{
  142. ProductKey: testProductKey,
  143. DeviceCode: "ffffaaeeccbb",
  144. DeviceVersion: "android-gprs-v1",
  145. }
  146. device := &models.Device{}
  147. err := r.RegisterDevice(args, device)
  148. if err != nil {
  149. t.Fatal(err)
  150. }
  151. t.Log(device)
  152. founddev := &models.Device{}
  153. err = r.FindDeviceById(int64(device.ID), founddev)
  154. if err != nil {
  155. t.Error(err)
  156. }
  157. if device.DeviceIdentifier != founddev.DeviceIdentifier {
  158. t.Errorf("FindDeviceById not match, want %v, got %v", device, founddev)
  159. }
  160. err = r.FindDeviceByIdentifier(device.DeviceIdentifier, founddev)
  161. if err != nil {
  162. t.Error(err)
  163. }
  164. if device.ID != founddev.ID {
  165. t.Errorf("FindDeviceByIdentifier not match, want %v, got %v", device, founddev)
  166. }
  167. device.DeviceDescription = "test change device info."
  168. args2 := &rpcs.ArgsDeviceUpdate{
  169. DeviceIdentifier: device.DeviceIdentifier,
  170. DeviceName: "testupdatename",
  171. DeviceDescription: "test change device info.",
  172. }
  173. err = r.UpdateDeviceInfo(args2, device)
  174. if err != nil {
  175. t.Error(err)
  176. }
  177. devRow := &models.Device{}
  178. err = r.FindDeviceByIdentifier(device.DeviceIdentifier, devRow)
  179. if err != nil {
  180. t.Error(err)
  181. }
  182. if devRow.DeviceName != device.DeviceName {
  183. t.Errorf(" want %v, got %v", device.DeviceName, devRow.DeviceName)
  184. }
  185. t.Log(device)
  186. }
  187. func testModifyPassword(t *testing.T, r *Registry) {
  188. userid := uint(1)
  189. pass := "lijian"
  190. reply := &models.User{}
  191. err := r.ModifyPass(userid, pass, reply)
  192. if err != nil {
  193. t.Fatal(err)
  194. }
  195. t.Log(reply)
  196. }
  197. func testLogin(t *testing.T, r *Registry) {
  198. reply := &models.User{}
  199. err := r.Login(&models.LoginRequest{
  200. UserName: "lijian",
  201. Password: "lijian",
  202. }, reply)
  203. if err != nil {
  204. t.Fatal(err)
  205. }
  206. t.Fatal(reply)
  207. }
  208. func TestRegistry(t *testing.T) {
  209. err := mysql.MigrateDatabase(defaultDBHost, defaultDBPort, defaultDBName, defaultDBUser, "123456")
  210. if err != nil {
  211. t.Fatal(err)
  212. }
  213. *confAESKey = "ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP"
  214. *confDBPass = "123456"
  215. r, err := NewRegistry()
  216. if err != nil {
  217. t.Fatal(err)
  218. }
  219. //testVendor(t, r)
  220. //testProduct(t, r)
  221. //testApplication(t, r)
  222. //testDevice(t, r)
  223. //testRegister(t, r)
  224. //testModifyPassword(t, r)
  225. testLogin(t, r)
  226. }