Skip to content

Commit fcb7089

Browse files
committed
TwilioFactorProvider: Add checks for only either "from" or "messagingServiceSid" specified but not both
1 parent ec87f8e commit fcb7089

2 files changed

Lines changed: 98 additions & 13 deletions

File tree

src/main/java/com/auth0/json/mgmt/guardian/TwilioFactorProvider.java

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,20 @@ public TwilioFactorProvider() {
3535
/**
3636
* Creates a Twilio settings object
3737
*
38+
* You must only specify either a non-null `from` or `messagingServiceSID`, but not both.
39+
*
3840
* @param from the Twilio From number.
3941
* @param messagingServiceSID the Twilio Messaging Service SID.
4042
* @param authToken the Twilio auth token.
4143
* @param sid the Twilio SID.
44+
* @throws IllegalArgumentException when both `from` and `messagingServiceSID` are set
4245
*/
4346
@JsonCreator
44-
public TwilioFactorProvider(@JsonProperty("from") String from, @JsonProperty("messaging_service_sid") String messagingServiceSID, @JsonProperty("auth_token") String authToken, @JsonProperty("sid") String sid) {
47+
public TwilioFactorProvider(@JsonProperty("from") String from, @JsonProperty("messaging_service_sid") String messagingServiceSID, @JsonProperty("auth_token") String authToken, @JsonProperty("sid") String sid)
48+
throws IllegalArgumentException {
49+
if (from != null && messagingServiceSID != null) {
50+
throw new IllegalArgumentException("You must specify either `from` or `messagingServiceSID`, but not both");
51+
}
4552
this.from = from;
4653
this.messagingServiceSID = messagingServiceSID;
4754
this.authToken = authToken;
@@ -62,11 +69,15 @@ public String getFrom() {
6269
* Setter for the Twilio From number.
6370
*
6471
* @param from the from number to set.
72+
* @throws IllegalArgumentException when both `from` and `messagingServiceSID` are set
6573
* @deprecated use the constructor instead
6674
*/
6775
@Deprecated
6876
@JsonProperty("from")
69-
public void setFrom(String from) {
77+
public void setFrom(String from) throws IllegalArgumentException {
78+
if (messagingServiceSID != null) {
79+
throw new IllegalArgumentException("You must specify either `from` or `messagingServiceSID`, but not both");
80+
}
7081
this.from = from;
7182
}
7283

@@ -84,11 +95,15 @@ public String getMessagingServiceSID() {
8495
* Setter for the Twilio Messaging Service SID.
8596
*
8697
* @param messagingServiceSID the messaging service SID.
98+
* @throws IllegalArgumentException when both `from` and `messagingServiceSID` are set
8799
* @deprecated use the constructor instead
88100
*/
89101
@Deprecated
90102
@JsonProperty("messaging_service_sid")
91-
public void setMessagingServiceSID(String messagingServiceSID) {
103+
public void setMessagingServiceSID(String messagingServiceSID) throws IllegalArgumentException {
104+
if (from != null) {
105+
throw new IllegalArgumentException("You must specify either `from` or `messagingServiceSID`, but not both");
106+
}
92107
this.messagingServiceSID = messagingServiceSID;
93108
}
94109

src/test/java/com/auth0/json/mgmt/guardian/TwilioFactorProviderTest.java

Lines changed: 80 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,53 +2,123 @@
22

33
import com.auth0.json.JsonMatcher;
44
import com.auth0.json.JsonTest;
5+
import org.junit.Rule;
56
import org.junit.Test;
7+
import org.junit.rules.ExpectedException;
68

79
import static org.hamcrest.MatcherAssert.assertThat;
8-
import static org.hamcrest.Matchers.is;
9-
import static org.hamcrest.Matchers.notNullValue;
10+
import static org.hamcrest.Matchers.*;
1011

1112
public class TwilioFactorProviderTest extends JsonTest<TwilioFactorProvider> {
1213

13-
private static final String json = "{\"from\":\"+12356789\",\"messaging_service_sid\":\"id321\",\"auth_token\":\"atokEn\",\"sid\":\"id123\"}";
14+
@Rule
15+
public ExpectedException exception = ExpectedException.none();
16+
17+
private static final String JSON_WITH_FROM = "{\"from\":\"+12356789\",\"auth_token\":\"atokEn\",\"sid\":\"id123\"}";
18+
private static final String JSON_WITH_MESSAGING_SERVICE_SID = "{\"messaging_service_sid\":\"id321\",\"auth_token\":\"atokEn\",\"sid\":\"id123\"}";
1419

1520
@Test
16-
public void shouldSerializeWithDeprecatedSetters() throws Exception {
21+
public void shouldFailConstructionWithBothFromAndMessagingServiceSID() throws Exception {
22+
exception.expect(IllegalArgumentException.class);
23+
exception.expectMessage("You must specify either `from` or `messagingServiceSID`, but not both");
24+
25+
new TwilioFactorProvider("+12356789", "messaging_service_sid", "atokEn", "id123");
26+
}
27+
28+
@Test
29+
public void shouldFailWhenSettingFromAndMessagingServiceSIDWasAlreadySet() throws Exception {
30+
exception.expect(IllegalArgumentException.class);
31+
exception.expectMessage("You must specify either `from` or `messagingServiceSID`, but not both");
32+
1733
TwilioFactorProvider provider = new TwilioFactorProvider();
18-
provider.setAuthToken("atokEn");
1934
provider.setFrom("+12356789");
2035
provider.setMessagingServiceSID("id321");
36+
}
37+
38+
@Test
39+
public void shouldFailWhenSettingMessagingServiceSIDAndFromWasAlreadySet() throws Exception {
40+
exception.expect(IllegalArgumentException.class);
41+
exception.expectMessage("You must specify either `from` or `messagingServiceSID`, but not both");
42+
43+
TwilioFactorProvider provider = new TwilioFactorProvider();
44+
provider.setMessagingServiceSID("id321");
45+
provider.setFrom("+12356789");
46+
}
47+
48+
@Test
49+
public void shouldSerializeWithDeprecatedSettersWithFrom() throws Exception {
50+
TwilioFactorProvider provider = new TwilioFactorProvider();
51+
provider.setAuthToken("atokEn");
52+
provider.setFrom("+12356789");
2153
provider.setSID("id123");
2254

2355
String serialized = toJSON(provider);
2456
assertThat(serialized, is(notNullValue()));
2557
assertThat(serialized, JsonMatcher.hasEntry("from", "+12356789"));
58+
assertThat(serialized, JsonMatcher.hasEntry("auth_token", "atokEn"));
59+
assertThat(serialized, JsonMatcher.hasEntry("sid", "id123"));
60+
assertThat(serialized, not(containsString("\"messaging_service_sid\"")));
61+
}
62+
63+
@Test
64+
public void shouldSerializeWithDeprecatedSettersWithMessagingServiceSID() throws Exception {
65+
TwilioFactorProvider provider = new TwilioFactorProvider();
66+
provider.setAuthToken("atokEn");
67+
provider.setMessagingServiceSID("id321");
68+
provider.setSID("id123");
69+
70+
String serialized = toJSON(provider);
71+
assertThat(serialized, is(notNullValue()));
72+
2673
assertThat(serialized, JsonMatcher.hasEntry("messaging_service_sid", "id321"));
2774
assertThat(serialized, JsonMatcher.hasEntry("auth_token", "atokEn"));
2875
assertThat(serialized, JsonMatcher.hasEntry("sid", "id123"));
76+
assertThat(serialized, not(containsString("\"from\"")));
2977
}
3078

3179
@Test
32-
public void shouldSerialize() throws Exception {
33-
TwilioFactorProvider provider = new TwilioFactorProvider("+12356789", "id321", "atokEn", "id123");
80+
public void shouldSerializeWithFrom() throws Exception {
81+
TwilioFactorProvider provider = new TwilioFactorProvider("+12356789", null, "atokEn", "id123");
3482

3583
String serialized = toJSON(provider);
3684
assertThat(serialized, is(notNullValue()));
3785
assertThat(serialized, JsonMatcher.hasEntry("from", "+12356789"));
86+
assertThat(serialized, JsonMatcher.hasEntry("auth_token", "atokEn"));
87+
assertThat(serialized, JsonMatcher.hasEntry("sid", "id123"));
88+
assertThat(serialized, not(containsString("\"messaging_service_sid\"")));
89+
}
90+
91+
@Test
92+
public void shouldSerializeWithMessaginServiceSID() throws Exception {
93+
TwilioFactorProvider provider = new TwilioFactorProvider(null, "id321", "atokEn", "id123");
94+
95+
String serialized = toJSON(provider);
96+
assertThat(serialized, is(notNullValue()));
3897
assertThat(serialized, JsonMatcher.hasEntry("messaging_service_sid", "id321"));
3998
assertThat(serialized, JsonMatcher.hasEntry("auth_token", "atokEn"));
4099
assertThat(serialized, JsonMatcher.hasEntry("sid", "id123"));
100+
assertThat(serialized, not(containsString("\"from\"")));
41101
}
42102

43103
@Test
44-
public void shouldDeserialize() throws Exception {
45-
TwilioFactorProvider provider = fromJSON(json, TwilioFactorProvider.class);
104+
public void shouldDeserializeWithFrom() throws Exception {
105+
TwilioFactorProvider provider = fromJSON(JSON_WITH_FROM, TwilioFactorProvider.class);
46106

47107
assertThat(provider, is(notNullValue()));
48108
assertThat(provider.getAuthToken(), is("atokEn"));
49109
assertThat(provider.getFrom(), is("+12356789"));
50-
assertThat(provider.getMessagingServiceSID(), is("id321"));
110+
assertThat(provider.getMessagingServiceSID(), is(nullValue()));
51111
assertThat(provider.getSID(), is("id123"));
52112
}
53113

114+
@Test
115+
public void shouldDeserializeWithMessagingServiceSID() throws Exception {
116+
TwilioFactorProvider provider = fromJSON(JSON_WITH_MESSAGING_SERVICE_SID, TwilioFactorProvider.class);
117+
118+
assertThat(provider, is(notNullValue()));
119+
assertThat(provider.getAuthToken(), is("atokEn"));
120+
assertThat(provider.getFrom(), is(nullValue()));
121+
assertThat(provider.getMessagingServiceSID(), is("id321"));
122+
assertThat(provider.getSID(), is("id123"));
123+
}
54124
}

0 commit comments

Comments
 (0)