<commit_before>/*
 * acme4j - Java ACME client
 *
 * Copyright (C) 2017 Richard "Shred" Körber
 *   http://acme4j.shredzone.org
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 */
package org.shredzone.acme4j.it.pebble;

import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;

import java.net.URI;
import java.net.URL;
import java.security.KeyPair;

import org.junit.Ignore;
import org.junit.Test;
import org.shredzone.acme4j.Account;
import org.shredzone.acme4j.AccountBuilder;
import org.shredzone.acme4j.Session;
import org.shredzone.acme4j.Status;
import org.shredzone.acme4j.exception.AcmeException;
import org.shredzone.acme4j.exception.AcmeServerException;
import org.shredzone.acme4j.exception.AcmeUnauthorizedException;

/**
 * Account related integration tests.
 */
public class AccountIT extends PebbleITBase {

    @Test
    public void testCreate() throws AcmeException {
        KeyPair keyPair = createKeyPair();
        Session session = new Session(pebbleURI(), keyPair);

        // Register a new user
        AccountBuilder ab = new AccountBuilder();
        ab.addContact("mailto:<EMAIL>");
        ab.agreeToTermsOfService();

        Account acct = ab.create(session);
        URL location = acct.getLocation();
        assertIsPebbleUrl(location);
        assertThat(session.getAccountLocation(), is(location));

        // Check registered data
        assertThat(acct.getContacts(), contains(URI.create("mailto:<EMAIL>")));
        assertThat(acct.getStatus(), is(Status.VALID));

        // Bind another Account object
        // TODO PEBBLE: Not supported yet
        // Session session2 = new Session(pebbleURI(), keyPair);
        // Account acct2 = Account.bind(session2, location);
        // assertThat(acct2.getLocation(), is(location));
        // assertThat(acct2.getContacts(), contains(URI.create("mailto:<EMAIL>")));
        // assertThat(acct2.getStatus(), is(Status.VALID));
    }

    @Test
    public void testCreateOnlyExisting() throws AcmeException {
        KeyPair keyPair = createKeyPair();

        Session session1 = new Session(pebbleURI(), keyPair);
        Account acct1 = new AccountBuilder()
                        .agreeToTermsOfService()
                        .create(session1);
        URL location1 = acct1.getLocation();
        assertIsPebbleUrl(location1);
        assertThat(session1.getAccountLocation(), is(location1));

        Session session2 = new Session(pebbleURI(), keyPair);
        Account acct2 = new AccountBuilder()
                        .onlyExisting()
                        .create(session2);
        URL location2 = acct2.getLocation();
        assertIsPebbleUrl(location2);
        assertThat(session2.getAccountLocation(), is(location2));

        assertThat(location1, is(location2));
    }

    @Test
    public void testNotExisting() throws AcmeException {
        try {
            KeyPair keyPair = createKeyPair();
            Session session = new Session(pebbleURI(), keyPair);
            new AccountBuilder().onlyExisting().create(session);
            fail("Expected an error");
        } catch (AcmeServerException ex) {
            assertThat(ex.getType(), is(URI.create("urn:ietf:params:acme:error:accountDoesNotExist")));
        }
    }

    @Test
    @Ignore // TODO PEBBLE: missing
    public void testModify() throws AcmeException {
        KeyPair keyPair = createKeyPair();
        Session session = new Session(pebbleURI(), keyPair);

        AccountBuilder ab = new AccountBuilder();
        ab.addContact("mailto:<EMAIL>");
        ab.agreeToTermsOfService();

        Account acct = ab.create(session);
        URL location = acct.getLocation();
        assertIsPebbleUrl(location);

        acct.modify().addContact("mailto:<EMAIL>").commit();

        assertThat(acct.getContacts(), contains(
                        URI.create("mailto:<EMAIL>"),
                        URI.create("mailto:<EMAIL>")));

        // Still the same after updating
        acct.update();
        assertThat(acct.getContacts(), contains(
                        URI.create("mailto:<EMAIL>"),
                        URI.create("mailto:<EMAIL>")));
    }

    @Test
    @Ignore // TODO PEBBLE: missing
    public void testKeyChange() throws AcmeException {
        KeyPair keyPair = createKeyPair();
        Session session = new Session(pebbleURI(), keyPair);

        Account acct = new AccountBuilder().agreeToTermsOfService().create(session);
        URL location = acct.getLocation();

        KeyPair newKeyPair = createKeyPair();
        acct.changeKey(newKeyPair);

        try {
            Session sessionOldKey = new Session(pebbleURI(), keyPair);
            Account oldAccount = Account.bind(sessionOldKey, location);
            oldAccount.update();
        } catch (AcmeUnauthorizedException ex) {
            // Expected
        }

        Session sessionNewKey = new Session(pebbleURI(), newKeyPair);
        Account newAccount = Account.bind(sessionNewKey, location);
        assertThat(newAccount.getStatus(), is(Status.VALID));
    }

