PageRenderTime 46ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/Godeps/_workspace/src/github.com/bugsnag/bugsnag-go/metadata_test.go

https://gitlab.com/CORP-RESELLER/cf-distribution
Go | 182 lines | 162 code | 20 blank | 0 comment | 3 complexity | bbf030fb20025b3f865942830ceb36b4 MD5 | raw file
  1. package bugsnag
  2. import (
  3. "reflect"
  4. "testing"
  5. "unsafe"
  6. "github.com/bugsnag/bugsnag-go/errors"
  7. )
  8. type _account struct {
  9. ID string
  10. Name string
  11. Plan struct {
  12. Premium bool
  13. }
  14. Password string
  15. secret string
  16. Email string `json:"email"`
  17. EmptyEmail string `json:"emptyemail,omitempty"`
  18. NotEmptyEmail string `json:"not_empty_email,omitempty"`
  19. }
  20. type _broken struct {
  21. Me *_broken
  22. Data string
  23. }
  24. var account = _account{}
  25. var notifier = New(Configuration{})
  26. func TestMetaDataAdd(t *testing.T) {
  27. m := MetaData{
  28. "one": {
  29. "key": "value",
  30. "override": false,
  31. }}
  32. m.Add("one", "override", true)
  33. m.Add("one", "new", "key")
  34. m.Add("new", "tab", account)
  35. m.AddStruct("lol", "not really a struct")
  36. m.AddStruct("account", account)
  37. if !reflect.DeepEqual(m, MetaData{
  38. "one": {
  39. "key": "value",
  40. "override": true,
  41. "new": "key",
  42. },
  43. "new": {
  44. "tab": account,
  45. },
  46. "Extra data": {
  47. "lol": "not really a struct",
  48. },
  49. "account": {
  50. "ID": "",
  51. "Name": "",
  52. "Plan": map[string]interface{}{
  53. "Premium": false,
  54. },
  55. "Password": "",
  56. "email": "",
  57. },
  58. }) {
  59. t.Errorf("metadata.Add didn't work: %#v", m)
  60. }
  61. }
  62. func TestMetaDataUpdate(t *testing.T) {
  63. m := MetaData{
  64. "one": {
  65. "key": "value",
  66. "override": false,
  67. }}
  68. m.Update(MetaData{
  69. "one": {
  70. "override": true,
  71. "new": "key",
  72. },
  73. "new": {
  74. "tab": account,
  75. },
  76. })
  77. if !reflect.DeepEqual(m, MetaData{
  78. "one": {
  79. "key": "value",
  80. "override": true,
  81. "new": "key",
  82. },
  83. "new": {
  84. "tab": account,
  85. },
  86. }) {
  87. t.Errorf("metadata.Update didn't work: %#v", m)
  88. }
  89. }
  90. func TestMetaDataSanitize(t *testing.T) {
  91. var broken = _broken{}
  92. broken.Me = &broken
  93. broken.Data = "ohai"
  94. account.Name = "test"
  95. account.ID = "test"
  96. account.secret = "hush"
  97. account.Email = "example@example.com"
  98. account.EmptyEmail = ""
  99. account.NotEmptyEmail = "not_empty_email@example.com"
  100. m := MetaData{
  101. "one": {
  102. "bool": true,
  103. "int": 7,
  104. "float": 7.1,
  105. "complex": complex(1, 1),
  106. "func": func() {},
  107. "unsafe": unsafe.Pointer(broken.Me),
  108. "string": "string",
  109. "password": "secret",
  110. "array": []hash{{
  111. "creditcard": "1234567812345678",
  112. "broken": broken,
  113. }},
  114. "broken": broken,
  115. "account": account,
  116. },
  117. }
  118. n := m.sanitize([]string{"password", "creditcard"})
  119. if !reflect.DeepEqual(n, map[string]interface{}{
  120. "one": map[string]interface{}{
  121. "bool": true,
  122. "int": 7,
  123. "float": 7.1,
  124. "complex": "[complex128]",
  125. "string": "string",
  126. "unsafe": "[unsafe.Pointer]",
  127. "func": "[func()]",
  128. "password": "[REDACTED]",
  129. "array": []interface{}{map[string]interface{}{
  130. "creditcard": "[REDACTED]",
  131. "broken": map[string]interface{}{
  132. "Me": "[RECURSION]",
  133. "Data": "ohai",
  134. },
  135. }},
  136. "broken": map[string]interface{}{
  137. "Me": "[RECURSION]",
  138. "Data": "ohai",
  139. },
  140. "account": map[string]interface{}{
  141. "ID": "test",
  142. "Name": "test",
  143. "Plan": map[string]interface{}{
  144. "Premium": false,
  145. },
  146. "Password": "[REDACTED]",
  147. "email": "example@example.com",
  148. "not_empty_email": "not_empty_email@example.com",
  149. },
  150. },
  151. }) {
  152. t.Errorf("metadata.Sanitize didn't work: %#v", n)
  153. }
  154. }
  155. func ExampleMetaData() {
  156. notifier.Notify(errors.Errorf("hi world"),
  157. MetaData{"Account": {
  158. "id": account.ID,
  159. "name": account.Name,
  160. "paying?": account.Plan.Premium,
  161. }})
  162. }