Skip to content

Commit

Permalink
Remove use of Client Secret
Browse files Browse the repository at this point in the history
No longer need to use client secret to intialize sdk.

Test: Run demo app just by updating client id and account token (no more client secret is needed) and see if everyithings run smoothly.
  • Loading branch information
ambarwootric committed May 11, 2018
1 parent 00beb5f commit 8e57711
Show file tree
Hide file tree
Showing 9 changed files with 118 additions and 17 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 2.7.0 (2018-05-08)

- Add new method that just needs client_id & doesn't require the client_secret

## 2.6.1 (2018-04-09)

- Fix Layout for short followup question
Expand Down
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@ If you use Maven, you can include this library as a dependency:
<dependency>
<groupId>com.wootric</groupId>
<artifactId>wootric-sdk-android</artifactId>
<version>2.6.1</version>
<version>2.7.0</version>
</dependency>
```

### Using Gradle

```xml
compile 'com.wootric:wootric-sdk-android:2.6.1'
compile 'com.wootric:wootric-sdk-android:2.7.0'
```

## Initializing Wootric
Expand Down Expand Up @@ -74,12 +74,12 @@ WootricSDK task is to present a fully functional survey view with just a few lin
import com.wootric.androidsdk.Wootric;
```

4. Configure the SDK with your client ID, secret and account token:
4. Configure the SDK with your client ID and account token:

All you need to do is to add this code to your Activity's `onCreate` method:

```java
Wootric wootric = Wootric.init(this, CLIENT_ID, CLIENT_SECRET, ACCOUNT_TOKEN);
Wootric wootric = Wootric.init(this, CLIENT_ID, ACCOUNT_TOKEN);
```

5. To display the survey (if user is eligible - this check is built in the method) use:
Expand All @@ -99,7 +99,7 @@ import com.wootric.androidsdk.Wootric;

// Inside your Activity's onCreate method

Wootric wootric = Wootric.init(this, YOUR_CLIENT_ID, YOUR_CLIENT_SECRET, YOUR_ACCOUNT_TOKEN);
Wootric wootric = Wootric.init(this, YOUR_CLIENT_ID, YOUR_ACCOUNT_TOKEN);
wootric.setEndUserEmail("[email protected]");
// Use only for testing
wootric.setSurveyImmediately(true);
Expand Down
4 changes: 2 additions & 2 deletions androidsdk/gradle.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
VERSION_NAME=2.6.1
VERSION_CODE=261
VERSION_NAME=2.7.0
VERSION_CODE=270
GROUP=com.wootric

