Example #1
0
def test_profile(tmp_path, monkeypatch):
    '''Test fetching the bot profile.'''
    discard = Discard(mode="profile", token=TEST_TOKEN, output_dir=tmp_path)
    monkeypatch_discard(monkeypatch, discard)

    discard.run()

    directories = list(tmp_path.iterdir())

    assert len(directories) == 1

    run_directory = directories[0]

    with open(run_directory / 'run.meta.json') as f:
        obj = json.load(f)
        assert obj['client']['name'] == 'discard'
        assert 'version' in obj['client']
        assert 'discord.py_version' in obj['client']
        assert obj['settings']['mode'] == 'profile'
        assert obj['settings']['token'] == None
        assert obj['run']['completed'] == True
        assert obj['run']['finished'] == True
        assert obj['run']['errors'] == False
        assert obj['run']['exception'] == None
        assert obj['user']['name'] == 'DiscardTest'

    with open(run_directory / 'run.jsonl') as f:
        for line in f:
            if line.strip():
                obj = json.loads(line)
                assert obj['type'] in ['http', 'ws']
Example #2
0
def test_wrong_token(tmp_path, monkeypatch):
    '''Test correct exception handling when connecting with a wrong token.'''
    discard = Discard(mode="profile", token="incorrect", output_dir=tmp_path)
    monkeypatch_discard(monkeypatch, discard)

    with pytest.raises(discord.errors.LoginFailure):
        discard.run()

    run_directory = list(tmp_path.iterdir())[0]

    with open(run_directory / 'run.meta.json') as f:
        obj = json.load(f)
        assert obj['run']['completed'] == False
        assert obj['run']['finished'] == True
        assert obj['run']['errors'] == True
        assert obj['run']['exception'].startswith('LoginFailure')
Example #3
0
def test_channel(tmp_path, monkeypatch):
    '''Test archiving a single channel.'''
    discard = Discard(mode="channel",
                      channel_id=TEST_CHANNEL.id,
                      token=TEST_TOKEN,
                      output_dir=tmp_path)
    monkeypatch_discard(monkeypatch, discard)

    discard.run()

    run_directory = list(tmp_path.iterdir())[0]

    with open(run_directory / 'run.meta.json') as f:
        obj = json.load(f)
        assert obj['client']['name'] == 'discard'
        assert obj['settings']['mode'] == 'channel'
        assert obj['run']['completed'] == True
        assert obj['run']['finished'] == True
        assert obj['run']['errors'] == False
        assert obj['run']['exception'] == None

    with open(run_directory / 'run.jsonl') as f:
        for line in f:
            if line.strip():
                obj = json.loads(line)
                assert obj['type'] in ['http', 'ws']

    with open(run_directory / Path(str(TEST_GUILD.id)) /
              Path(f"{TEST_CHANNEL.id}.meta.json")) as f:
        obj = json.load(f)
        assert obj['channel']['id'] == TEST_CHANNEL.id
        assert obj['channel']['name'] == TEST_CHANNEL.name
        assert obj['summary']['num_messages'] > 0

    fetched_reactions = False

    with open(run_directory / Path(str(TEST_GUILD.id)) /
              Path(f"{TEST_CHANNEL.id}.jsonl")) as f:
        for line in f:
            if line.strip():
                obj = json.loads(line)
                assert obj['type'] in ['http', 'ws']
                if obj['type'] == 'http' and '/reactions' in obj['request'][
                        'url']:
                    fetched_reactions = True

    assert fetched_reactions
Example #4
0
def test_gzip(tmp_path, monkeypatch):
    '''Test saving gzipped files.'''
    discard = Discard(mode="profile",
                      token=TEST_TOKEN,
                      output_dir=tmp_path,
                      gzip=True)
    monkeypatch_discard(monkeypatch, discard)

    discard.run()

    run_directory = list(tmp_path.iterdir())[0]

    with open(run_directory / 'run.meta.json') as f:
        obj = json.load(f)
        assert obj['settings']['gzip'] == True

    with gzip.open(str(run_directory / 'run.jsonl.gz')) as f:
        for line in f:
            if line.strip():
                obj = json.loads(line)
                assert obj['type'] in ['http', 'ws']
Example #5
0
def guild(ctx, guild_id):
    require_token(ctx)
    discard = Discard(mode="guild", guild_id=guild_id, **ctx.obj)
    discard.run()
Example #6
0
def channel(ctx, channel_id):
    require_token(ctx)
    discard = Discard(mode="channel", channel_id=channel_id, **ctx.obj)
    discard.run()
Example #7
0
def profile(ctx):
    require_token(ctx)
    discard = Discard(mode="profile", **ctx.obj)
    discard.run()