|
| 1 | +/* |
| 2 | + * Copyright 2017 Realm Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package io.realm; |
| 18 | + |
| 19 | +import android.support.test.runner.AndroidJUnit4; |
| 20 | + |
| 21 | +import org.junit.Test; |
| 22 | +import org.junit.runner.RunWith; |
| 23 | + |
| 24 | +import io.realm.permissions.AccessLevel; |
| 25 | +import io.realm.permissions.PermissionRequest; |
| 26 | +import io.realm.permissions.UserCondition; |
| 27 | + |
| 28 | +import static org.junit.Assert.assertEquals; |
| 29 | +import static org.junit.Assert.assertTrue; |
| 30 | +import static org.junit.Assert.fail; |
| 31 | + |
| 32 | + |
| 33 | +@RunWith(AndroidJUnit4.class) |
| 34 | +public class PermissionRequestTests { |
| 35 | + |
| 36 | + @Test |
| 37 | + public void nullArgumentsThrows() { |
| 38 | + try { |
| 39 | + new PermissionRequest(null, "*", AccessLevel.ADMIN); |
| 40 | + fail(); |
| 41 | + } catch (IllegalArgumentException e) { |
| 42 | + assertTrue(e.getMessage().contains("Non-null 'condition' required.")); |
| 43 | + } |
| 44 | + |
| 45 | + try { |
| 46 | + new PermissionRequest(UserCondition.userId("id"), null, AccessLevel.ADMIN); |
| 47 | + fail(); |
| 48 | + } catch (IllegalArgumentException e) { |
| 49 | + assertTrue(e.getMessage().contains("Non-empty 'realmUrl' required.")); |
| 50 | + } |
| 51 | + |
| 52 | + try { |
| 53 | + new PermissionRequest(UserCondition.userId("id"), "*", null); |
| 54 | + fail(); |
| 55 | + } catch (IllegalArgumentException e) { |
| 56 | + assertTrue(e.getMessage().contains("Non-null 'accessLevel' required.")); |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + @Test |
| 61 | + public void url_throwsOnInvalidURIs() { |
| 62 | + String[] invalidUrls = { "", "\\", "<foo>" }; |
| 63 | + for (String url : invalidUrls) { |
| 64 | + try { |
| 65 | + new PermissionRequest(UserCondition.userId("id"), url, AccessLevel.ADMIN); |
| 66 | + fail(url + " should have thrown"); |
| 67 | + } catch (IllegalArgumentException ignore) { |
| 68 | + } |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + @Test |
| 73 | + public void url_validURIs() { |
| 74 | + // We support "*" and valid URI's |
| 75 | + // We don't attempt to do more validation than that and leaves that up to ROS |
| 76 | + String[] validUrls = { |
| 77 | + "*", |
| 78 | + "http://foo/bar/baz", |
| 79 | + "https://foo/bar/baz", |
| 80 | + "realm://foo.bar/~/default", |
| 81 | + "realms://foo.bar/~/default" |
| 82 | + }; |
| 83 | + for (String url : validUrls) { |
| 84 | + PermissionRequest request = new PermissionRequest(UserCondition.userId("id"), url, AccessLevel.ADMIN); |
| 85 | + assertEquals(url, request.getUrl()); |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + @Test |
| 90 | + public void getters() { |
| 91 | + UserCondition condition = UserCondition.userId("id"); |
| 92 | + String url = "*"; |
| 93 | + AccessLevel accessLevel = AccessLevel.ADMIN; |
| 94 | + |
| 95 | + PermissionRequest request = new PermissionRequest(condition, url, accessLevel); |
| 96 | + |
| 97 | + assertEquals(condition, request.getCondition()); |
| 98 | + assertEquals(url, request.getUrl()); |
| 99 | + assertEquals(accessLevel, request.getAccessLevel()); |
| 100 | + } |
| 101 | + |
| 102 | + @Test |
| 103 | + public void equals() { |
| 104 | + PermissionRequest r1 = new PermissionRequest(UserCondition.userId("id"), "*", AccessLevel.ADMIN); |
| 105 | + PermissionRequest r2 = new PermissionRequest(UserCondition.userId("id"), "*", AccessLevel.ADMIN); |
| 106 | + |
| 107 | + assertTrue(r1.equals(r2)); |
| 108 | + assertTrue(r2.equals(r1)); |
| 109 | + assertEquals(r1.hashCode(), r2.hashCode()); |
| 110 | + } |
| 111 | + |
| 112 | +} |
0 commit comments