<commit_before>import 'dart:async';
import 'dart:js';

import 'package:firebase/firebase.dart';
import 'package:scheduled_test/scheduled_test.dart';
import 'package:unittest/html_config.dart';

const TEST_URL = 'https://dart-test.firebaseio-demo.com/test/';

// Update TEST_URL to a valid URL and update AUTH_TOKEN to a corresponding
// authentication token to test authentication.
const AUTH_TOKEN = null;
const INVALID_AUTH_TOKEN = '<KEY>';

// Update CREDENTIALS_EMAIL to an email address to test
// auth using email/password credentials.
// Unfortunately, createUser does not work with the firebaseio demo test URL,
// if you want to enable this, you will likely need to change TEST_URL to your own.
const CREDENTIALS_EMAIL = null;
const CREDENTIALS_PASSWORD = '<PASSWORD>';
const CREDENTIALS_WRONG_PASSWORD = '<PASSWORD>';

final _dateKey = new DateTime.now().toUtc().toIso8601String();
final _testKey = '$_dateKey'.replaceAll(new RegExp(r'[\.]'), '_');

final _testUrl = TEST_URL + _testKey + '/';

void main() {
  useHtmlConfiguration();
  groupSep = ' - ';

  Firebase f;

  setUp(() {
    f = new Firebase(_testUrl);

    currentSchedule.onComplete.schedule(() {
      if (f != null) {
        f.unauth();
        f = null;
      }
    });
  });

  if (AUTH_TOKEN != null) {
    group('authWithCustomToken', () {
      test('bad auth token should fail', () {
        expect(f.authWithCustomToken(INVALID_AUTH_TOKEN), throwsA((error) {
          expect(error['code'], 'INVALID_TOKEN');
          return true;
        }));
      });

      test('good auth token', () {
        return f.authWithCustomToken(AUTH_TOKEN);
      });
    });
  }
  
  if (CREDENTIALS_EMAIL != null) {
    group('auth-credentials', () {
      var credentials = {'email':CREDENTIALS_EMAIL,
        'password':<PASSWORD>};
      var badCredentials = {'email':CREDENTIALS_EMAIL,
        'password':<PASSWORD>};

      test('auth-credentials', () {
        schedule(() {
          return f.createUser(credentials).then((err) {
            expect(err, null);
            f.authWithPassword(credentials).then((authResponse) {
              expect(authResponse.auth, isNotNull);
              expect(f.authWithPassword(badCredentials), throwsA((error) {
                expect(error['code'], 'INVALID_PASSWORD');
                f.removeUser(credentials).then((err) { expect(err, null); });
                return true;
              }));
            });
          });
        });
      });
    });

    group('getAuth', () {
      var credentials = {'email':CREDENTIALS_EMAIL,
          'password':<PASSWORD>};

      test('getAuth when not authenticated', () {
        var response = f.getAuth();
        expect(response, isNull);
      });

      test('getAuth when authenticated', () {
        schedule(() {
          return f.authWithPassword(credentials).then((_) {
            var response = f.getAuth();
            expect(response.auth, isNotNull);
          });
        });
      });
    });
  }

  group('non-auth', () {
    test('child', () {
      var child = f.child('trad');
      expect(child.name, 'trad');

      var parent = child.parent();
      expect(parent.name, _testKey);

      var root = child.root();
      expect(root.name, isNull);
    });

    test('set', () {
      var value = {'number value': 42};
      return f.set(value).then((v) {
        // TODO: check the value?
      });
    });

    test('set string', () {
      var child = f.child('bar');
      return child.set('foo').then((foo) {
        // TODO: actually test result
      });
    });<commit_msg>added tests for createUser and removeUser<commit_after>import 'dart:async';
import 'dart:js';

import 'package:firebase/firebase.dart';
import 'package:scheduled_test/scheduled_test.dart';
import 'package:unittest/html_config.dart';

const TEST_URL = 'https://dart-test.firebaseio-demo.com/test/';

// Update TEST_URL to a valid URL and update AUTH_TOKEN to a corresponding
// authentication token to test authentication.
const AUTH_TOKEN = null;
const INVALID_AUTH_TOKEN = '<KEY>';

// Update CREDENTIALS_EMAIL to an email address to test
// auth using email/password credentials.
// Unfortunately, createUser does not work with the firebaseio demo test URL,
// if you want to enable this, you will likely need to change TEST_URL to your own.
const CREDENTIALS_EMAIL = null;
const CREDENTIALS_PASSWORD = '<PASSWORD>';
const CREDENTIALS_WRONG_PASSWORD = '<PASSWORD>';

final _dateKey = new DateTime.now().toUtc().toIso8601String();
final _testKey = '$_dateKey'.replaceAll(new RegExp(r'[\.]'), '_');

final _testUrl = TEST_URL + _testKey + '/';

void main() {
  useHtmlConfiguration();
  groupSep = ' - ';

  Firebase f;

  setUp(() {
    f = new Firebase(_testUrl);

    currentSchedule.onComplete.schedule(() {
      if (f != null) {
        f.unauth();
        f = null;
      }
    });
  });

  if (AUTH_TOKEN != null) {
    group('authWithCustomToken', () {
      test('bad auth token should fail', () {
        expect(f.authWithCustomToken(INVALID_AUTH_TOKEN), throwsA((error) {
          expect(error['code'], 'INVALID_TOKEN');
          return true;
        }));
      });

      test('good auth token', () {
        return f.authWithCustomToken(AUTH_TOKEN);
      });
    });
  }
  
  if (CREDENTIALS_EMAIL != null) {
    group('auth-credentials', () {
      var credentials = {'email':CREDENTIALS_EMAIL,
        'password':<PASSWORD>};
      var badCredentials = {'email':CREDENTIALS_EMAIL,
        'password':<PASSWORD>};

      test('auth-credentials', () {
        schedule(() {
          return f.createUser(credentials).then((err) {
            expect(err, null);
            f.authWithPassword(credentials).then((authResponse) {
              expect(authResponse.auth, isNotNull);
              expect(f.authWithPassword(badCredentials), throwsA((error) {
                expect(error['code'], 'INVALID_PASSWORD');
                f.removeUser(credentials).then((err) { expect(err, null); });
                return true;
              }));
            });
          });
        });
      });
    });

    group('createUser', () {
      test('createUser returns null on success', () {
        schedule(() {
          var credentials = {'email': '<EMAIL>',
              'password': '<PASSWORD>'};
          return f.createUser(credentials).then((result) {
            expect(result, null);
            f.removeUser(credentials);
          });
        });
      });

      test('createUser throws error', () {
        schedule(() {
          var credentials = {'email': 'badEmailAddress',
              'password': '<PASSWORD>'};
          expect(f.createUser(credentials), throwsA((error) {
            expect(error['code'], 'INVALID_EMAIL');
            return true;
          }));
        });
      });

    });

    group('removeUser', () {
      test('removeUser returns null on success', () {
        var credentials = {'email': '<EMAIL>',
            'password': '<PASSWORD>'};
        schedule(() {
          return f.createUser(credentials).then((result) {
            f.removeUser(credentials).then((result) {
              expect(result, null);
            });
          });
        });
      });

      test('removeUser returns error', () {
        var credentials = {'email': '<EMAIL>',
            'password': '<PASSWORD>'};
        schedule(() {
          expect(f.removeUser(credentials), throwsA((error) {
            expect(error['code'], 'INVALID_USER');
            return true;
          }));
        });
      });
    });

    group('getAuth', () {
      var credentials = {'email':CREDENTIALS_EMAIL,
          'password':CREDENTIALS_PASSWORD};

      test('getAuth when not authenticated', () {
        var response = f.getAuth();
        expect(response, isNull);
      });

      test('getAuth when authenticated', () {
        schedule(() {
          var credentials = {
              'email': '<EMAIL>',
              'password': '<PASSWORD>'
          };
          return f.createUser(credentials).then((_) {
            f.authWithPassword(credentials).then((_) {
              var response = f.getAuth();
              expect(response.auth, isNotNull);
              f.removeUser(credentials);
            });
          });
        });
      });
    });
  }

  group('non-auth', () {
    test('child', () {
      var child = f.child('trad');
      expect(child.name, 'trad');

      var parent = child.parent();
      expect(parent.name, _testKey);

      var root = child.root();
      expect(root.name, isNull);
    });

    test('set', () {
      var value = {'number value': 42};
      return f.set(value).then((v) {
        // TODO: check the value?
      });
    });

    test('set string', () {
      var child = f.child('bar');
      return child.set('foo').then((foo) {
        // TODO: actually test result
      });
    });
{'fraction_non_alphanumeric': 0.1581148282097649, 'fraction_numerical': 0.00135623869801085, 'mean_word_length': 2.3192048012003, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 23, 'https://': 2, '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': '1015102', 'n_tokens_mistral': 2616, 'n_tokens_neox': 2592, 'n_words': 537}