@@ -29,23 +29,34 @@ void TestUtils::merginGetAuthCredentials( MerginApi *api, QString &apiRoot, QStr
2929 Q_ASSERT ( api );
3030
3131 // Test server url needs to be set
32- Q_ASSERT ( ::getenv ( " TEST_MERGIN_URL" ) );
33-
34- apiRoot = ::getenv ( " TEST_MERGIN_URL" );
32+ if ( ::getenv ( " TEST_MERGIN_URL" ) == nullptr )
33+ {
34+ // if there is none, just default to the dev one
35+ apiRoot = QStringLiteral ( " https://app.dev.merginmaps.com/" );
36+ }
37+ else
38+ {
39+ apiRoot = ::getenv ( " TEST_MERGIN_URL" );
40+ // let's make sure we do not mess with the public instance
41+ Q_ASSERT ( apiRoot != MerginApi::sDefaultApiRoot );
42+ }
3543 api->setApiRoot ( apiRoot );
3644 qDebug () << " MERGIN API ROOT:" << apiRoot;
3745
38- // let's make sure we do not mess with the public instance
39- Q_ASSERT ( apiRoot != MerginApi::sDefaultApiRoot );
40-
41- // Test user needs to be set
42- Q_ASSERT ( ::getenv ( " TEST_API_USERNAME" ) );
43-
44- // Test password needs to be set
45- Q_ASSERT ( ::getenv ( " TEST_API_PASSWORD" ) );
46-
47- username = ::getenv ( " TEST_API_USERNAME" );
48- password = ::getenv ( " TEST_API_PASSWORD" );
46+ // test user needs to be set
47+ // check if there are environmental variables for the username and the password
48+ if ( ::getenv ( " TEST_API_USERNAME" ) == nullptr && ::getenv ( " TEST_API_PASSWORD" ) == nullptr )
49+ {
50+ // generate a random email and pasword
51+ // create the user on the server
52+ // create a workspace for the user
53+ generateRandomUser ( api, username, password );
54+ }
55+ else
56+ {
57+ username = ::getenv ( " TEST_API_USERNAME" );
58+ password = ::getenv ( " TEST_API_PASSWORD" );
59+ }
4960}
5061
5162void TestUtils::authorizeUser ( MerginApi *api, const QString &username, const QString &password )
@@ -112,14 +123,14 @@ QString TestUtils::generateUsername()
112123{
113124 QDateTime time = QDateTime::currentDateTime ();
114125 QString uniqename = time.toString ( QStringLiteral ( " ddMMyy-hhmmss-z" ) );
115- return QStringLiteral ( " input -%1" ).arg ( uniqename );
126+ return QStringLiteral ( " mobile -%1" ).arg ( uniqename );
116127}
117128
118129QString TestUtils::generateEmail ()
119130{
120131 QDateTime time = QDateTime::currentDateTime ();
121132 QString uniqename = time.toString ( QStringLiteral ( " ddMMyy-hhmmss-z" ) );
122- return QStringLiteral ( " mergin+ autotest+%1@lutraconsulting.co.uk" ).arg ( uniqename );
133+ return QStringLiteral ( " mobile- autotest+%1@lutraconsulting.co.uk" ).arg ( uniqename );
123134}
124135
125136QString TestUtils::generatePassword ()
@@ -128,6 +139,86 @@ QString TestUtils::generatePassword()
128139 return QStringLiteral ( " _Pass12%1" ).arg ( pass );
129140}
130141
142+ QString TestUtils::generateWorkspaceName ( const QString &username )
143+ {
144+ static const QRegularExpression regex ( R"( mobile-autotest(\d{6})-(\d{4})\d{2}-\d{3})" );
145+ const QRegularExpressionMatch match = regex.match ( username );
146+
147+ if ( match.hasMatch () )
148+ {
149+ const QString date = match.captured ( 1 ); // Day Month Year
150+ const QString time = match.captured ( 2 ); // Hour Second
151+ return QString ( " mmat-%1-%2" ).arg ( date, time );
152+ }
153+ return {};
154+ }
155+
156+ void TestUtils::generateRandomUser ( MerginApi *api, QString &username, QString &password )
157+ {
158+ // generate the test run-specific user details
159+ QString email = generateEmail ();
160+ password = generatePassword ();
161+ username = email.split ( ' @' ).first ();
162+ username.remove ( " +" );
163+
164+ // create the account for the test run user
165+ api->clearAuth ();
166+ QSignalSpy spy ( api, &MerginApi::registrationSucceeded );
167+ QSignalSpy spy2 ( api, &MerginApi::registrationFailed );
168+ api->registerUser ( email, password, true );
169+ // check that the account has been created.
170+ bool success = spy.wait ( TestUtils::LONG_REPLY );
171+ if ( !success )
172+ {
173+ qDebug () << " Failed registration" << spy2.takeFirst ();
174+ QVERIFY ( false );
175+ }
176+
177+ // check that the user can be authorized
178+ QSignalSpy spyAuth ( api->userAuth (), &MerginUserAuth::authChanged );
179+ api->authorize ( email, password );
180+ QVERIFY ( spyAuth.wait ( TestUtils::LONG_REPLY * 5 ) );
181+
182+ // create workspace
183+ QSignalSpy wsSpy ( api, &MerginApi::workspaceCreated );
184+ // create the workspace name
185+ QString workspace = generateWorkspaceName ( username );
186+ api->createWorkspace ( workspace );
187+ bool workspaceSuccess = wsSpy.wait ( TestUtils::LONG_REPLY );
188+ QVERIFY ( workspaceSuccess );
189+ qDebug () << " CREATED NEW WORKSPACE:" << workspace;
190+
191+ // call userInfo to set active workspace
192+ QSignalSpy infoSpy ( api, &MerginApi::userInfoReplyFinished );
193+ api->getUserInfo ();
194+ QVERIFY ( infoSpy.wait ( TestUtils::LONG_REPLY ) );
195+ QVERIFY ( api->userInfo ()->activeWorkspaceId () >= 0 );
196+
197+ // change the data plan
198+ QString workspaceId = QString::number ( api->userInfo ()->activeWorkspaceId () );
199+ QSignalSpy wsStorageSpy ( api, &MerginApi::updateWorkspaceService );
200+
201+ // Create JSON payload to change the data plan
202+ QString payload = QString ( R"( {
203+ "limits_override": {
204+ "storage": %1,
205+ "projects" : %2,
206+ "api_allowed" : true
207+ }
208+ })" ).arg ( TEST_WORKSPACE_STORAGE_SIZE ).arg ( TEST_WORKSPACE_PROJECT_NUMBER );
209+
210+ api->updateWorkspaceService ( workspaceId, payload );
211+ bool workspaceStorageModified = wsStorageSpy.wait ( TestUtils::LONG_REPLY );
212+ if ( workspaceStorageModified )
213+ {
214+ qDebug () << " Updated the storage limit" << workspace;
215+ }
216+
217+ // this needs to be cleared, as the user will be authorized in the test cases.
218+ api->clearAuth ();
219+
220+ }
221+
131222QString TestUtils::testDataDir ()
132223{
133224 QString dataDir ( TEST_DATA_DIR );
0 commit comments