<commit_before>package org.sagebionetworks.bridge.sdk.integration;

import static org.junit.Assert.*;

import org.apache.http.HttpResponse;
import org.apache.http.client.fluent.Request;
import org.apache.http.entity.StringEntity;
import org.apache.http.util.EntityUtils;
import org.junit.Test;
import org.junit.experimental.categories.Category;
import org.sagebionetworks.bridge.IntegrationSmokeTest;
import org.sagebionetworks.bridge.Tests;
import org.sagebionetworks.bridge.sdk.AdminClient;
import org.sagebionetworks.bridge.sdk.ClientProvider;
import org.sagebionetworks.bridge.sdk.Config;
import org.sagebionetworks.bridge.sdk.Config.Props;
import org.sagebionetworks.bridge.sdk.Session;
import org.sagebionetworks.bridge.sdk.TestUserHelper;
import org.sagebionetworks.bridge.sdk.TestUserHelper.TestUser;
import org.sagebionetworks.bridge.sdk.exceptions.BridgeServerException;
import org.sagebionetworks.bridge.sdk.models.studies.Study;
import org.sagebionetworks.bridge.sdk.models.users.EmailCredentials;
import org.sagebionetworks.bridge.sdk.models.users.SignInCredentials;
import org.sagebionetworks.bridge.sdk.models.users.SignUpCredentials;

@Category(IntegrationSmokeTest.class)
public class AuthenticationTest {

    @Test
    public void canResendEmailVerification() {
        String username = TestUserHelper.makeUserName(AuthenticationTest.class);
        String email = username + "@sagebase.org";
        String password = "<PASSWORD>";
        
        try {
            
            SignUpCredentials signUp = new SignUpCredentials(Tests.TEST_KEY, username, email, password);
            ClientProvider.signUp(signUp);
            
            // Beyond an exception being thrown, there's not a lot you can test here.
            ClientProvider.resendEmailVerification(new EmailCredentials(Tests.TEST_KEY, email));
            
        } finally {
            Config config = ClientProvider.getConfig();
            Session session = ClientProvider.signIn(config.getAdminCredentials());
            session.getAdminClient().deleteUser(email);
        }
    }
    
    @Test
    public void resendingEmailVerificationToUnknownEmailDoesNotThrowException() throws Exception {
        ClientProvider.resendEmailVerification(new EmailCredentials(Tests.TEST_KEY, "<EMAIL>"));
    }
    
    @Test
    public void requestingResetPasswordForUnknownEmailDoesNotThrowException() throws Exception {
        ClientProvider.requestResetPassword(new EmailCredentials(Tests.TEST_KEY, "<EMAIL>"));
    }
    
    @Test
    public void accountWithOneStudySeparateFromAccountWithSecondStudy() {
        TestUser testUser1 = TestUserHelper.createAndSignInUser(AuthenticationTest.class, true);
        Config config = ClientProvider.getConfig();
        AdminClient client = ClientProvider.signIn(config.getAdminCredentials()).getAdminClient();
        try {
            // Make a second study for this test:
            Study study = new Study();
            study.setIdentifier("secondstudy");
            study.setName("Second Study");
            study.setSupportEmail("<EMAIL>");
            study.setConsentNotificationEmail("<EMAIL>");
            study.setTechnicalEmail("<EMAIL>");
            client.createStudy(study);
            
            // Can we sign in to secondstudy? No.
            try {
                config.set(Props.STUDY_IDENTIFIER, "secondstudy");
                ClientProvider.signIn(new SignInCredentials("secondstudy", testUser1.getUsername(), testUser1.getPassword()));
                fail("Should not have allowed sign in");
            } catch(BridgeServerException e) {
                assertEquals(404, e.getStatusCode());
            }
        } finally {
            config.set(Props.STUDY_IDENTIFIER, "api");
            client.deleteStudy("secondstudy");
            testUser1.signOutAndDeleteUser();
        }
    }
    
    // BRIDGE-465. We can at least verify that it gets processed as an error.
    @Test
    public void emailVerificationThrowsTheCorrectError() throws Exception {
        Config config = ClientProvider.getConfig();
        
        HttpResponse response = Request
            .Post(config.getEnvironment().getUrl() + "/api/v1/auth/verifyEmail?study=api")
            .body(new StringEntity("{\"sptoken\":\"testtoken\",\"study\":\"api\"}"))
            .execute().returnResponse();
        assertEquals(404, response.getStatusLine().getStatusCode());
        assertEquals("{\"message\":\"Account not found.\"}", EntityUtils.toString(response.getEntity()));
    }
    