    @Test
    @Ignore // TODO PEBBLE: missing
    public void testDeactivate() throws AcmeException {<commit_msg>More Pebble integration tests<commit_after>/*
 * acme4j - Java ACME client
 *
 * Copyright (C) 2017 Richard "Shred" Körber
 *   http://acme4j.shredzone.org
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 */
package org.shredzone.acme4j.it.pebble;

import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;

import java.net.URI;
import java.net.URL;
import java.security.KeyPair;

import org.junit.Ignore;
import org.junit.Test;
import org.shredzone.acme4j.Account;
import org.shredzone.acme4j.AccountBuilder;
import org.shredzone.acme4j.Session;
import org.shredzone.acme4j.Status;
import org.shredzone.acme4j.exception.AcmeException;
import org.shredzone.acme4j.exception.AcmeServerException;
import org.shredzone.acme4j.exception.AcmeUnauthorizedException;

/**
 * Account related integration tests.
 */
public class AccountIT extends PebbleITBase {

    /**
     * Create a new account, then bind it to a second session.
     */
    @Test
    public void testCreate() throws AcmeException {
        KeyPair keyPair = createKeyPair();
        Session session = new Session(pebbleURI(), keyPair);

        // Register a new user
        AccountBuilder ab = new AccountBuilder();
        ab.addContact("mailto:<EMAIL>");
        ab.agreeToTermsOfService();

        Account acct = ab.create(session);
        URL location = acct.getLocation();
        assertIsPebbleUrl(location);
        assertThat(session.getAccountLocation(), is(location));

        // Check registered data
        assertThat(acct.getContacts(), contains(URI.create("mailto:<EMAIL>")));
        assertThat(acct.getStatus(), is(Status.VALID));

        // Bind another Account object
        Session session2 = new Session(pebbleURI(), keyPair);
        Account acct2 = Account.bind(session2, location);
        assertThat(acct2.getLocation(), is(location));
        assertThat(acct2.getContacts(), contains(URI.create("mailto:<EMAIL>")));
        assertThat(acct2.getStatus(), is(Status.VALID));
    }

    /**
     * Register the same key pair twice.
     */
    @Test
    public void testReCreate() throws AcmeException {
        KeyPair keyPair = createKeyPair();

        // Register a new user
        Session session1 = new Session(pebbleURI(), keyPair);
        Account acct1 = new AccountBuilder()
                        .addContact("mailto:<EMAIL>")
                        .agreeToTermsOfService()
                        .create(session1);
        URL location1 = acct1.getLocation();
        assertIsPebbleUrl(location1);

        // Try to register the same account again
        Session session2 = new Session(pebbleURI(), keyPair);
        Account acct2 = new AccountBuilder()
                        .addContact("mailto:<EMAIL>")
                        .agreeToTermsOfService()
                        .create(session2);
        URL location2 = acct2.getLocation();
        assertIsPebbleUrl(location2);

        assertThat(location1, is(location2));
    }

    /**
     * Create a new account. Locate it via onlyExisting.
     */
    @Test
    public void testCreateOnlyExisting() throws AcmeException {
        KeyPair keyPair = createKeyPair();

        Session session1 = new Session(pebbleURI(), keyPair);
        Account acct1 = new AccountBuilder()
                        .agreeToTermsOfService()
                        .create(session1);
        URL location1 = acct1.getLocation();
        assertIsPebbleUrl(location1);
        assertThat(session1.getAccountLocation(), is(location1));

        Session session2 = new Session(pebbleURI(), keyPair);
        Account acct2 = new AccountBuilder()
                        .onlyExisting()
                        .create(session2);
        URL location2 = acct2.getLocation();
        assertIsPebbleUrl(location2);
        assertThat(session2.getAccountLocation(), is(location2));

        assertThat(location1, is(location2));
    }

    /**
     * Locate a non-existing account via onlyExisting. Make sure an accountDoesNotExist
     * error is returned.
     */
    @Test
    public void testNotExisting() throws AcmeException {
        try {
            KeyPair keyPair = createKeyPair();
            Session session = new Session(pebbleURI(), keyPair);
            new AccountBuilder().onlyExisting().create(session);
            fail("onlyExisting flag was ignored");
        } catch (AcmeServerException ex) {
            assertThat(ex.getType(), is(URI.create("urn:ietf:params:acme:error:accountDoesNotExist")));
        }
    }

    /**
     * Modify the contacts of an account.
     */
    @Test
    public void testModify() throws AcmeException {
        KeyPair keyPair = createKeyPair();
        Session session = new Session(pebbleURI(), keyPair);

        Account acct = new AccountBuilder()
                        .addContact("mailto:<EMAIL>")
                        .agreeToTermsOfService()
                        .create(session);
        URL location = acct.getLocation();
        assertIsPebbleUrl(location);

        acct.modify().addContact("mailto:<EMAIL>").commit();

        assertThat(acct.getContacts(), contains(
                        URI.create("mailto:<EMAIL>"),
                        URI.create("mailto:<EMAIL>")));

        // Still the same after updating
        acct.update();
        assertThat(acct.getContacts(), contains(
                        URI.create("mailto:<EMAIL>"),
                        URI.create("mailto:<EMAIL>")));
    }

    /**
     * Change the account key.
     */
    @Test
    @Ignore // TODO PEBBLE: missing
    public void testKeyChange() throws AcmeException {
        KeyPair keyPair = createKeyPair();
        Session session = new Session(pebbleURI(), keyPair);

        Account acct = new AccountBuilder().agreeToTermsOfService().create(session);
        URL location = acct.getLocation();

        KeyPair newKeyPair = createKeyPair();
        acct.changeKey(newKeyPair);

        try {
            Session sessionOldKey = new Session(pebbleURI(), keyPair);
            Account oldAccount = Account.bind(sessionOldKey, location);
            oldAccount.update();
        } catch (AcmeUnauthorizedException ex) {
            // Expected
        }

        Session sessionNewKey = new Session(pebbleURI(), newKeyPair);
        Account newAccount = Account.bind(sessionNewKey, location);
        assertThat(newAccount.getStatus(), is(Status.VALID));
    }

    /**
     * Deactivate an account.
     */
    @Test
    @Ignore // TODO PEBBLE: missing
    public void testDeactivate() throws AcmeException {
{'fraction_non_alphanumeric': 0.11021571021571022, 'fraction_numerical': 0.007651607651607652, 'mean_word_length': 2.5426758938869667, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 23, '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': '7392615', 'n_tokens_mistral': 3431, 'n_tokens_neox': 3380, 'n_words': 794}