-
Notifications
You must be signed in to change notification settings - Fork 0
Laurislopata/signin #52
Changes from 24 commits
422b04e
2ab5a06
c7b9e84
0568213
547784f
958c1ca
1503d5c
e80ba02
7653243
8487a00
6ab78ac
56a61b2
774a4bd
0c1f0b8
6b62476
fd0e3b8
dac18f0
63f7030
843f504
0ccd964
5abf2d1
1d4611b
eccaba7
6ae0e0e
37d3ba4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
package ch.sdp.vibester | ||
|
||
import androidx.test.espresso.Espresso.onView | ||
import androidx.test.espresso.action.ViewActions | ||
import androidx.test.espresso.action.ViewActions.click | ||
import androidx.test.espresso.action.ViewActions.closeSoftKeyboard | ||
import androidx.test.espresso.assertion.ViewAssertions.matches | ||
import androidx.test.espresso.matcher.ViewMatchers.withId | ||
import androidx.test.espresso.matcher.ViewMatchers.withText | ||
import androidx.test.ext.junit.rules.ActivityScenarioRule | ||
import androidx.test.ext.junit.runners.AndroidJUnit4 | ||
import androidx.test.platform.app.InstrumentationRegistry | ||
import ch.sdp.vibester.auth.FireBaseAuthenticator | ||
import org.junit.Assert.assertEquals | ||
import org.junit.Rule | ||
import org.junit.Test | ||
import org.junit.runner.RunWith | ||
import kotlin.random.Random | ||
|
||
|
||
/** | ||
* Instrumented test, which will execute on an Android device. | ||
* | ||
* See [testing documentation](http://d.android.com/tools/testing). | ||
*/ | ||
@RunWith(AndroidJUnit4::class) | ||
class SignInActivityTest { | ||
|
||
@Test | ||
fun useAppContext() { | ||
// Context of the app under test. | ||
val appContext = InstrumentationRegistry.getInstrumentation().targetContext | ||
assertEquals("ch.sdp.vibester", appContext.packageName) | ||
} | ||
|
||
@get:Rule | ||
val testRule = ActivityScenarioRule( | ||
Register::class.java | ||
) | ||
|
||
|
||
@Test | ||
fun logInCorrect() { | ||
val username = "[email protected]" | ||
val password = "password" | ||
onView(withId(R.id.username)).perform(ViewActions.typeText(username), closeSoftKeyboard()) | ||
onView(withId(R.id.password)).perform(ViewActions.typeText(password), closeSoftKeyboard()) | ||
onView(withId(R.id.logIn)).perform(click()) | ||
Thread.sleep(3_000) | ||
onView(withId(R.id.email)).check(matches(withText("[email protected]"))) | ||
} | ||
|
||
@Test | ||
fun logInIncorrect() { | ||
val username = "[email protected]" | ||
val password = "password" | ||
onView(withId(R.id.username)).perform(ViewActions.typeText(username), closeSoftKeyboard()) | ||
onView(withId(R.id.password)).perform(ViewActions.typeText(password), closeSoftKeyboard()) | ||
onView(withId(R.id.logIn)).perform(click()) | ||
Thread.sleep(3_000) | ||
onView(withId(R.id.email)).check(matches(withText("Authentication error"))) | ||
} | ||
|
||
@Test | ||
fun createAccountIncorrect() { | ||
val username = "[email protected]" | ||
val password = "password" | ||
onView(withId(R.id.username)).perform(ViewActions.typeText(username), closeSoftKeyboard()) | ||
onView(withId(R.id.password)).perform(ViewActions.typeText(password), closeSoftKeyboard()) | ||
onView(withId(R.id.createAcc)).perform(click()) | ||
Thread.sleep(3_000) | ||
onView(withId(R.id.email)).check(matches(withText("Authentication error"))) | ||
} | ||
|
||
@Test | ||
fun createAccountCorrect() { | ||
val randomInt = Random.nextInt(0, 10000) | ||
val password = "password" | ||
val username = randomInt.toString().plus("@gg.com") | ||
onView(withId(R.id.username)).perform(ViewActions.typeText(username), closeSoftKeyboard()) | ||
onView(withId(R.id.password)).perform(ViewActions.typeText(password), closeSoftKeyboard()) | ||
onView(withId(R.id.createAcc)).perform(click()) | ||
Thread.sleep(3_000) | ||
onView(withId(R.id.email)).check(matches(withText(randomInt.toString().plus("@gg.com")))) | ||
} | ||
|
||
|
||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,6 +19,5 @@ class GreetingActivity : AppCompatActivity() { | |
|
||
textViewActivity.text = "Hello $name!" | ||
|
||
|
||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
package ch.sdp.vibester | ||
|
||
import android.content.Intent | ||
import androidx.appcompat.app.AppCompatActivity | ||
import android.os.Bundle | ||
import android.widget.Button | ||
import android.widget.EditText | ||
import android.widget.TextView | ||
import android.widget.Toast | ||
import ch.sdp.vibester.auth.FireBaseAuthenticator | ||
import com.google.android.gms.auth.api.signin.GoogleSignIn | ||
import com.google.android.gms.auth.api.signin.GoogleSignInClient | ||
import com.google.android.gms.auth.api.signin.GoogleSignInOptions | ||
import com.google.android.gms.tasks.Task | ||
import com.google.firebase.auth.AuthResult | ||
|
||
class Register : AppCompatActivity() { | ||
|
||
// private lateinit var googleSignInClient: GoogleSignInClient | ||
|
||
private lateinit var authenticator: FireBaseAuthenticator | ||
|
||
private lateinit var email: TextView | ||
|
||
|
||
override fun onCreate(savedInstanceState: Bundle?) { | ||
super.onCreate(savedInstanceState) | ||
setContentView(R.layout.activity_google_log_in) | ||
|
||
// val gso = GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Consider adding @fixme or explain in the comment why stop using google sign in to remind us later in case |
||
// .requestEmail() | ||
// .build() | ||
|
||
// googleSignInClient = GoogleSignIn.getClient(this, gso) | ||
|
||
// Initialize Firebase Auth | ||
authenticator = FireBaseAuthenticator() | ||
|
||
val btCreateAcc = findViewById<Button>(R.id.createAcc) | ||
val btLogIn = findViewById<Button>(R.id.logIn) | ||
// val googleSignIn = findViewById<Button>(R.id.googleBtn) | ||
|
||
|
||
val username = findViewById<EditText>(R.id.username) | ||
val password = findViewById<EditText>(R.id.password) | ||
email = findViewById<TextView>(R.id.email) | ||
|
||
btCreateAcc.setOnClickListener { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Similar blocks of code found in 2 locations. Consider refactoring. |
||
createAccount(username.text.toString(), password.text.toString()) | ||
} | ||
|
||
btLogIn.setOnClickListener { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Similar blocks of code found in 2 locations. Consider refactoring. |
||
signIn(username.text.toString(), password.text.toString()) | ||
} | ||
|
||
// googleSignIn.setOnClickListener { | ||
// signInGoogle() | ||
// } | ||
|
||
|
||
} | ||
|
||
public override fun onStart() { | ||
super.onStart() | ||
val currentUser = authenticator.auth.currentUser | ||
if(currentUser != null){ | ||
reload(); | ||
} | ||
} | ||
|
||
// public override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { | ||
// super.onActivityResult(requestCode, resultCode, data) | ||
// updateUI(authenticator.googleActivityResult(requestCode, resultCode, data)); | ||
// } | ||
|
||
// private fun signInGoogle() { | ||
// val intent = googleSignInClient.signInIntent | ||
// startActivityForResult(intent, 1000) | ||
// } | ||
|
||
private fun createAccount(email: String, password: String) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Similar blocks of code found in 2 locations. Consider refactoring. |
||
authenticator.createAccount(email, password) | ||
.addOnCompleteListener(this) { task -> | ||
onCompleteAuthentication(task) | ||
} | ||
} | ||
|
||
private fun signIn(email: String, password: String) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Similar blocks of code found in 2 locations. Consider refactoring. |
||
authenticator.signIn(email, password) | ||
.addOnCompleteListener(this) { task -> | ||
onCompleteAuthentication(task) | ||
} | ||
} | ||
|
||
private fun onCompleteAuthentication(task: Task<AuthResult>) { | ||
if (task.isSuccessful) { | ||
Toast.makeText( | ||
baseContext, "You have logged in successfully", | ||
Toast.LENGTH_SHORT | ||
).show() | ||
val user = authenticator.auth.currentUser | ||
if (user != null) { | ||
updateUI(user.email) | ||
} | ||
} else { | ||
Toast.makeText(baseContext, "Authentication failed.", | ||
Toast.LENGTH_SHORT).show() | ||
updateUI("Authentication error") | ||
} | ||
} | ||
|
||
private fun reload() { | ||
|
||
} | ||
|
||
private fun updateUI(emailText: String?) { | ||
email.text = emailText | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package ch.sdp.vibester.auth | ||
|
||
import android.content.ContentValues | ||
import android.content.Intent | ||
import android.util.Log | ||
import android.view.View | ||
import android.widget.TextView | ||
import android.widget.Toast | ||
import com.google.android.gms.auth.api.signin.GoogleSignIn | ||
import com.google.android.gms.auth.api.signin.GoogleSignInClient | ||
import com.google.android.gms.common.api.ApiException | ||
import com.google.android.gms.tasks.Task | ||
import com.google.firebase.auth.AuthResult | ||
import com.google.firebase.auth.FirebaseAuth | ||
import com.google.firebase.auth.ktx.auth | ||
import com.google.firebase.ktx.Firebase | ||
|
||
class FireBaseAuthenticator() { | ||
|
||
|
||
val auth: FirebaseAuth = Firebase.auth | ||
|
||
|
||
fun signIn(email: String, password: String): Task<AuthResult> { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Similar blocks of code found in 2 locations. Consider refactoring. |
||
return auth.signInWithEmailAndPassword(email, password) | ||
} | ||
|
||
fun createAccount(email: String, password: String): Task<AuthResult> { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Similar blocks of code found in 2 locations. Consider refactoring. |
||
return auth.createUserWithEmailAndPassword(email, password) | ||
} | ||
|
||
// fun googleActivityResult(requestCode: Int, resultCode: Int, data: Intent?): String? { | ||
// return if(requestCode == 1000) { | ||
// val task = GoogleSignIn.getSignedInAccountFromIntent(data) | ||
// try { | ||
// val account = task.getResult(ApiException::class.java)!! | ||
// account.email | ||
// } catch (e: ApiException) { | ||
// "Authentication error" | ||
// } | ||
// } else { | ||
// "Authentication error" | ||
// } | ||
// } | ||
|
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.