|
| 1 | +package messagebird |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + "time" |
| 6 | +) |
| 7 | + |
| 8 | +var OtpGenerateObject []byte = []byte(`{ |
| 9 | + "id": "76d429606554b5827a46b12o29500954", |
| 10 | + "recipient": "31630174123", |
| 11 | + "reference": null, |
| 12 | + "status": "sent", |
| 13 | + "href": { |
| 14 | + "message": "https://rest.messagebird.com/messages/5b823a806554b5827b0cb66b58154766" |
| 15 | + }, |
| 16 | + "createdDatetime": "2015-05-07T12:18:47+00:00", |
| 17 | + "validUntilDatetime": "2015-05-07T12:19:17+00:00" |
| 18 | +}`) |
| 19 | + |
| 20 | +func TestOtpGenerate(t *testing.T) { |
| 21 | + SetServerResponse(200, OtpGenerateObject) |
| 22 | + |
| 23 | + result, err := mbClient.OtpGenerate("31630174123", nil) |
| 24 | + if err != nil { |
| 25 | + t.Fatalf("Didn't expect error while generating an OTP: %s", err) |
| 26 | + } |
| 27 | + |
| 28 | + if result.Id != "76d429606554b5827a46b12o29500954" { |
| 29 | + t.Errorf("Unexpected OTP message id: %s", result.Id) |
| 30 | + } |
| 31 | + |
| 32 | + if result.Recipient != "31630174123" { |
| 33 | + t.Errorf("Unexpected OTP message recipient: %s", result.Recipient) |
| 34 | + } |
| 35 | + |
| 36 | + if result.Reference != "" { |
| 37 | + t.Errorf("Unexpected OTP message reference: %s", result.Reference) |
| 38 | + } |
| 39 | + |
| 40 | + if result.Status != "sent" { |
| 41 | + t.Errorf("Unexpected OTP message status: %s", result.Status) |
| 42 | + } |
| 43 | + |
| 44 | + if result.CreatedDatetime == nil || result.CreatedDatetime.Format(time.RFC3339) != "2015-05-07T12:18:47Z" { |
| 45 | + t.Errorf("Unexpected OTP message created datetime: %s", result.CreatedDatetime) |
| 46 | + } |
| 47 | + |
| 48 | + if result.ValidUntilDatetime == nil || result.ValidUntilDatetime.Format(time.RFC3339) != "2015-05-07T12:19:17Z" { |
| 49 | + t.Errorf("Unexpected OTP message valid until datetime: %s", result.ValidUntilDatetime) |
| 50 | + } |
| 51 | + |
| 52 | + message, exists := result.Href["message"] |
| 53 | + if !exists { |
| 54 | + t.Errorf("Unexpected OTP message href value: %s", result.Href) |
| 55 | + } |
| 56 | + |
| 57 | + if exists && message != "https://rest.messagebird.com/messages/5b823a806554b5827b0cb66b58154766" { |
| 58 | + t.Errorf("Unexpected OTP message href message: %s", message) |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +var OtpVerifyObject []byte = []byte(`{ |
| 63 | + "id": "8b912ea03554b7a3d5d6e22o95082672", |
| 64 | + "recipient": "31630174123", |
| 65 | + "reference": null, |
| 66 | + "status": "verified", |
| 67 | + "href": { |
| 68 | + "message": "https://rest.messagebird.com/messages/8668de203554b7a3d62b5e7b62054920" |
| 69 | + }, |
| 70 | + "createdDatetime": "2015-05-07T14:44:13+00:00", |
| 71 | + "validUntilDatetime": "2015-05-07T14:44:43+00:00" |
| 72 | +}`) |
| 73 | + |
| 74 | +func TestOtpVerify(t *testing.T) { |
| 75 | + SetServerResponse(200, OtpVerifyObject) |
| 76 | + |
| 77 | + result, err := mbClient.OtpVerify("31630174123", "302443", nil) |
| 78 | + if err != nil { |
| 79 | + t.Fatalf("Didn't expect error while verifying an OTP: %s", err) |
| 80 | + } |
| 81 | + |
| 82 | + if result.Id != "8b912ea03554b7a3d5d6e22o95082672" { |
| 83 | + t.Errorf("Unexpected OTP message id: %s", result.Id) |
| 84 | + } |
| 85 | + |
| 86 | + if result.Recipient != "31630174123" { |
| 87 | + t.Errorf("Unexpected OTP message recipient: %s", result.Recipient) |
| 88 | + } |
| 89 | + |
| 90 | + if result.Reference != "" { |
| 91 | + t.Errorf("Unexpected OTP message reference: %s", result.Reference) |
| 92 | + } |
| 93 | + |
| 94 | + if result.Status != "verified" { |
| 95 | + t.Errorf("Unexpected OTP message status: %s", result.Status) |
| 96 | + } |
| 97 | + |
| 98 | + if result.CreatedDatetime == nil || result.CreatedDatetime.Format(time.RFC3339) != "2015-05-07T14:44:13Z" { |
| 99 | + t.Errorf("Unexpected OTP message created datetime: %s", result.CreatedDatetime) |
| 100 | + } |
| 101 | + |
| 102 | + if result.ValidUntilDatetime == nil || result.ValidUntilDatetime.Format(time.RFC3339) != "2015-05-07T14:44:43Z" { |
| 103 | + t.Errorf("Unexpected OTP message valid until datetime: %s", result.ValidUntilDatetime) |
| 104 | + } |
| 105 | + |
| 106 | + message, exists := result.Href["message"] |
| 107 | + if !exists { |
| 108 | + t.Errorf("Unexpected OTP message href value: %s", result.Href) |
| 109 | + } |
| 110 | + |
| 111 | + if exists && message != "https://rest.messagebird.com/messages/8668de203554b7a3d62b5e7b62054920" { |
| 112 | + t.Errorf("Unexpected OTP message href message: %s", message) |
| 113 | + } |
| 114 | +} |
0 commit comments