PageRenderTime 108ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/vendor/github.com/mailgun/mailgun-go/acceptance/domains_test.go

https://gitlab.com/admin-github-cloud/dex
Go | 96 lines | 80 code | 9 blank | 7 comment | 19 complexity | e835987ccb4a684f639f0bd061a3bebe MD5 | raw file
  1. // +build acceptance
  2. package acceptance
  3. import (
  4. "crypto/rand"
  5. "fmt"
  6. "github.com/mailgun/mailgun-go"
  7. "testing"
  8. )
  9. func TestGetDomains(t *testing.T) {
  10. domain := reqEnv(t, "MG_DOMAIN")
  11. apiKey := reqEnv(t, "MG_API_KEY")
  12. mg := mailgun.NewMailgun(domain, apiKey, "")
  13. n, domains, err := mg.GetDomains(mailgun.DefaultLimit, mailgun.DefaultSkip)
  14. if err != nil {
  15. t.Fatal(err)
  16. }
  17. fmt.Printf("TestGetDomains: %d domains retrieved\n", n)
  18. for _, d := range domains {
  19. fmt.Printf("TestGetDomains: %#v\n", d)
  20. }
  21. }
  22. func TestGetSingleDomain(t *testing.T) {
  23. domain := reqEnv(t, "MG_DOMAIN")
  24. apiKey := reqEnv(t, "MG_API_KEY")
  25. mg := mailgun.NewMailgun(domain, apiKey, "")
  26. _, domains, err := mg.GetDomains(mailgun.DefaultLimit, mailgun.DefaultSkip)
  27. if err != nil {
  28. t.Fatal(err)
  29. }
  30. dr, rxDnsRecords, txDnsRecords, err := mg.GetSingleDomain(domains[0].Name)
  31. if err != nil {
  32. t.Fatal(err)
  33. }
  34. fmt.Printf("TestGetSingleDomain: %#v\n", dr)
  35. for _, rxd := range rxDnsRecords {
  36. fmt.Printf("TestGetSingleDomains: %#v\n", rxd)
  37. }
  38. for _, txd := range txDnsRecords {
  39. fmt.Printf("TestGetSingleDomains: %#v\n", txd)
  40. }
  41. }
  42. func TestGetSingleDomainNotExist(t *testing.T) {
  43. domain := reqEnv(t, "MG_DOMAIN")
  44. apiKey := reqEnv(t, "MG_API_KEY")
  45. mg := mailgun.NewMailgun(domain, apiKey, "")
  46. _, _, _, err := mg.GetSingleDomain(randomString(32, "com.edu.org.")+".com")
  47. if err == nil {
  48. t.Fatal("Did not expect a domain to exist")
  49. }
  50. ure, ok := err.(*mailgun.UnexpectedResponseError)
  51. if !ok {
  52. t.Fatal("Expected UnexpectedResponseError")
  53. }
  54. if ure.Actual != 404 {
  55. t.Fatalf("Expected 404 response code; got %d", ure.Actual)
  56. }
  57. }
  58. func TestAddDeleteDomain(t *testing.T) {
  59. // First, we need to add the domain.
  60. domain := reqEnv(t, "MG_DOMAIN")
  61. apiKey := reqEnv(t, "MG_API_KEY")
  62. mg := mailgun.NewMailgun(domain, apiKey, "")
  63. randomDomainName := randomString(16, "DOMAIN") + ".example.com"
  64. randomPassword := randomString(16, "PASSWD")
  65. err := mg.CreateDomain(randomDomainName, randomPassword, mailgun.Tag, false)
  66. if err != nil {
  67. t.Fatal(err)
  68. }
  69. // Next, we delete it.
  70. err = mg.DeleteDomain(randomDomainName)
  71. if err != nil {
  72. t.Fatal(err)
  73. }
  74. }
  75. // randomString generates a string of given length, but random content.
  76. // All content will be within the ASCII graphic character set.
  77. // (Implementation from Even Shaw's contribution on
  78. // http://stackoverflow.com/questions/12771930/what-is-the-fastest-way-to-generate-a-long-random-string-in-go).
  79. func randomString(n int, prefix string) string {
  80. const alphanum = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
  81. var bytes = make([]byte, n)
  82. rand.Read(bytes)
  83. for i, b := range bytes {
  84. bytes[i] = alphanum[b%byte(len(alphanum))]
  85. }
  86. return prefix + string(bytes)
  87. }