Skip to content
This repository has been archived by the owner on Feb 16, 2024. It is now read-only.

Developed Login for DevAuthBackEnd #48 #65

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions app/build.gradle
Expand Up @@ -47,6 +47,7 @@ android {
dependencies {
compile 'com.android.support:support-v13:23.3.0'
compile 'com.android.support:appcompat-v7:23.3.0'
compile "com.android.support:recyclerview-v7:23.0.1"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is a small thing, but can you remove the period from this commit message?

compile 'com.google.android.gms:play-services-gcm:8.4.0'
compile 'com.google.android.gms:play-services-auth:8.4.0'
compile 'com.j256.ormlite:ormlite-core:4.48'
Expand Down
@@ -0,0 +1,136 @@
package com.zulip.android.activities;


import android.support.test.espresso.NoMatchingViewException;
import android.support.test.espresso.ViewAssertion;
import android.support.test.espresso.ViewInteraction;
import android.support.test.espresso.matcher.BoundedMatcher;
import android.support.test.rule.ActivityTestRule;
import android.support.test.runner.AndroidJUnit4;
import android.support.v7.widget.RecyclerView;
import android.test.suitebuilder.annotation.LargeTest;
import android.view.View;
import android.widget.Button;

import com.zulip.android.R;
import com.zulip.android.ZulipApp;

import org.hamcrest.Description;
import org.hamcrest.Matcher;
import org.hamcrest.Matchers;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;

import static android.support.test.espresso.Espresso.onView;
import static android.support.test.espresso.action.ViewActions.click;
import static android.support.test.espresso.action.ViewActions.closeSoftKeyboard;
import static android.support.test.espresso.action.ViewActions.replaceText;
import static android.support.test.espresso.matcher.ViewMatchers.assertThat;
import static android.support.test.espresso.matcher.ViewMatchers.isDisplayed;
import static android.support.test.espresso.matcher.ViewMatchers.withHint;
import static android.support.test.espresso.matcher.ViewMatchers.withId;
import static android.support.test.espresso.matcher.ViewMatchers.withParent;
import static android.support.test.espresso.matcher.ViewMatchers.withText;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.core.AllOf.allOf;

@LargeTest
@RunWith(AndroidJUnit4.class)
public class LoginDevAuthTest {

@Rule
public ActivityTestRule<ZulipActivity> mActivityTestRule = new ActivityTestRule<>(ZulipActivity.class);

private static String EMAIL_TEST = "";
private static String SERVER_URL = "http://10.0.3.2:9991/api/";

// Convenience helper
@Test
public void TestSerially() {
getDevEmails();
loginThroughDevMail();
}

@Before
public void setup() {
closeSoftKeyboard();
}

public void getDevEmails() {

closeSoftKeyboard();
//Uncheck Checkbox
ViewInteraction checkBox = onView(
allOf(withId(R.id.checkbox_usezulip), withText("Use Zulip.com"),
withParent(withId(R.id.server)),
isDisplayed()));
checkBox.perform(click());

//Give a Server URL
ViewInteraction editText = onView(allOf(withId(R.id.server_url), withHint(R.string.server_domain), withParent(withId(R.id.server)), isDisplayed()));
editText.perform(replaceText(SERVER_URL));

closeSoftKeyboard();

//Click DevAuth TextView Button
ViewInteraction textView = onView(allOf(withId(R.id.local_server_button), withText("Dev Backend testing server"), isDisplayed()));
textView.perform(click());

//Check if there are Emails
onView(withId(R.id.devAuthRecyclerView)).check(hasItemsCount());
}

private static boolean matchedBefore = false;

//This filter returns only one View!
private Matcher<View> emailFilter() {

return new BoundedMatcher<View, Button>(Button.class) {
@Override
public void describeTo(Description description) {
description.appendText("ERROR");
}

@Override
protected boolean matchesSafely(Button item) {
if (!matchedBefore && item.getText().toString().contains("@")) {
EMAIL_TEST = item.getText().toString();
matchedBefore = true;
return true;
}
return false;
}
};
}


public void loginThroughDevMail() {

//If EMAIL not specified click on first EMAIL.
if (EMAIL_TEST.equals("")) {
onView(allOf(withId(android.R.id.text1), emailFilter())).perform(click());
} else {
//Find and click the E-Mail Button.
ViewInteraction button = onView(Matchers.allOf(withId(android.R.id.text1), withText(EMAIL_TEST), isDisplayed()));
button.perform(click());
}

//Verify Correct E-Mail is Stored
assertThat(ZulipApp.get().getEmail(), is(EMAIL_TEST));
}

public static ViewAssertion hasItemsCount() {
return new ViewAssertion() {
@Override
public void check(View view, NoMatchingViewException e) {
if (!(view instanceof RecyclerView)) {
throw e;
}
RecyclerView rv = (RecyclerView) view;
assertThat("Items less than 2, which means no E-Mails!", rv.getAdapter().getItemCount(), Matchers.greaterThan(2));
}
};
}
}
2 changes: 2 additions & 0 deletions app/src/main/AndroidManifest.xml
Expand Up @@ -68,6 +68,8 @@
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />

<activity android:name=".activities.DevAuthActivity"></activity>
</application>

</manifest>
3 changes: 0 additions & 3 deletions app/src/main/java/com/zulip/android/ZulipApp.java
Expand Up @@ -150,9 +150,6 @@ public void clearConnectionState() {
* @return either the production or staging server's URI
*/
public String getServerURI() {
if (getEmail().equals("iago@zulip.com")) {
return "http://10.0.2.2:9991/api/";
}
return settings.getString("server_url", DEFAULT_SERVER_URL);
}
public String getApiKey() {
Expand Down
101 changes: 101 additions & 0 deletions app/src/main/java/com/zulip/android/activities/AuthEmailAdapter.java
@@ -0,0 +1,101 @@
package com.zulip.android.activities;

import android.content.Context;
import android.support.annotation.ColorInt;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.zulip.android.R;
import com.zulip.android.util.AuthClickListener;

import java.util.List;

class AuthEmailAdapter extends RecyclerView.Adapter<AuthEmailAdapter.AuthEmailViewHolder> {

private static final int VIEW_TYPE_HEADER = 0;
private static final int VIEW_TYPE_ADMIN = 1;
private static final int VIEW_TYPE_USER = 2;
private List<String> emails;
private int userStringPosition; //Position of the User String header
private static AuthClickListener authClickListener;

private
@ColorInt
int devAuthUserColor;
private
@ColorInt
int devAuthAdminColor;

AuthEmailAdapter(List<String> emails, int directAdminSize, Context context) {
this.userStringPosition = directAdminSize + 1; //+1 due to the Administrator String
this.emails = emails;
devAuthUserColor = ContextCompat.getColor(context, R.color.dev_auth_user);
devAuthAdminColor = ContextCompat.getColor(context, R.color.dev_auth_admin);
}

@Override
public AuthEmailViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = null;
if (viewType == VIEW_TYPE_HEADER)
v = LayoutInflater.from(parent.getContext()).inflate(R.layout.dev_auth_header, parent, false);
else
v = LayoutInflater.from(parent.getContext()).inflate(R.layout.dev_auth_row, parent, false);
return new AuthEmailViewHolder(v);
}

@Override
public void onBindViewHolder(AuthEmailViewHolder holder, int position) {
if (position == 0) {
if (userStringPosition == 1)
((TextView) holder.view).setText(R.string.admin_none); //Show none if no admins
else ((TextView) holder.view).setText(R.string.admin);
} else if (position == userStringPosition)
((TextView) holder.view).setText(R.string.normal_user);
else if (position > userStringPosition) {
((Button) holder.view).setText(emails.get(position - 2));
holder.view.setBackgroundColor(devAuthUserColor);
} else {
((Button) holder.view).setText(emails.get(position - 1));
holder.view.setBackgroundColor(devAuthAdminColor);
}
}

@Override
public int getItemCount() {
return emails.size() + 2;
//+2 due to the two headers {"Admin and user"}
}

@Override
public int getItemViewType(int position) {
if (position == 0 || position == userStringPosition) return VIEW_TYPE_HEADER;
if (position > userStringPosition) return VIEW_TYPE_USER;
else return VIEW_TYPE_ADMIN;
}

void setOnItemClickListener(AuthClickListener authClickListener) {
AuthEmailAdapter.authClickListener = authClickListener;
}

class AuthEmailViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
View view;

AuthEmailViewHolder(View itemView) {
super(itemView);
view = itemView.findViewById(android.R.id.text1);
if (view instanceof Button) {
view.setOnClickListener(this);
}
}

@Override
public void onClick(View v) {
authClickListener.onItemClick(((Button) v).getText().toString());
}
}
}
@@ -0,0 +1,74 @@
package com.zulip.android.activities;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.zulip.android.R;
import com.zulip.android.networking.AsyncDevGetEmails;
import com.zulip.android.networking.AsyncLogin;
import com.zulip.android.util.AuthClickListener;
import com.zulip.android.util.ZLog;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class DevAuthActivity extends Activity {
private RecyclerView recyclerView;
private ProgressDialog connectionProgressDialog;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dev_auth);
String json = getIntent().getStringExtra(AsyncDevGetEmails.EMAIL_JSON);
recyclerView = (RecyclerView) findViewById(R.id.devAuthRecyclerView);
List<String> emails = new ArrayList<>();
int directAdminSize = 1;
connectionProgressDialog = new ProgressDialog(this);
connectionProgressDialog.setMessage(getString(R.string.signing_in));

try {
JSONObject jsonObject = new JSONObject(json);
JSONArray jsonArray = jsonObject.getJSONArray("direct_admins");
for (int i = 0; i < jsonArray.length(); i++) {
emails.add(jsonArray.get(i).toString());
}
directAdminSize = jsonArray.length();
jsonArray = jsonObject.getJSONArray("direct_users");
for (int i = 0; i < jsonArray.length(); i++) {
emails.add(jsonArray.get(i).toString());
}
} catch (JSONException e) {
ZLog.logException(e);
}
AuthEmailAdapter authEmailAdapter = new AuthEmailAdapter(emails, directAdminSize, DevAuthActivity.this);
recyclerView.setAdapter(authEmailAdapter);
authEmailAdapter.setOnItemClickListener(new AuthClickListener() {
@Override
public void onItemClick(String email) {
AsyncLogin asyncLogin = new AsyncLogin(DevAuthActivity.this, email, null, true);
asyncLogin.execute();
connectionProgressDialog.show();
}
});
recyclerView.setLayoutManager(new LinearLayoutManager(DevAuthActivity.this) {
});
}

public void openHome() {
// Cancel before leaving activity to avoid leaking windows
connectionProgressDialog.dismiss();
Intent i = new Intent(this, ZulipActivity.class);
startActivity(i);
finish();
}
}