POM_DESCRIPTION=WootricSDK Android
Expand Down
37 changes: 37 additions & 0 deletions androidsdk/src/main/java/com/wootric/androidsdk/Wootric.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ public class Wootric {
* @param clientSecret Found in API section of the Wootric's admin panel.
* @param accountToken Found in Install section of the Wootric's admin panel.
*/
@Deprecated
public static Wootric init(FragmentActivity activity, String clientId, String clientSecret, String accountToken) {
Wootric local = singleton;
if(local == null) {
Expand All @@ -84,6 +85,30 @@ public static Wootric init(FragmentActivity activity, String clientId, String cl
return local;
}

/**
* It configures the SDK with required parameters.
*
* @param activity Activity where the survey will be presented.
* @param clientId Found in API section of the Wootric's admin panel.
* @param accountToken Found in Install section of the Wootric's admin panel.
*/
public static Wootric init(FragmentActivity activity, String clientId, String accountToken) {
Wootric local = singleton;
if(local == null) {
synchronized (Wootric.class) {
local = singleton;
if(local == null) {
checkNotNull(activity, "Activity");
checkNotNull(clientId, "Client Id");
checkNotNull(accountToken, "Account Token");
singleton = local = new Wootric(activity, clientId, accountToken);
}
}
}

return local;
}

public static void notifySurveyFinished(boolean surveyShown, boolean responseSent, Integer resurvey_days) {
if(singleton == null) return;

Expand Down Expand Up @@ -374,6 +399,18 @@ private Wootric(FragmentActivity activity, String clientId, String clientSecret,
permissionsValidator = new PermissionsValidator(weakContext);
}

private Wootric(FragmentActivity activity, String clientId, String accountToken) {
weakActivity = new WeakReference<>(activity);
weakContext = new WeakReference<>(activity.getApplicationContext());

endUser = new EndUser();
user = new User(clientId, accountToken);
settings = new Settings();

preferencesUtils = new PreferencesUtils(weakContext);
permissionsValidator = new PermissionsValidator(weakContext);
}

SurveyValidator buildSurveyValidator(User user, EndUser endUser, Settings settings,
WootricRemoteClient wootricRemoteClient, PreferencesUtils preferencesUtils) {
return new SurveyValidator(user, endUser, settings, wootricRemoteClient, preferencesUtils);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,9 @@ protected String requestUrl() {
protected void buildParams() {
paramsMap.put("grant_type", "client_credentials");
paramsMap.put("client_id", clientId);
paramsMap.put("client_secret", clientSecret);
if (clientSecret != null) {
paramsMap.put("client_secret", clientSecret);
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,21 @@
*/
public class User implements Parcelable {

private final String clientId;
private final String clientSecret;
private final String accountToken;
private String clientId;
private String clientSecret;
private String accountToken;

public User(String clientId, String clientSecret, String accountToken) {
this.clientId = clientId;
this.clientSecret = clientSecret;
this.accountToken = accountToken;
}

public User(String clientId, String accountToken) {
this.clientId = clientId;
this.accountToken = accountToken;
}

public String getClientId() {
return clientId;
}
Expand Down
60 changes: 57 additions & 3 deletions androidsdk/src/test/java/com/wootric/androidsdk/WootricTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.internal.matchers.Null;
import org.mockito.runners.MockitoJUnitRunner;

import java.util.HashMap;
Expand Down Expand Up @@ -43,12 +44,14 @@ public class WootricTest {
public void setUp() {
Wootric.singleton = null;
Wootric.init(TEST_ACTIVITY, CLIENT_ID, CLIENT_SECRET, ACCOUNT_TOKEN);
Wootric.init(TEST_ACTIVITY, CLIENT_ID, ACCOUNT_TOKEN);
}

@Test public void fails_whenContextIsNull() throws Exception {
try {
Wootric.singleton = null;
Wootric.init(null, CLIENT_ID, CLIENT_SECRET, ACCOUNT_TOKEN);
Wootric.init(null, CLIENT_ID, ACCOUNT_TOKEN);
fail("Null activity should throw exception");
} catch (IllegalArgumentException expected) {
}
Expand All @@ -58,6 +61,7 @@ public void setUp() {
try {
Wootric.singleton = null;
Wootric.init(new FragmentActivity(), null, CLIENT_SECRET, ACCOUNT_TOKEN);
Wootric.init(new FragmentActivity(), null, ACCOUNT_TOKEN);
fail("Null client id should throw exception");
} catch (IllegalArgumentException expected) {
}
Expand All @@ -67,6 +71,7 @@ public void setUp() {
try {
Wootric.singleton = null;
Wootric.init(new FragmentActivity(), CLIENT_ID, null, ACCOUNT_TOKEN);
Wootric.init(new FragmentActivity(), CLIENT_ID, ACCOUNT_TOKEN);
fail("Null client secret should throw exception");
} catch (IllegalArgumentException expected) {
}
Expand All @@ -76,6 +81,7 @@ public void setUp() {
try {
Wootric.singleton = null;
Wootric.init(new FragmentActivity(), CLIENT_ID, CLIENT_SECRET, null);
Wootric.init(new FragmentActivity(), CLIENT_ID, null);
fail("Null account token should throw exception");
} catch (IllegalArgumentException expected) {
}
Expand All @@ -84,9 +90,12 @@ public void setUp() {
@Test public void inits_singleton() throws Exception {
Wootric.singleton = null;
Wootric wootric = Wootric.init(TEST_ACTIVITY, CLIENT_ID, CLIENT_SECRET, ACCOUNT_TOKEN);
Wootric wootric_2 = Wootric.init(TEST_ACTIVITY, CLIENT_ID, CLIENT_SECRET, ACCOUNT_TOKEN);
Wootric wootric_1 = Wootric.init(TEST_ACTIVITY, CLIENT_ID, CLIENT_SECRET, ACCOUNT_TOKEN);
Wootric wootric_2 = Wootric.init(TEST_ACTIVITY, CLIENT_ID, ACCOUNT_TOKEN);
Wootric wootric_3 = Wootric.init(TEST_ACTIVITY, CLIENT_ID, ACCOUNT_TOKEN);

assertThat(wootric).isEqualTo(wootric_2);
assertThat(wootric).isEqualTo(wootric_1);
assertThat(wootric_2).isEqualTo(wootric_3);
}

@Test public void init_sets_endUser() throws Exception {
Expand Down Expand Up @@ -138,9 +147,12 @@ public void setsEndUserProperties() {

@Test public void setSurveyImmediately() throws Exception {
Wootric wootric = Wootric.init(new FragmentActivity(), CLIENT_ID, CLIENT_SECRET, ACCOUNT_TOKEN);
Wootric wootric_1 = Wootric.init(new FragmentActivity(), CLIENT_ID, ACCOUNT_TOKEN);
wootric.setSurveyImmediately(true);
wootric_1.setSurveyImmediately(true);

assertThat(wootric.settings.isSurveyImmediately()).isTrue();
assertThat(wootric_1.settings.isSurveyImmediately()).isTrue();
}

@Test
Expand Down Expand Up @@ -218,41 +230,69 @@ public void setsTweeterPage() {
@Test public void survey_startsSurvey() throws Exception {
Wootric.singleton = null;
Wootric wootric = spy(Wootric.init(TEST_ACTIVITY, CLIENT_ID, CLIENT_SECRET, ACCOUNT_TOKEN));
Wootric wootric_1 = spy(Wootric.init(TEST_ACTIVITY, CLIENT_ID, ACCOUNT_TOKEN));

doReturn(mockSurveyValidator).when(wootric).buildSurveyValidator(eq(wootric.user),
eq(wootric.endUser), eq(wootric.settings), any(WootricRemoteClient.class), any(PreferencesUtils.class));

doReturn(mockSurveyValidator).when(wootric_1).buildSurveyValidator(eq(wootric_1.user),
eq(wootric_1.endUser), eq(wootric_1.settings), any(WootricRemoteClient.class), any(PreferencesUtils.class));


doReturn(mockSurveyManager).when(wootric).buildSurveyManager(eq(wootric.weakActivity.get()),
any(WootricRemoteClient.class), eq(wootric.user),
eq(wootric.endUser), eq(wootric.settings),
any(PreferencesUtils.class), eq(mockSurveyValidator));

doReturn(mockSurveyManager).when(wootric_1).buildSurveyManager(eq(wootric_1.weakActivity.get()),
any(WootricRemoteClient.class), eq(wootric_1.user),
eq(wootric_1.endUser), eq(wootric_1.settings),
any(PreferencesUtils.class), eq(mockSurveyValidator));

wootric.permissionsValidator = mockPermissionsValidator;
wootric_1.permissionsValidator = mockPermissionsValidator;
doReturn(true).when(wootric.permissionsValidator).check();
doReturn(true).when(wootric_1.permissionsValidator).check();

wootric.survey();
verify(mockSurveyManager, times(1)).start();
wootric_1.survey();
verify(mockSurveyManager, times(2)).start();
assertThat(wootric.surveyInProgress).isTrue();
assertThat(wootric_1.surveyInProgress).isTrue();
}

@Test
public void doesNotStartSurvey_whenSurveyInProgress() {
Wootric.singleton = null;
Wootric wootric = spy(Wootric.init(TEST_ACTIVITY, CLIENT_ID, CLIENT_SECRET, ACCOUNT_TOKEN));
Wootric wootric_1 = spy(Wootric.init(TEST_ACTIVITY, CLIENT_ID, ACCOUNT_TOKEN));

doReturn(mockSurveyValidator).when(wootric).buildSurveyValidator(eq(wootric.user),
eq(wootric.endUser), eq(wootric.settings), any(WootricRemoteClient.class), any(PreferencesUtils.class));

doReturn(mockSurveyValidator).when(wootric_1).buildSurveyValidator(eq(wootric_1.user),
eq(wootric_1.endUser), eq(wootric_1.settings), any(WootricRemoteClient.class), any(PreferencesUtils.class));


doReturn(mockSurveyManager).when(wootric).buildSurveyManager(eq(wootric.weakActivity.get()),
any(WootricRemoteClient.class), eq(wootric.user),
eq(wootric.endUser), eq(wootric.settings),
any(PreferencesUtils.class), eq(mockSurveyValidator));

doReturn(mockSurveyManager).when(wootric_1).buildSurveyManager(eq(wootric_1.weakActivity.get()),
any(WootricRemoteClient.class), eq(wootric_1.user),
eq(wootric_1.endUser), eq(wootric_1.settings),
any(PreferencesUtils.class), eq(mockSurveyValidator));

wootric.permissionsValidator = mockPermissionsValidator;
wootric_1.permissionsValidator = mockPermissionsValidator;
doReturn(true).when(wootric.permissionsValidator).check();
doReturn(true).when(wootric_1.permissionsValidator).check();
wootric.surveyInProgress = true;
wootric_1.surveyInProgress = true;

wootric.survey();
wootric_1.survey();

verify(mockSurveyManager, times(0)).start();
}
Expand All @@ -261,20 +301,34 @@ public void doesNotStartSurvey_whenSurveyInProgress() {
public void doesNotStartSurvey_whenPermissionsValidatorChecksReturnsFalse() {
Wootric.singleton = null;
Wootric wootric = spy(Wootric.init(TEST_ACTIVITY, CLIENT_ID, CLIENT_SECRET, ACCOUNT_TOKEN));
Wootric wootric_1 = spy(Wootric.init(TEST_ACTIVITY, CLIENT_ID, ACCOUNT_TOKEN));

doReturn(mockSurveyValidator).when(wootric).buildSurveyValidator(eq(wootric.user),
eq(wootric.endUser), eq(wootric.settings), any(WootricRemoteClient.class), any(PreferencesUtils.class));

doReturn(mockSurveyValidator).when(wootric_1).buildSurveyValidator(eq(wootric_1.user),
eq(wootric_1.endUser), eq(wootric_1.settings), any(WootricRemoteClient.class), any(PreferencesUtils.class));


doReturn(mockSurveyManager).when(wootric).buildSurveyManager(eq(wootric.weakActivity.get()),
any(WootricRemoteClient.class), eq(wootric.user),
eq(wootric.endUser), eq(wootric.settings),
any(PreferencesUtils.class), eq(mockSurveyValidator));

doReturn(mockSurveyManager).when(wootric_1).buildSurveyManager(eq(wootric_1.weakActivity.get()),
any(WootricRemoteClient.class), eq(wootric_1.user),
eq(wootric_1.endUser), eq(wootric_1.settings),
any(PreferencesUtils.class), eq(mockSurveyValidator));

wootric.permissionsValidator = mockPermissionsValidator;
wootric_1.permissionsValidator = mockPermissionsValidator;
doReturn(false).when(wootric.permissionsValidator).check();
doReturn(false).when(wootric_1.permissionsValidator).check();
wootric.surveyInProgress = false;
wootric_1.surveyInProgress = false;

wootric.survey();
wootric_1.survey();

verify(mockSurveyManager, times(0)).start();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
public class MainActivity extends FragmentActivity {

private static final String CLIENT_ID = "CLIENT ID";
private static final String CLIENT_SECRET = "CLIENT SECRET";
private static final String ACCOUNT_TOKEN = "ACCOUNT TOKEN";

@Override
Expand All @@ -25,7 +24,7 @@ public void showSurvey(View view) {

private void startSurvey() {

Wootric wootric = Wootric.init(this, CLIENT_ID, CLIENT_SECRET, ACCOUNT_TOKEN);
Wootric wootric = Wootric.init(this, CLIENT_ID, ACCOUNT_TOKEN);
wootric.setEndUserEmail("[email protected]");
wootric.setSurveyImmediately(true);
wootric.survey();
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ buildscript {
google()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.0.1'
classpath 'com.android.tools.build:gradle:3.1.0'
classpath 'com.github.dcendents:android-maven-plugin:1.2'

// NOTE: Do not place your application dependencies here; they belong
Expand Down

0 comments on commit 8e57711

Please sign in to comment.