    // Should not be able to tell from the sign up response if an email is enrolled in the study or not.
    // Server change is not yet checked in for this.
    @Test
    public void secondTimeSignUpLooksTheSameAsFirstTimeSignUp() {
        TestUser testUser = TestUserHelper.createAndSignInUser(AuthenticationTest.class, true);<commit_msg>Adding some logging around possible failure in dev<commit_after>package org.sagebionetworks.bridge.sdk.integration;

import static org.junit.Assert.*;

import org.apache.http.HttpResponse;
import org.apache.http.client.fluent.Request;
import org.apache.http.entity.StringEntity;
import org.apache.http.util.EntityUtils;
import org.junit.Test;
import org.junit.experimental.categories.Category;
import org.sagebionetworks.bridge.IntegrationSmokeTest;
import org.sagebionetworks.bridge.Tests;
import org.sagebionetworks.bridge.sdk.AdminClient;
import org.sagebionetworks.bridge.sdk.ClientProvider;
import org.sagebionetworks.bridge.sdk.Config;
import org.sagebionetworks.bridge.sdk.Config.Props;
import org.sagebionetworks.bridge.sdk.Session;
import org.sagebionetworks.bridge.sdk.TestUserHelper;
import org.sagebionetworks.bridge.sdk.TestUserHelper.TestUser;
import org.sagebionetworks.bridge.sdk.exceptions.BridgeServerException;
import org.sagebionetworks.bridge.sdk.models.studies.Study;
import org.sagebionetworks.bridge.sdk.models.users.EmailCredentials;
import org.sagebionetworks.bridge.sdk.models.users.SignInCredentials;
import org.sagebionetworks.bridge.sdk.models.users.SignUpCredentials;

@Category(IntegrationSmokeTest.class)
public class AuthenticationTest {

    @Test
    public void canResendEmailVerification() {
        String username = TestUserHelper.makeUserName(AuthenticationTest.class);
        String email = username + "@sagebase.org";
        String password = "<PASSWORD>";
        
        try {
            
            SignUpCredentials signUp = new SignUpCredentials(Tests.TEST_KEY, username, email, password);
            ClientProvider.signUp(signUp);
            
            // Beyond an exception being thrown, there's not a lot you can test here.
            ClientProvider.resendEmailVerification(new EmailCredentials(Tests.TEST_KEY, email));
            
        } finally {
            Config config = ClientProvider.getConfig();
            Session session = ClientProvider.signIn(config.getAdminCredentials());
            session.getAdminClient().deleteUser(email);
        }
    }
    
    @Test
    public void resendingEmailVerificationToUnknownEmailDoesNotThrowException() throws Exception {
        ClientProvider.resendEmailVerification(new EmailCredentials(Tests.TEST_KEY, "<EMAIL>"));
    }
    
    @Test
    public void requestingResetPasswordForUnknownEmailDoesNotThrowException() throws Exception {
        ClientProvider.requestResetPassword(new EmailCredentials(Tests.TEST_KEY, "<EMAIL>"));
    }
    
    @Test
    public void accountWithOneStudySeparateFromAccountWithSecondStudy() {
        TestUser testUser1 = TestUserHelper.createAndSignInUser(AuthenticationTest.class, true);
        Config config = ClientProvider.getConfig();
        AdminClient client = ClientProvider.signIn(config.getAdminCredentials()).getAdminClient();
        try {
            // Make a second study for this test:
            Study study = new Study();
            study.setIdentifier("secondstudy");
            study.setName("Second Study");
            study.setSupportEmail("<EMAIL>");
            study.setConsentNotificationEmail("<EMAIL>");
            study.setTechnicalEmail("<EMAIL>");
            client.createStudy(study);
            
            // Can we sign in to secondstudy? No.
            try {
                config.set(Props.STUDY_IDENTIFIER, "secondstudy");
                ClientProvider.signIn(new SignInCredentials("secondstudy", testUser1.getUsername(), testUser1.getPassword()));
                fail("Should not have allowed sign in");
            } catch(BridgeServerException e) {
                assertEquals(404, e.getStatusCode());
            }
        } catch(Exception e) {
            fail("Threw an exception creating a study: " + e.getMessage());
        } finally {
            config.set(Props.STUDY_IDENTIFIER, "api");
            client.deleteStudy("secondstudy");
            testUser1.signOutAndDeleteUser();
        }
    }
    
    // BRIDGE-465. We can at least verify that it gets processed as an error.
    @Test
    public void emailVerificationThrowsTheCorrectError() throws Exception {
        Config config = ClientProvider.getConfig();
        
        HttpResponse response = Request
            .Post(config.getEnvironment().getUrl() + "/api/v1/auth/verifyEmail?study=api")
            .body(new StringEntity("{\"sptoken\":\"testtoken\",\"study\":\"api\"}"))
            .execute().returnResponse();
        assertEquals(404, response.getStatusLine().getStatusCode());
        assertEquals("{\"message\":\"Account not found.\"}", EntityUtils.toString(response.getEntity()));
    }
    
    // Should not be able to tell from the sign up response if an email is enrolled in the study or not.
    // Server change is not yet checked in for this.
    @Test
    public void secondTimeSignUpLooksTheSameAsFirstTimeSignUp() {
        TestUser testUser = TestUserHelper.createAndSignInUser(AuthenticationTest.class, true);
{'fraction_non_alphanumeric': 0.10151668351870577, 'fraction_numerical': 0.0028311425682507585, 'mean_word_length': 3.1489093959731544, 'pattern_counts': {'":': 6, '<': 15, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3144063', 'n_tokens_mistral': 2694, 'n_tokens_neox': 2649, 'n_words': 515}