summaryrefslogtreecommitdiffstats
path: root/tests/auto/installer/repository/tst_repository.cpp
blob: 7b8405bafb8416a0241440990da1413d40a19612 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#include "repository.h"
#include "repositorycategory.h"
#include "settings.h"

#include <QDataStream>
#include <QString>
#include <QTest>

using namespace QInstaller;

class tst_Repository : public QObject
{
    Q_OBJECT

private slots:
    void testRepository()
    {
        Repository repo;
        QCOMPARE(repo.isValid(), false);
        QCOMPARE(repo.isDefault(), false);
        QCOMPARE(repo.isEnabled(), false);

        QCOMPARE(repo.url(), QUrl());
        QCOMPARE(repo.username(), QString());
        QCOMPARE(repo.password(), QString());

        repo.setUrl(QUrl("http://www.digia.com"));
        QCOMPARE(repo.isValid(), true);
        QCOMPARE(repo.url(), QUrl("http://www.digia.com"));

        repo.setEnabled(true);
        QCOMPARE(repo.isEnabled(), true);

        repo.setUsername("tester");
        QCOMPARE(repo.username(), QString("tester"));

        repo.setPassword("test");
        QCOMPARE(repo.password(), QString("test"));
    }

    void testRepositoryFromUrl()
    {
        Repository repo(QUrl("http://www.digia.com"), true);
        QCOMPARE(repo.isValid(), true);
        QCOMPARE(repo.isDefault(), true);
        QCOMPARE(repo.isEnabled(), true);

        QCOMPARE(repo.url(), QUrl("http://www.digia.com"));
        QCOMPARE(repo.username(), QString());
        QCOMPARE(repo.password(), QString());

        repo.setUrl(QUrl());
        QCOMPARE(repo.isValid(), false);

        repo.setEnabled(false);
        QCOMPARE(repo.isEnabled(), false);

        repo.setUsername("tester");
        QCOMPARE(repo.username(), QString("tester"));

        repo.setPassword("test");
        QCOMPARE(repo.password(), QString("test"));
    }

    void testRepositoryFromUserInput()
    {
        Repository repo = Repository::fromUserInput("ftp://tester:test@www.digia.com");
        QCOMPARE(repo.isValid(), true);
        QCOMPARE(repo.isDefault(), false);
        QCOMPARE(repo.isEnabled(), true);

        QCOMPARE(repo.url(), QUrl("ftp://www.digia.com"));
        QCOMPARE(repo.username(), QString("tester"));
        QCOMPARE(repo.password(), QString("test"));

        repo.setUrl(QUrl());
        QCOMPARE(repo.isValid(), false);

        repo.setEnabled(false);
        QCOMPARE(repo.isEnabled(), false);

        repo.setUsername("");
        QCOMPARE(repo.username(), QString());

        repo.setPassword("");
        QCOMPARE(repo.password(), QString());
    }

    void testRepositoryOperators()
    {
        Repository lhs, rhs;

        // operator==
        QVERIFY(lhs == rhs);

        // assignment operator
        rhs = Repository::fromUserInput("ftp://tester:test@www.digia.com");

        // operator!=
        QVERIFY(lhs != rhs);

        // copy constructor
        Repository clhs(rhs);

        // operator==
        QVERIFY(clhs == rhs);

        QByteArray ba1, ba2;
        QDataStream s1(&ba1, QIODevice::ReadWrite), s2(&ba2, QIODevice::ReadWrite);

        // QDatastream operator
        s1 << clhs; s2 << rhs;
        QCOMPARE(ba1, ba2);

        Repository r1, r2;
        s1 >> r1; s2 >> r2;
        QCOMPARE(r1, r2);
    }

    void testUpdateRepositoryCategories()
    {
        Settings settings;

        RepositoryCategory category;
        category.setEnabled(true);
        category.setDisplayName(QLatin1String("category"));

        Repository original(QUrl("http://example.com/"), true);
        original.setEnabled(true);
        category.addRepository(original);

        Repository replacement = original;
        replacement.setUsername(QLatin1String("user"));
        replacement.setPassword(QLatin1String("pass"));

        QSet<RepositoryCategory> categories;
        categories.insert(category);
        settings.setRepositoryCategories(categories);

        QHash<QString, QPair<Repository, Repository>> update;

        // non-empty update
        update.insert(QLatin1String("replace"), qMakePair(original, replacement));
        QVERIFY(settings.updateRepositoryCategories(update) == Settings::UpdatesApplied);

        // verify that the values really updated
        QVERIFY(settings.repositoryCategories().values().at(0)
            .repositories().values().at(0).username() == "user");

        QVERIFY(settings.repositoryCategories().values().at(0)
            .repositories().values().at(0).password() == "pass");

        // empty update
        update.clear();
        QVERIFY(settings.updateRepositoryCategories(update) == Settings::NoUpdatesApplied);

        // non-matching repository update
        Repository nonMatching(QUrl("https://www.qt.io/"), true);
        nonMatching.setEnabled(true);

        update.insert(QLatin1String("replace"), qMakePair(nonMatching, replacement));
        QVERIFY(settings.updateRepositoryCategories(update) == Settings::NoUpdatesApplied);
    }
};

QTEST_MAIN(tst_Repository)

#include "tst_repository.moc"